0

Are there any object representation of these comparison operators (<, <=, ==, >=, >, !=) in Java ?

E.g. use case:

void filterHotel( Object operator, float rating ) {

    String query = "SELECT hotel.name from hotel where hotel.rating " + 
operator.toString() + rating;    
    // execute query
}
brainydexter
  • 19,826
  • 28
  • 77
  • 115
  • 1
    Just write your own, it'll take 5 minutes.. – calebds Mar 12 '12 at 17:30
  • 2
    Why not just a `String`? – Gowtham Mar 12 '12 at 17:31
  • 1
    Java is not c++. You can not override operators in java. – DwB Mar 12 '12 at 17:32
  • 1
    @paislee: actually I admire people who always check whether maybe *this small thingy* already exists. Otherwise we end up with duplication and code harder to port. – Tomasz Nurkiewicz Mar 12 '12 at 17:33
  • Why? You're writing SQL in the form of a string. Why would the SQL operators be so special that you'd want to extract those? If you're doing that, why stop at the operators, why not extract SELECT and FROM and WHERE keywords too? Or the dot and the comma, and what have you? I can imagine that it makes sense to externalize those things that actually differ between RDBMS platforms. To crack that problem, the operators are probably least of your worries. – Roland Bouman Mar 12 '12 at 17:35
  • I thought `<>` rather than `!=` was standard SQL. – Peter Lawrey Mar 12 '12 at 17:35
  • @PeterLawrey - Absolutely right. See [this answer](http://stackoverflow.com/a/723426/535871) – Ted Hopp Mar 12 '12 at 17:39
  • 1
    @TomaszNurkiewicz: Yep, I asked for the same reason :). There's like so many things happening, so there's always a good chance that the wheel has already been invented! – brainydexter Mar 12 '12 at 17:44

2 Answers2

3

No. But it is easy to write, consider using enum with custom method:

public enum Operator {
    EQUAL("=="),
    NOT_EQUAL("<>"),
    GREATER_THAN(">"),
    GREATER_THAN_OR_EQUAL(">="),
    LESS_THAN("<"),
    LESS_THAN_OR_EQUAL("<=");

    private final String representation;

    private Operator(String representation) {
        this.representation = representation;
    }

    public String getRepresentation() {
        return representation;
    }
}

Pass e.g. Operator.LESS_THAN and extract actual operator using operator.getRepresentation().

Also make sure user cannot put arbitrary string in place of operator to avoid .

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
1

There's nothing built in, but you can define an enum that does the trick:

public enum ComparisonOperator {
    LT("<"), LE("<="), EQ("=="), NE("<>"), GE(">="), GT(">");

    ComparisonOperator(String symbol) { this.symbol = symbol; }
    private final String symbol;
    public String toSymbol() { return symbol; }
}

Then:

void filterHotel(ComparisonOperator operator, float rating) {

    String query = "SELECT hotel.name from hotel where hotel.rating " + 
        operator.toSymbol() + rating;    
    // execute query
}
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521