KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > queue > BufferImpl


1 /**
2  * Dream
3  * Copyright (C) 2003-2004 INRIA Rhone-Alpes
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: dream@objectweb.org
20  *
21  * Initial developer(s): Vivien Quema
22  * Contributor(s):
23  */

24
25 package org.objectweb.dream.queue;
26
27 import org.objectweb.dream.message.Message;
28 import org.objectweb.dream.message.manager.MessageManager;
29 import org.objectweb.fractal.api.NoSuchInterfaceException;
30 import org.objectweb.fractal.api.control.IllegalBindingException;
31 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
32
33 /**
34  * Basic implementation of the <code>Buffer</code> interface using a
35  * <code>LinkedList</code>. This buffer does not sort messages. They are
36  * added and removed in a FIFO order.
37  * <p>
38  * <i>Indicators </i> on available space, available messages, and stored
39  * messages are expressed as number of messages.
40  *
41  * @see Buffer
42  * @see AbstractBufferImpl
43  */

44 public class BufferImpl extends AbstractBufferImpl
45 {
46
47   ListAddFirstLast listAddFirstLastItf;
48   ListRemoveFirstLast listRemoveFirstLastItf;
49   List listItf;
50
51   // ---------------------------------------------------------------------------
52
// Implemented methods.
53
// ---------------------------------------------------------------------------
54

55   /**
56    * @see org.objectweb.dream.queue.AbstractBufferImpl#hasAvailableMessage()
57    */

58   protected boolean hasAvailableMessage()
59   {
60     return (availableMessagesIndicator > 0);
61   }
62
63   /**
64    * @see org.objectweb.dream.queue.AbstractBufferImpl#canAdd(org.objectweb.dream.message.Message)
65    */

66   protected boolean canAdd(Message message)
67   {
68     if (maxCapacity > 0 && (maxCapacity - storedMessagesIndicator <= 0))
69     {
70       return false;
71     }
72     return true;
73   }
74
75   // ---------------------------------------------------------------------------
76
// Implemented methods for the Buffer interface.
77
// ---------------------------------------------------------------------------
78

79   /**
80    * @see org.objectweb.dream.queue.AbstractBufferImpl#doAdd(org.objectweb.dream.message.Message)
81    */

82   protected void doAdd(Message message)
83   {
84     listItf.add(message);
85     incrementAvailableMessagesIndicator(1);
86     incrementStoredMessagesIndicator(1);
87   }
88
89   /**
90    * @see org.objectweb.dream.queue.AbstractBufferImpl#doRemove()
91    */

92   protected Message doRemove()
93   {
94     incrementAvailableMessagesIndicator(-1);
95     incrementStoredMessagesIndicator(-1);
96     return (Message) listRemoveFirstLastItf.removeFirst();
97   }
98
99   /**
100    * @see org.objectweb.dream.queue.AbstractBufferImpl#doGet()
101    */

102   protected Message doGet()
103   {
104     return (Message) listRemoveFirstLastItf.getFirst();
105   }
106
107   // ---------------------------------------------------------------------------
108
// Implemented methods for the BufferAddFirstLast interface.
109
// ---------------------------------------------------------------------------
110

111   /**
112    * @see org.objectweb.dream.queue.AbstractBufferImpl#doAddFirst(org.objectweb.dream.message.Message)
113    */

114   protected void doAddFirst(Message message)
115   {
116     listAddFirstLastItf.addFirst(message);
117     incrementAvailableMessagesIndicator(1);
118     incrementStoredMessagesIndicator(1);
119   }
120
121   /**
122    * @see org.objectweb.dream.queue.AbstractBufferImpl#doAddLast(org.objectweb.dream.message.Message)
123    */

124   protected void doAddLast(Message message)
125   {
126     listAddFirstLastItf.addLast(message);
127     incrementAvailableMessagesIndicator(1);
128     incrementStoredMessagesIndicator(1);
129   }
130
131   // ---------------------------------------------------------------------------
132
// Implemented methods for the BufferRemoveFirstLast interface.
133
// ---------------------------------------------------------------------------
134

135   /**
136    * @see org.objectweb.dream.queue.AbstractBufferImpl#doGetFirst()
137    */

138   protected Message doGetFirst()
139   {
140     return (Message) listRemoveFirstLastItf.getFirst();
141   }
142
143   /**
144    * @see org.objectweb.dream.queue.AbstractBufferImpl#doGetLast()
145    */

146   protected Message doGetLast()
147   {
148     return (Message) listRemoveFirstLastItf.getLast();
149   }
150
151   /**
152    * @see org.objectweb.dream.queue.AbstractBufferImpl#doRemoveFirst()
153    */

154   protected Message doRemoveFirst()
155   {
156     incrementAvailableMessagesIndicator(-1);
157     incrementStoredMessagesIndicator(-1);
158     return (Message) listRemoveFirstLastItf.removeFirst();
159   }
160
161   /**
162    * @see org.objectweb.dream.queue.AbstractBufferImpl#doRemoveLast()
163    */

164   protected Message doRemoveLast()
165   {
166     incrementAvailableMessagesIndicator(-1);
167     incrementStoredMessagesIndicator(-1);
168     return (Message) listRemoveFirstLastItf.removeLast();
169   }
170
171   // ---------------------------------------------------------------------------
172
// Implementation of the BindingController interface.
173
// ---------------------------------------------------------------------------
174

175   /**
176    * @see org.objectweb.fractal.api.control.BindingController#bindFc(String,
177    * Object)
178    */

179   public synchronized void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf)
180       throws NoSuchInterfaceException, IllegalBindingException,
181       IllegalLifeCycleException
182   {
183     super.bindFc(clientItfName, serverItf);
184     if (clientItfName.equals(List.ITF_NAME))
185     {
186       listItf = (List) serverItf;
187     }
188     else if (clientItfName.equals(ListAddFirstLast.ITF_NAME))
189     {
190       listAddFirstLastItf = (ListAddFirstLast) serverItf;
191     }
192     else if (clientItfName.equals(ListRemoveFirstLast.ITF_NAME))
193     {
194       listRemoveFirstLastItf = (ListRemoveFirstLast) serverItf;
195     }
196   }
197
198   /**
199    * @see org.objectweb.fractal.api.control.BindingController#listFc()
200    */

201   public String JavaDoc[] listFc()
202   {
203     return new String JavaDoc[]{MessageManager.ITF_NAME, ListAddFirstLast.ITF_NAME,
204         ListRemoveFirstLast.ITF_NAME, List.ITF_NAME};
205   }
206
207 }
Popular Tags