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 */
020package org.apache.mina.util;
021
022import java.io.Serializable;
023import java.util.Collection;
024import java.util.Iterator;
025import 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 <a href="http://mina.apache.org">Apache MINA Project</a>
032 */
033public class SynchronizedQueue<E> implements Queue<E>, Serializable {
034
035    private static final long serialVersionUID = -1439242290701194806L;
036
037    private final Queue<E> q;
038
039    public SynchronizedQueue(Queue<E> q) {
040        this.q = q;
041    }
042
043    public synchronized boolean add(E e) {
044        return q.add(e);
045    }
046
047    public synchronized E element() {
048        return q.element();
049    }
050
051    public synchronized boolean offer(E e) {
052        return q.offer(e);
053    }
054
055    public synchronized E peek() {
056        return q.peek();
057    }
058
059    public synchronized E poll() {
060        return q.poll();
061    }
062
063    public synchronized E remove() {
064        return q.remove();
065    }
066
067    public synchronized boolean addAll(Collection<? extends E> c) {
068        return q.addAll(c);
069    }
070
071    public synchronized void clear() {
072        q.clear();
073    }
074
075    public synchronized boolean contains(Object o) {
076        return q.contains(o);
077    }
078
079    public synchronized boolean containsAll(Collection<?> c) {
080        return q.containsAll(c);
081    }
082
083    public synchronized boolean isEmpty() {
084        return q.isEmpty();
085    }
086
087    public synchronized Iterator<E> iterator() {
088        return q.iterator();
089    }
090
091    public synchronized boolean remove(Object o) {
092        return q.remove(o);
093    }
094
095    public synchronized boolean removeAll(Collection<?> c) {
096        return q.removeAll(c);
097    }
098
099    public synchronized boolean retainAll(Collection<?> c) {
100        return q.retainAll(c);
101    }
102
103    public synchronized int size() {
104        return q.size();
105    }
106
107    public synchronized Object[] toArray() {
108        return q.toArray();
109    }
110
111    public synchronized <T> T[] toArray(T[] a) {
112        return q.toArray(a);
113    }
114
115    @Override
116    public synchronized boolean equals(Object obj) {
117        return q.equals(obj);
118    }
119
120    @Override
121    public synchronized int hashCode() {
122        return q.hashCode();
123    }
124
125    @Override
126    public synchronized String toString() {
127        return q.toString();
128    }
129}