HaniiPuppy

CombinedQueue get not threadsafe

Jul 5th, 2014
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.84 KB | None | 0 0
  1. public class CombinedQueue<T> implements Queue<T> // WIP
  2. {
  3.     final Collection<Queue<T>> memberQueues;
  4.     final Lambda<T, Comparable> keyGetter;
  5.    
  6.     public T get(boolean remove)
  7.     {
  8.         // Work on a copy of memberQueues
  9.         List<Queue<T>> memberQueuesCopy;
  10.        
  11.         synchronized(memberQueues)
  12.         { memberQueuesCopy = new Arraylist<Queue<T>>(memberQueues); }
  13.  
  14.         // local variables
  15.         Queue<T> nextQueue;
  16.         T nextValue;
  17.         Comparable nextValueComparable;
  18.        
  19.         // Run through all member queues, filling those variables with the variables
  20.         // corresponding to the queue with the lowest next comparable derived via keyGetter.
  21.         for(Queue<T> i : memberQueuesCopy)
  22.         {
  23.             if(i != null && !i.isEmpty())
  24.             {
  25.                 T iValue = i.peek();
  26.                
  27.                 if(iValue == null && nextValue == null)
  28.                 {
  29.                     nextQueue = i;
  30.                     nextValue = null;
  31.                     nextValueComparable = null;
  32.                 }
  33.                 else if(iValue != null)
  34.                 {
  35.                     Comparable iValueComparable = keyGetter.getMember(iValue);
  36.                
  37.                     if((iValueComparable == null && nextQueue == null) ||
  38.                         (iValueComparable != null && iValueComparable.compareTo(nextValueComparable) < 0))
  39.                     {
  40.                         nextQueue = i;
  41.                         nextValue = iValue;
  42.                         nextValueComparable = iValueComparable;
  43.                     }
  44.                 }
  45.             }
  46.         }
  47.        
  48.         if(Queue == null)
  49.             throw new NoSuchElementException();
  50.        
  51.         if(remove)
  52.             nextQueue.remove();
  53.            
  54.         return nextValue;
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment