1 package org.jacorb.notification.queue; 2 3 23 24 import java.util.Collections ; 25 import java.util.LinkedList ; 26 import java.util.List ; 27 28 import org.jacorb.notification.interfaces.Message; 29 30 38 39 public class BoundedFifoEventQueue extends AbstractBoundedEventQueue 40 { 41 private final LinkedList linkedList_; 42 43 public BoundedFifoEventQueue(int maxSize, EventQueueOverflowStrategy overflowStrategy) 44 { 45 this(maxSize, overflowStrategy, new Object ()); 46 } 47 48 public BoundedFifoEventQueue(int maxSize, EventQueueOverflowStrategy overflowStrategy, 49 Object lock) 50 { 51 super(maxSize, overflowStrategy, lock); 52 53 linkedList_ = new LinkedList (); 54 } 55 56 public boolean isEmpty() 57 { 58 return linkedList_.isEmpty(); 59 } 60 61 public int getSize() 62 { 63 return linkedList_.size(); 64 } 65 66 protected Message getEarliestTimeout() 67 { 68 List _sorted = (List ) linkedList_.clone(); 69 70 Collections.sort(_sorted, QueueUtil.ASCENDING_TIMEOUT_COMPARATOR); 71 72 Message _event = (Message) _sorted.get(0); 73 74 linkedList_.remove(_event); 75 76 return _event; 77 } 78 79 protected Message getLeastPriority() 80 { 81 List _sorted = (List ) linkedList_.clone(); 82 83 Collections.sort(_sorted, QueueUtil.ASCENDING_PRIORITY_COMPARATOR); 84 85 Message _event = (Message) _sorted.get(0); 86 87 linkedList_.remove(_event); 88 89 return _event; 90 } 91 92 protected Message getNextElement() 93 { 94 return getOldestElement(); 95 } 96 97 protected Message getOldestElement() 98 { 99 return (Message) linkedList_.removeFirst(); 100 } 101 102 protected Message getYoungestElement() 103 { 104 return (Message) linkedList_.removeLast(); 105 } 106 107 protected Message[] getAllElements() 108 { 109 try 110 { 111 return (Message[]) linkedList_.toArray(QueueUtil.MESSAGE_ARRAY_TEMPLATE); 112 } finally 113 { 114 linkedList_.clear(); 115 } 116 } 117 118 protected void addElement(Message e) 119 { 120 linkedList_.add(e); 121 } 122 123 124 protected Message[] getElements(int max) 125 { 126 int _retSize = (max > linkedList_.size()) ? linkedList_.size() : max; 127 128 Message[] _ret = new Message[_retSize]; 129 130 for (int x = 0; x < _retSize; ++x) 131 { 132 _ret[x] = (Message) linkedList_.removeFirst(); 133 } 134 135 return _ret; 136 } 137 } 138 | Popular Tags |