This is for a school project. I have a Query<E> class that holds an element that is part of the Query condition. So if it’s a GreaterQuery and the element is 10, all values greater than 10 pass the condition.
The constructor of this Query object takes the element, and a Comparator object to be used to test the condition. If null is passed in, I need to create a Comparator to use for the comparison.
I have a match(E element) method that tests the condition. Here’s bare bones version of it:
public boolean matches(E element)
{
if (comparator == null)
{
}
}
What I thought I could do was write an anonymous Comparator class inside the if statement, though I’m not sure if that’s even possible. The other problem I’m having is how to even compare to E objects without knowing anything about them. Also, I’m not sure of this, but there may be a pre condition that E extends Comparable. It’s very late and I apologize if this makes no sense. But any help will be appreciated.
If
Eis guaranteed to implementComparable<E>, you can do this: