A Queue
is a Collection
, so you can always copy its elements to another Collection
that lets you retrieve the order, e.g.
List<YourType> copy = new ArrayList<YourType>(yourQueue);
if(copy.indexOf(obj1)<copy.indexOf(obj2)){
// some code here
}
Of course this is horribly inefficient, but it works. (You will probably have to synchronize the queue while doing this)
Another way would be a to do it via iterator()
:
/**
* Returns -1 if a occurs in the collection before b, 1 if b occurs before a
* and 0 otherwise.
*/
public static <T> int comparePositionInCollection(final T a,
final T b,
final Collection<T> collection){
// todo: check for a==null, b==null, a.equals(b)
final Iterator<T> iterator = collection.iterator();
boolean foundA = false;
boolean foundB = false;
int result = 0;
while(iterator.hasNext()){
final T t = iterator.next();
if(a.equals(t)){
if(foundB){
result = 1;
break;
}
foundA = true;
} else if(b.equals(t)){
if(foundA){
result = -1;
break;
}
foundB = true;
}
}
return result;
}
Of course you would have to synchronize the queue before accessing the iterator, so this is also horribly inefficient.