KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > queue > BoundedFifoEventQueue


1 package org.jacorb.notification.queue;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1999-2004 Gerald Brose
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */

23
24 import java.util.Collections JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.jacorb.notification.interfaces.Message;
29
30 /**
31  * Note that most of the methods are not thread-safe. this causes no problem as the methods are not
32  * intended to be directly called by clients. instead the superclass implements the interface
33  * EventQueue and invokes the methods thereby synchronizing access.
34  *
35  * @author Alphonse Bendt
36  * @version $Id: BoundedFifoEventQueue.java,v 1.5 2005/05/01 21:52:41 alphonse.bendt Exp $
37  */

38
39 public class BoundedFifoEventQueue extends AbstractBoundedEventQueue
40 {
41     private final LinkedList JavaDoc linkedList_;
42
43     public BoundedFifoEventQueue(int maxSize, EventQueueOverflowStrategy overflowStrategy)
44     {
45         this(maxSize, overflowStrategy, new Object JavaDoc());
46     }
47
48     public BoundedFifoEventQueue(int maxSize, EventQueueOverflowStrategy overflowStrategy,
49             Object JavaDoc lock)
50     {
51         super(maxSize, overflowStrategy, lock);
52
53         linkedList_ = new LinkedList JavaDoc();
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 JavaDoc _sorted = (List JavaDoc) 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 JavaDoc _sorted = (List JavaDoc) 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