001: package org.jacorb.notification.queue;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 1999-2004 Gerald Brose
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: import java.util.Collections;
025: import java.util.LinkedList;
026: import java.util.List;
027:
028: import org.jacorb.notification.interfaces.Message;
029:
030: /**
031: * Note that most of the methods are not thread-safe. this causes no problem as the methods are not
032: * intended to be directly called by clients. instead the superclass implements the interface
033: * EventQueue and invokes the methods thereby synchronizing access.
034: *
035: * @author Alphonse Bendt
036: * @version $Id: BoundedFifoEventQueue.java,v 1.7 2006/02/25 15:28:40 alphonse.bendt Exp $
037: */
038:
039: public class BoundedFifoEventQueue extends AbstractBoundedEventQueue {
040: private final LinkedList linkedList_;
041:
042: public BoundedFifoEventQueue(int maxSize,
043: EventQueueOverflowStrategy overflowStrategy) {
044: this (maxSize, overflowStrategy, new Object());
045: }
046:
047: public BoundedFifoEventQueue(int maxSize,
048: EventQueueOverflowStrategy overflowStrategy, Object lock) {
049: super (maxSize, overflowStrategy, lock);
050:
051: linkedList_ = new LinkedList();
052: }
053:
054: public String getOrderPolicyName() {
055: return "FifoOrder";
056: }
057:
058: public boolean isEmpty() {
059: return linkedList_.isEmpty();
060: }
061:
062: public int getSize() {
063: return linkedList_.size();
064: }
065:
066: protected Message getEarliestTimeout() {
067: final List _sorted = (List) linkedList_.clone();
068:
069: Collections.sort(_sorted,
070: QueueUtil.ASCENDING_TIMEOUT_COMPARATOR);
071:
072: final Message _event = (Message) _sorted.get(0);
073:
074: linkedList_.remove(_event);
075:
076: return _event;
077: }
078:
079: protected Message getLeastPriority() {
080: final List _sorted = (List) linkedList_.clone();
081:
082: Collections.sort(_sorted,
083: QueueUtil.ASCENDING_PRIORITY_COMPARATOR);
084:
085: final Message _event = (Message) _sorted.get(0);
086:
087: linkedList_.remove(_event);
088:
089: return _event;
090: }
091:
092: protected Message getNextElement() {
093: return getOldestElement();
094: }
095:
096: protected Message getOldestElement() {
097: return (Message) linkedList_.removeFirst();
098: }
099:
100: protected Message getYoungestElement() {
101: return (Message) linkedList_.removeLast();
102: }
103:
104: protected Message[] getAllElements() {
105: try {
106: return (Message[]) linkedList_
107: .toArray(QueueUtil.MESSAGE_ARRAY_TEMPLATE);
108: } finally {
109: linkedList_.clear();
110: }
111: }
112:
113: protected void addElement(Message e) {
114: linkedList_.add(e);
115: }
116:
117: protected Message[] getElements(int max) {
118: final int _retSize = (max > linkedList_.size()) ? linkedList_
119: .size() : max;
120:
121: final Message[] _result = new Message[_retSize];
122:
123: for (int x = 0; x < _retSize; ++x) {
124: _result[x] = (Message) linkedList_.removeFirst();
125: }
126:
127: return _result;
128: }
129: }
|