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
19 import java.util.Queue;
20
21 import org.apache.commons.collections4.collection.SynchronizedCollection;
22
23 /**
24 * Decorates another {@link Queue} to synchronize its behavior for a multithreaded environment.
25 * <p>
26 * Methods are synchronized, then forwarded to the decorated queue. Iterators must be separately synchronized around the
27 * loop.
28 * </p>
29 *
30 * @param <E> the type of the elements in the collection
31 * @since 4.2
32 */
33 public class SynchronizedQueue<E> extends SynchronizedCollection<E> implements Queue<E> {
34
35 /** Serialization version */
36 private static final long serialVersionUID = 1L;
37
38 /**
39 * Factory method to create a synchronized queue.
40 *
41 * @param <E>
42 * the type of the elements in the queue
43 * @param queue
44 * the queue to decorate, must not be null
45 * @return a new synchronized Queue
46 * @throws NullPointerException
47 * if queue is null
48 */
49 public static <E> SynchronizedQueue<E> synchronizedQueue(final Queue<E> queue) {
50 return new SynchronizedQueue<>(queue);
51 }
52
53 /**
54 * Constructor that wraps (not copies).
55 *
56 * @param queue
57 * the queue to decorate, must not be null
58 * @throws NullPointerException
59 * if queue is null
60 */
61 protected SynchronizedQueue(final Queue<E> queue) {
62 super(queue);
63 }
64
65 /**
66 * Constructor that wraps (not copies).
67 *
68 * @param queue
69 * the queue to decorate, must not be null
70 * @param lock
71 * the lock to use, must not be null
72 * @throws NullPointerException
73 * if queue or lock is null
74 */
75 protected SynchronizedQueue(final Queue<E> queue, final Object lock) {
76 super(queue, lock);
77 }
78
79 /**
80 * Gets the queue being decorated.
81 *
82 * @return the decorated queue
83 */
84 @Override
85 protected Queue<E> decorated() {
86 return (Queue<E>) super.decorated();
87 }
88
89 @Override
90 public E element() {
91 synchronized (lock) {
92 return decorated().element();
93 }
94 }
95
96 @Override
97 public boolean equals(final Object object) {
98 if (object == this) {
99 return true;
100 }
101 synchronized (lock) {
102 return decorated().equals(object);
103 }
104 }
105
106 @Override
107 public int hashCode() {
108 synchronized (lock) {
109 return decorated().hashCode();
110 }
111 }
112
113 @Override
114 public boolean offer(final E e) {
115 synchronized (lock) {
116 return decorated().offer(e);
117 }
118 }
119
120 @Override
121 public E peek() {
122 synchronized (lock) {
123 return decorated().peek();
124 }
125 }
126
127 @Override
128 public E poll() {
129 synchronized (lock) {
130 return decorated().poll();
131 }
132 }
133
134 @Override
135 public E remove() {
136 synchronized (lock) {
137 return decorated().remove();
138 }
139 }
140
141 }