View Javadoc
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.AbstractCollectionDecorator;
22  
23  /**
24   * Decorates another {@link Queue} to provide additional behavior.
25   * <p>
26   * Methods are forwarded directly to the decorated queue.
27   * </p>
28   * <p>
29   * This implementation does not forward the hashCode and equals methods through
30   * to the backing object, but relies on Object's implementation. This is
31   * necessary as some Queue implementations, e.g. LinkedList, have a custom
32   * equals implementation for which symmetry can not be preserved.
33   * See class Javadoc of AbstractCollectionDecorator for more information.
34   * </p>
35   *
36   * @param <E> the type of the elements in the queue
37   * @since 4.0
38   */
39  public abstract class AbstractQueueDecorator<E> extends AbstractCollectionDecorator<E>
40          implements Queue<E> {
41  
42      /** Serialization version */
43      private static final long serialVersionUID = -2629815475789577029L;
44  
45      /**
46       * Constructor only used in deserialization, do not use otherwise.
47       */
48      protected AbstractQueueDecorator() {
49      }
50  
51      /**
52       * Constructor that wraps (not copies).
53       *
54       * @param queue  the queue to decorate, must not be null
55       * @throws NullPointerException if queue is null
56       */
57      protected AbstractQueueDecorator(final Queue<E> queue) {
58          super(queue);
59      }
60  
61      /**
62       * Gets the queue being decorated.
63       *
64       * @return the decorated queue
65       */
66      @Override
67      protected Queue<E> decorated() {
68          return (Queue<E>) super.decorated();
69      }
70  
71      @Override
72      public E element() {
73          return decorated().element();
74      }
75  
76      @Override
77      public boolean offer(final E obj) {
78          return decorated().offer(obj);
79      }
80  
81      @Override
82      public E peek() {
83          return decorated().peek();
84      }
85  
86      @Override
87      public E poll() {
88          return decorated().poll();
89      }
90  
91      @Override
92      public E remove() {
93          return decorated().remove();
94      }
95  
96  }