I would like to store tuple objects in a collection of matching java, and then have an efficient, blocking query method that returns the first element matching the pattern. If such an element is not available, it is blocked until such an element appears.
For example, if I have a class:
public class Pair { public final String first; public final String Second; public Pair( String first, String second ) { this.first = first; this.second = second; } }
And a collection like:
public class FunkyCollection { public void add( Pair p ) { } public Pair get( Pair p ) { } }
I would like to request it as:
myFunkyCollection.get( new Pair( null, "foo" ) );
which returns the first available pair with the second field equal to "foo", or blocks until this element is added. Another example request:
myFunkyCollection.get( new Pair( null, null ) );
must return the first available pair regardless of its values.
Does a solution already exist? If this is not the case, what do you suggest implementing the get( Pair p ) method?
Clarification: The get( Pair p) method should also remove the item. The choice of name was not very smart. The best name would be take( ... ) .
source share