Trying to use Java's DelayQueue
, I have to implement the Delayed
interface which requires a compareTo()
"method that provides an ordering consistent with its getDelay method.". The intention is of course that the DelayQueue
can easily sort the queued objects such that the next one running out of its delay can be returned to any taker.
Now I have the need to also remove objects from the queue ahead of time. I need to call delayQueue.remove(queuedObject)
. This of course only works if the queued objects have an equals()
method that reflects their payload and not the completely unrelated remaining delay time.
As a result, compareTo()
is based on the remaining delay time while equals()
is based on the payload of the queued objects, so they are not consistent, as is "strongly recommended" in the javadoc of Comparable
.
Question: am I missing something or is this indeed a bit of a quirk in the design of DelayQueue
?
compareTo
inconsistent withequals
, includingScheduledFutureTask
which implementsDelayed
. – Whitehall