001: /*
002: * JacORB - a free Java ORB
003: *
004: * Copyright (C) 1999-2006 Gerald Brose
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Library General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Library General Public License for more details.
015: *
016: * You should have received a copy of the GNU Library General Public
017: * License along with this library; if not, write to the Free
018: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
019: *
020: */
021:
022: package org.jacorb.notification.queue;
023:
024: import java.util.ArrayList;
025: import java.util.Collections;
026: import java.util.Comparator;
027: import java.util.List;
028:
029: import org.jacorb.notification.interfaces.Message;
030:
031: import edu.emory.mathcs.backport.java.util.PriorityQueue;
032:
033: public abstract class AbstractBoundedEventHeap extends
034: AbstractBoundedEventQueue {
035: private final PriorityQueue heap_;
036:
037: protected AbstractBoundedEventHeap(int capacity,
038: EventQueueOverflowStrategy overflowStrategy, Object lock,
039: Comparator comparator) {
040: super (capacity, overflowStrategy, lock);
041:
042: heap_ = new PriorityQueue(capacity, comparator);
043: }
044:
045: private final List copyAllEntries() {
046: final List _events = new ArrayList(heap_.size());
047:
048: _events.addAll(heap_);
049:
050: return _events;
051: }
052:
053: private final List removeAllEntries() {
054: final List _entries = copyAllEntries();
055:
056: heap_.clear();
057:
058: return _entries;
059: }
060:
061: protected final Message removeFirstElement(Comparator comp) {
062: final List _entries = copyAllEntries();
063: Collections.sort(_entries, comp);
064:
065: final Message _mesg = (Message) _entries.remove(0);
066:
067: heap_.clear();
068: heap_.addAll(_entries);
069:
070: return _mesg;
071: }
072:
073: protected final Message[] getAllElements() {
074: final List _entries = removeAllEntries();
075:
076: return (Message[]) _entries
077: .toArray(new Message[_entries.size()]);
078: }
079:
080: public final boolean isEmpty() {
081: return getSize() == 0;
082: }
083:
084: public final int getSize() {
085: return heap_.size();
086: }
087:
088: protected final void addElement(Message event) {
089: heap_.add(event);
090: }
091:
092: protected final Message[] getElements(int max) {
093: final List _result = new ArrayList();
094:
095: while ((heap_.peek()) != null && (_result.size() < max)) {
096: _result.add(heap_.remove());
097: }
098:
099: return (Message[]) _result
100: .toArray(QueueUtil.MESSAGE_ARRAY_TEMPLATE);
101: }
102:
103: protected final Message getNextHeapElement() {
104: return (Message) heap_.remove();
105: }
106:
107: protected final Message getNextElement() {
108: return getNextHeapElement();
109: }
110:
111: public final String toString() {
112: return heap_.toString();
113: }
114: }
|