View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.mina.util;
21  
22  import java.io.Serializable;
23  import java.util.Collection;
24  import java.util.Iterator;
25  import java.util.Queue;
26  
27  /**
28   * A decorator that makes the specified {@link Queue} thread-safe.
29   * Like any other synchronizing wrappers, iteration is not thread-safe.
30   * 
31   * @param <E> The type of elements stored in the queue
32   * 
33   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
34   */
35  public class SynchronizedQueue<E> implements Queue<E>, Serializable {
36  
37      private static final long serialVersionUID = -1439242290701194806L;
38  
39      private final Queue<E> queue;
40  
41      /**
42       * Create a new SynchronizedQueue instance
43       * 
44       * @param queue The queue
45       */
46      public SynchronizedQueue(Queue<E> queue) {
47          this.queue = queue;
48      }
49  
50      /**
51       * {@inheritDoc}
52       */
53      @Override
54      public synchronized boolean add(E e) {
55          return queue.add(e);
56      }
57  
58      /**
59       * {@inheritDoc}
60       */
61      @Override
62      public synchronized E element() {
63          return queue.element();
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      @Override
70      public synchronized boolean offer(E e) {
71          return queue.offer(e);
72      }
73  
74      /**
75       * {@inheritDoc}
76       */
77      @Override
78      public synchronized E peek() {
79          return queue.peek();
80      }
81  
82      /**
83       * {@inheritDoc}
84       */
85      @Override
86      public synchronized E poll() {
87          return queue.poll();
88      }
89  
90      /**
91       * {@inheritDoc}
92       */
93      @Override
94      public synchronized E remove() {
95          return queue.remove();
96      }
97  
98      /**
99       * {@inheritDoc}
100      */
101     @Override
102     public synchronized boolean addAll(Collection<? extends E> c) {
103         return queue.addAll(c);
104     }
105 
106     /**
107      * {@inheritDoc}
108      */
109     @Override
110     public synchronized void clear() {
111         queue.clear();
112     }
113 
114     /**
115      * {@inheritDoc}
116      */
117     @Override
118     public synchronized boolean contains(Object o) {
119         return queue.contains(o);
120     }
121 
122     /**
123      * {@inheritDoc}
124      */
125     @Override
126     public synchronized boolean containsAll(Collection<?> c) {
127         return queue.containsAll(c);
128     }
129 
130     /**
131      * {@inheritDoc}
132      */
133     @Override
134     public synchronized boolean isEmpty() {
135         return queue.isEmpty();
136     }
137 
138     /**
139      * {@inheritDoc}
140      */
141     @Override
142     public synchronized Iterator<E> iterator() {
143         return queue.iterator();
144     }
145 
146     /**
147      * {@inheritDoc}
148      */
149     @Override
150     public synchronized boolean remove(Object o) {
151         return queue.remove(o);
152     }
153 
154     /**
155      * {@inheritDoc}
156      */
157     @Override
158     public synchronized boolean removeAll(Collection<?> c) {
159         return queue.removeAll(c);
160     }
161 
162     /**
163      * {@inheritDoc}
164      */
165     @Override
166     public synchronized boolean retainAll(Collection<?> c) {
167         return queue.retainAll(c);
168     }
169 
170     /**
171      * {@inheritDoc}
172      */
173     @Override
174     public synchronized int size() {
175         return queue.size();
176     }
177 
178     /**
179      * {@inheritDoc}
180      */
181     @Override
182     public synchronized Object[] toArray() {
183         return queue.toArray();
184     }
185 
186     /**
187      * {@inheritDoc}
188      */
189     @Override
190     public synchronized <T> T[] toArray(T[] a) {
191         return queue.toArray(a);
192     }
193 
194     /**
195      * {@inheritDoc}
196      */
197     @Override
198     public synchronized boolean equals(Object obj) {
199         return queue.equals(obj);
200     }
201 
202     /**
203      * {@inheritDoc}
204      */
205     @Override
206     public synchronized int hashCode() {
207         return queue.hashCode();
208     }
209 
210     /**
211      * {@inheritDoc}
212      */
213     @Override
214     public synchronized String toString() {
215         return queue.toString();
216     }
217 }