001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.apache.mina.util;
021:
022: import java.io.Serializable;
023: import java.util.Collection;
024: import java.util.Iterator;
025: import java.util.Queue;
026:
027: /**
028: * A decorator that makes the specified {@link Queue} thread-safe.
029: * Like any other synchronizing wrappers, iteration is not thread-safe.
030: *
031: * @author The Apache Directory Project (mina-dev@directory.apache.org)
032: * @version $Rev: 591948 $, $Date: 2007-11-05 03:19:56 -0700 (Mon, 05 Nov 2007) $
033: */
034: public class SynchronizedQueue<E> implements Queue<E>, Serializable {
035:
036: private static final long serialVersionUID = -1439242290701194806L;
037:
038: private final Queue<E> q;
039:
040: public SynchronizedQueue(Queue<E> q) {
041: this .q = q;
042: }
043:
044: public synchronized boolean add(E e) {
045: return q.add(e);
046: }
047:
048: public synchronized E element() {
049: return q.element();
050: }
051:
052: public synchronized boolean offer(E e) {
053: return q.offer(e);
054: }
055:
056: public synchronized E peek() {
057: return q.peek();
058: }
059:
060: public synchronized E poll() {
061: return q.poll();
062: }
063:
064: public synchronized E remove() {
065: return q.remove();
066: }
067:
068: public synchronized boolean addAll(Collection<? extends E> c) {
069: return q.addAll(c);
070: }
071:
072: public synchronized void clear() {
073: q.clear();
074: }
075:
076: public synchronized boolean contains(Object o) {
077: return q.contains(o);
078: }
079:
080: public synchronized boolean containsAll(Collection<?> c) {
081: return q.containsAll(c);
082: }
083:
084: public synchronized boolean isEmpty() {
085: return q.isEmpty();
086: }
087:
088: public synchronized Iterator<E> iterator() {
089: return q.iterator();
090: }
091:
092: public synchronized boolean remove(Object o) {
093: return q.remove(o);
094: }
095:
096: public synchronized boolean removeAll(Collection<?> c) {
097: return q.removeAll(c);
098: }
099:
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: }
|