SynchronizedQueue.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.queue;

  18. import java.util.Queue;

  19. import org.apache.commons.collections4.collection.SynchronizedCollection;

  20. /**
  21.  * Decorates another {@link Queue} to synchronize its behavior for a multithreaded environment.
  22.  * <p>
  23.  * Methods are synchronized, then forwarded to the decorated queue. Iterators must be separately synchronized around the
  24.  * loop.
  25.  * </p>
  26.  *
  27.  * @param <E> the type of the elements in the collection
  28.  * @since 4.2
  29.  */
  30. public class SynchronizedQueue<E> extends SynchronizedCollection<E> implements Queue<E> {

  31.     /** Serialization version */
  32.     private static final long serialVersionUID = 1L;

  33.     /**
  34.      * Factory method to create a synchronized queue.
  35.      *
  36.      * @param <E>
  37.      *            the type of the elements in the queue
  38.      * @param queue
  39.      *            the queue to decorate, must not be null
  40.      * @return a new synchronized Queue
  41.      * @throws NullPointerException
  42.      *             if queue is null
  43.      */
  44.     public static <E> SynchronizedQueue<E> synchronizedQueue(final Queue<E> queue) {
  45.         return new SynchronizedQueue<>(queue);
  46.     }

  47.     /**
  48.      * Constructor that wraps (not copies).
  49.      *
  50.      * @param queue
  51.      *            the queue to decorate, must not be null
  52.      * @throws NullPointerException
  53.      *             if queue is null
  54.      */
  55.     protected SynchronizedQueue(final Queue<E> queue) {
  56.         super(queue);
  57.     }

  58.     /**
  59.      * Constructor that wraps (not copies).
  60.      *
  61.      * @param queue
  62.      *            the queue to decorate, must not be null
  63.      * @param lock
  64.      *            the lock to use, must not be null
  65.      * @throws NullPointerException
  66.      *             if queue or lock is null
  67.      */
  68.     protected SynchronizedQueue(final Queue<E> queue, final Object lock) {
  69.         super(queue, lock);
  70.     }

  71.     /**
  72.      * Gets the queue being decorated.
  73.      *
  74.      * @return the decorated queue
  75.      */
  76.     @Override
  77.     protected Queue<E> decorated() {
  78.         return (Queue<E>) super.decorated();
  79.     }

  80.     @Override
  81.     public E element() {
  82.         synchronized (lock) {
  83.             return decorated().element();
  84.         }
  85.     }

  86.     @Override
  87.     public boolean equals(final Object object) {
  88.         if (object == this) {
  89.             return true;
  90.         }
  91.         synchronized (lock) {
  92.             return decorated().equals(object);
  93.         }
  94.     }

  95.     @Override
  96.     public int hashCode() {
  97.         synchronized (lock) {
  98.             return decorated().hashCode();
  99.         }
  100.     }

  101.     @Override
  102.     public boolean offer(final E e) {
  103.         synchronized (lock) {
  104.             return decorated().offer(e);
  105.         }
  106.     }

  107.     @Override
  108.     public E peek() {
  109.         synchronized (lock) {
  110.             return decorated().peek();
  111.         }
  112.     }

  113.     @Override
  114.     public E poll() {
  115.         synchronized (lock) {
  116.             return decorated().poll();
  117.         }
  118.     }

  119.     @Override
  120.     public E remove() {
  121.         synchronized (lock) {
  122.             return decorated().remove();
  123.         }
  124.     }

  125. }