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   * @author The Apache MINA Project (dev@mina.apache.org)
32   * @version $Rev: 662890 $, $Date: 2008-06-03 23:14:21 +0200 (mar, 03 jun 2008) $
33   */
34  public class SynchronizedQueue<E> implements Queue<E>, Serializable {
35      
36      private static final long serialVersionUID = -1439242290701194806L;
37      
38      private final Queue<E> q;
39  
40      public SynchronizedQueue(Queue<E> q) {
41          this.q = q;
42      }
43      
44      public synchronized boolean add(E e) {
45          return q.add(e);
46      }
47  
48      public synchronized E element() {
49          return q.element();
50      }
51  
52      public synchronized boolean offer(E e) {
53          return q.offer(e);
54      }
55  
56      public synchronized E peek() {
57          return q.peek();
58      }
59  
60      public synchronized E poll() {
61          return q.poll();
62      }
63  
64      public synchronized E remove() {
65          return q.remove();
66      }
67  
68      public synchronized boolean addAll(Collection<? extends E> c) {
69          return q.addAll(c);
70      }
71  
72      public synchronized void clear() {
73          q.clear();
74      }
75  
76      public synchronized boolean contains(Object o) {
77          return q.contains(o);
78      }
79  
80      public synchronized boolean containsAll(Collection<?> c) {
81          return q.containsAll(c);
82      }
83  
84      public synchronized boolean isEmpty() {
85          return q.isEmpty();
86      }
87  
88      public synchronized Iterator<E> iterator() {
89          return q.iterator();
90      }
91  
92      public synchronized boolean remove(Object o) {
93          return q.remove(o);
94      }
95  
96      public synchronized boolean removeAll(Collection<?> c) {
97          return q.removeAll(c);
98      }
99  
100     public synchronized boolean retainAll(Collection<?> c) {
101         return q.retainAll(c);
102     }
103 
104     public synchronized int size() {
105         return q.size();
106     }
107 
108     public synchronized Object[] toArray() {
109         return q.toArray();
110     }
111 
112     public synchronized <T> T[] toArray(T[] a) {
113         return q.toArray(a);
114     }
115 
116     @Override
117     public synchronized boolean equals(Object obj) {
118         return q.equals(obj);
119     }
120 
121     @Override
122     public synchronized int hashCode() {
123         return q.hashCode();
124     }
125 
126     @Override
127     public synchronized String toString() {
128         return q.toString();
129     }
130 }