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.Transformer;
22 import org.apache.commons.collections4.collection.TransformedCollection;
23
24 /**
25 * Decorates another {@link Queue} to transform objects that are added.
26 * <p>
27 * The add/offer methods are affected by this class.
28 * Thus objects must be removed or searched for using their transformed form.
29 * For example, if the transformation converts Strings to Integers, you must
30 * use the Integer form to remove objects.
31 * </p>
32 *
33 * @param <E> the type of elements held in this queue
34 * @since 4.0
35 */
36 public class TransformedQueue<E> extends TransformedCollection<E> implements Queue<E> {
37
38 /** Serialization version */
39 private static final long serialVersionUID = -7901091318986132033L;
40
41 /**
42 * Factory method to create a transforming queue that will transform
43 * existing contents of the specified queue.
44 * <p>
45 * If there are any elements already in the queue being decorated, they
46 * will be transformed by this method.
47 * Contrast this with {@link #transformingQueue(Queue, Transformer)}.
48 *
49 * @param <E> the type of the elements in the queue
50 * @param queue the queue to decorate, must not be null
51 * @param transformer the transformer to use for conversion, must not be null
52 * @return a new transformed Queue
53 * @throws NullPointerException if queue or transformer is null
54 * @since 4.0
55 */
56 public static <E> TransformedQueue<E> transformedQueue(final Queue<E> queue,
57 final Transformer<? super E, ? extends E> transformer) {
58 // throws IAE if queue or transformer is null
59 final TransformedQueue<E> decorated = new TransformedQueue<>(queue, transformer);
60 if (!queue.isEmpty()) {
61 @SuppressWarnings("unchecked") // queue is type <E>
62 final E[] values = (E[]) queue.toArray(); // NOPMD - false positive for generics
63 queue.clear();
64 for (final E value : values) {
65 decorated.decorated().add(transformer.apply(value));
66 }
67 }
68 return decorated;
69 }
70
71 /**
72 * Factory method to create a transforming queue.
73 * <p>
74 * If there are any elements already in the queue being decorated, they
75 * are NOT transformed.
76 * Contrast this with {@link #transformedQueue(Queue, Transformer)}.
77 *
78 * @param <E> the type of the elements in the queue
79 * @param queue the queue to decorate, must not be null
80 * @param transformer the transformer to use for conversion, must not be null
81 * @return a new transformed Queue
82 * @throws NullPointerException if queue or transformer is null
83 */
84 public static <E> TransformedQueue<E> transformingQueue(final Queue<E> queue,
85 final Transformer<? super E, ? extends E> transformer) {
86 return new TransformedQueue<>(queue, transformer);
87 }
88
89 /**
90 * Constructor that wraps (not copies).
91 * <p>
92 * If there are any elements already in the queue being decorated, they
93 * are NOT transformed.
94 *
95 * @param queue the queue to decorate, must not be null
96 * @param transformer the transformer to use for conversion, must not be null
97 * @throws NullPointerException if queue or transformer is null
98 */
99 protected TransformedQueue(final Queue<E> queue, final Transformer<? super E, ? extends E> transformer) {
100 super(queue, transformer);
101 }
102
103 @Override
104 public E element() {
105 return getQueue().element();
106 }
107
108 /**
109 * Gets the decorated queue.
110 *
111 * @return the decorated queue
112 */
113 protected Queue<E> getQueue() {
114 return (Queue<E>) decorated();
115 }
116
117 @Override
118 public boolean offer(final E obj) {
119 return getQueue().offer(transform(obj));
120 }
121
122 @Override
123 public E peek() {
124 return getQueue().peek();
125 }
126
127 @Override
128 public E poll() {
129 return getQueue().poll();
130 }
131
132 @Override
133 public E remove() {
134 return getQueue().remove();
135 }
136
137 }