comparison operator objects in java -
are there object representation of these comparison operators (<, <=, ==, >=, >, !=) in java ?
e.g. use case:
void filterhotel( object operator, float rating ) { string query = "select hotel.name hotel hotel.rating " + operator.tostring() + rating; // execute query }
no. easy write, consider using enum
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
, extract actual operator using operator.getrepresentation()
.
also make sure user cannot put arbitrary string in place of operator
avoid sql-injection.
Comments
Post a Comment