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 <a href="http://mina.apache.org">Apache MINA Project</a>
    32   */
    33  public class SynchronizedQueue<E> implements Queue<E>, Serializable {
    34
    35      private static final long serialVersionUID = -1439242290701194806L;
    36
    37      private final Queue<E> q;
    38
    39      public SynchronizedQueue(Queue<E> q) {
    40          this.q = q;
    41      }
    42
    43      public synchronized boolean add(E e) {
    44          return q.add(e);
    45      }
    46
    47      public synchronized E element() {
    48          return q.element();
    49      }
    50
    51      public synchronized boolean offer(E e) {
    52          return q.offer(e);
    53      }
    54
    55      public synchronized E peek() {
    56          return q.peek();
    57      }
    58
    59      public synchronized E poll() {
    60          return q.poll();
    61      }
    62
    63      public synchronized E remove() {
    64          return q.remove();
    65      }
    66
    67      public synchronized boolean addAll(Collection<? extends E> c) {
    68          return q.addAll(c);
    69      }
    70
    71      public synchronized void clear() {
    72          q.clear();
    73      }
    74
    75      public synchronized boolean contains(Object o) {
    76          return q.contains(o);
    77      }
    78
    79      public synchronized boolean containsAll(Collection<?> c) {
    80          return q.containsAll(c);
    81      }
    82
    83      public synchronized boolean isEmpty() {
    84          return q.isEmpty();
    85      }
    86
    87      public synchronized Iterator<E> iterator() {
    88          return q.iterator();
    89      }
    90
    91      public synchronized boolean remove(Object o) {
    92          return q.remove(o);
    93      }
    94
    95      public synchronized boolean removeAll(Collection<?> c) {
    96          return q.removeAll(c);
    97      }
    98
    99      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 }