1 /*******************************************************************************
2 * Copyright 2012 Fat Cat
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 ******************************************************************************/
16 package com
.assembla
.ariane
.jellyfish
.util
.collections
.queue
;
18 import java
.util
.Iterator
;
19 import java
.util
.NoSuchElementException
;
20 import java
.util
.concurrent
.BlockingQueue
;
21 import java
.util
.concurrent
.LinkedBlockingQueue
;
22 import java
.util
.concurrent
.TimeUnit
;
23 import org
.slf4j
.Logger
;
24 import org
.slf4j
.LoggerFactory
;
27 * Class that represents an queue of requests.
29 * @param <Request> The request type.
33 public class RequestQueue
< Request
>
35 /** An list of messages to process. */
36 private final BlockingQueue
< Request
> sender
;
38 /** The size of the request queue.*/
41 /** The logger to use. */
42 private Logger logger
;
45 * Creates a new request queue given it's size.
46 * @param size The size of the queue.
48 public RequestQueue( final int size
)
50 this.sender
= new LinkedBlockingQueue
< Request
>( size
);
52 this.logger
= LoggerFactory
.getLogger( RequestQueue
.class );
56 * Adds a new request to the queue.
57 * @param request The request to add
58 * @throws IllegalStateException Exception raised if the request could not be
59 * added due to problems with the queue size.
61 public void add( final Request request
) throws IllegalStateException
63 this.logger
.debug( "Adding a new request." );
64 if ( !this.sender
.contains( request
) )
66 this.sender
.add( request
);
71 * Clears the request queue.
75 this.logger
.debug( "Clearing all the requests." );
80 * Checks if the queue contains the given request.
81 * @param request the request to check for.
82 * @return true, if successful.
84 public boolean contains( final Request request
)
86 return this.sender
.contains( request
);
90 * Retrieves the element at the head of this queue, without removing it.
91 * @return The head element of this queue.
93 public Request
element( )
95 return this.sender
.element( );
99 * Checks if the requests queue is empty.
100 * @return true, if is empty.
102 public boolean isEmpty( )
104 return this.sender
.isEmpty( );
108 * Returns the iterator to iterate the request queue.
109 * @return The requests queue iterator.
111 public Iterator
< Request
> iterator( )
113 return this.sender
.iterator( );
117 * Adds an element to this queue and returns whether or not is was added.
118 * @param e The request to add.
119 * @return true, if successful.
121 public boolean offer( final Request e
)
123 this.logger
.debug( "Adding a new request." );
124 return this.sender
.offer( e
);
128 * Adds the element to this queue and waits until the element
129 * is successfully inserted.
131 * @param e The request to add.
132 * @param timeout The maximum waiting time.
133 * @param unit The unit of the timeout value.
134 * @return true, if successful.
135 * @throws InterruptedException If an interrupted exception occurred.
137 public boolean offer( final Request e
, final long timeout
, final TimeUnit unit
) throws InterruptedException
139 return this.sender
.offer( e
, timeout
, unit
);
143 * Retrieves the head of the queue or returns null if it does not exist.
144 * @return The request in the head of this queue.
146 public Request
peek( )
148 return this.sender
.peek( );
152 * Retrieves and removes the head of this queue if the queue is not empty.
153 * @return The request in the head of this queue or null is the queue is empty.
155 public Request
poll( )
157 this.logger
.debug( "Removing a request." );
158 return this.sender
.poll( );
162 * Retrieves and removes the head of this queue, waiting the
163 * specified time until the request is available.
165 * @param timeout The maximum waiting time.
166 * @param unit The unit of the timeout value.
167 * @return The request in the head or null if the queue is empty.
168 * @throws InterruptedException If an interrupted exception occurred.
170 public Request
poll( final long timeout
, final TimeUnit unit
) throws InterruptedException
172 return this.sender
.poll( timeout
, unit
);
176 * Inserts the given request while waiting for space to be available.
177 * @param e The request to add.
178 * @throws InterruptedException If an interrupted exception occurred.
180 public void put( final Request e
) throws InterruptedException
182 this.logger
.debug( "Adding a request." );
183 this.sender
.put( e
);
187 * Returns the number of requests that can be added without
188 * violating the size dimension of this queue.
189 * @return The number of remaining requests.
191 public int remainingCapacity( )
193 return this.sender
.remainingCapacity( );
197 * Retrieves and removes the request at the head of the queue.
198 * @return The request at the head.
199 * @throws NoSuchElementException If there is no available request.
201 public Request
remove( ) throws NoSuchElementException
203 this.logger
.debug( "Removing a request." );
204 return this.sender
.remove( );
208 * Returns the number of available requests.
209 * @return The number of available requests.
213 return this.sender
.size( );
217 * Retrieves and removes the request at the head of the queue,
218 * waiting the amount of time until the operation is successful.
220 * @return The request at the head of the queue.
221 * @throws InterruptedException If an interrupted exception occurred.
223 public Request
take( ) throws InterruptedException
225 this.logger
.debug( "Removing a request." );
226 return this.sender
.take( );
230 * Returns the maximum number of requests in this queue.
231 * @return The maximum number of queue requests.
233 public int getMaxSize( )