Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CombinedQueue<T> implements Queue<T> // WIP
- {
- final Collection<Queue<T>> memberQueues;
- final Lambda<T, Comparable> keyGetter;
- public T get(boolean remove)
- {
- // Work on a copy of memberQueues
- List<Queue<T>> memberQueuesCopy;
- synchronized(memberQueues)
- { memberQueuesCopy = new Arraylist<Queue<T>>(memberQueues); }
- // local variables
- Queue<T> nextQueue;
- T nextValue;
- Comparable nextValueComparable;
- // Run through all member queues, filling those variables with the variables
- // corresponding to the queue with the lowest next comparable derived via keyGetter.
- for(Queue<T> i : memberQueuesCopy)
- {
- if(i != null && !i.isEmpty())
- {
- T iValue = i.peek();
- if(iValue == null && nextValue == null)
- {
- nextQueue = i;
- nextValue = null;
- nextValueComparable = null;
- }
- else if(iValue != null)
- {
- Comparable iValueComparable = keyGetter.getMember(iValue);
- if((iValueComparable == null && nextQueue == null) ||
- (iValueComparable != null && iValueComparable.compareTo(nextValueComparable) < 0))
- {
- nextQueue = i;
- nextValue = iValue;
- nextValueComparable = iValueComparable;
- }
- }
- }
- }
- if(Queue == null)
- throw new NoSuchElementException();
- if(remove)
- nextQueue.remove();
- return nextValue;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment