001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.collections4.queue;
018
019import java.util.Queue;
020
021import org.apache.commons.collections4.Transformer;
022import org.apache.commons.collections4.collection.TransformedCollection;
023
024/**
025 * Decorates another {@link Queue} to transform objects that are added.
026 * <p>
027 * The add/offer methods are affected by this class.
028 * Thus objects must be removed or searched for using their transformed form.
029 * For example, if the transformation converts Strings to Integers, you must
030 * use the Integer form to remove objects.
031 *
032 * @param <E> the type of elements held in this queue
033 * @since 4.0
034 */
035public class TransformedQueue<E> extends TransformedCollection<E> implements Queue<E> {
036
037    /** Serialization version */
038    private static final long serialVersionUID = -7901091318986132033L;
039
040    /**
041     * Factory method to create a transforming queue.
042     * <p>
043     * If there are any elements already in the queue being decorated, they
044     * are NOT transformed.
045     * Contrast this with {@link #transformedQueue(Queue, Transformer)}.
046     *
047     * @param <E> the type of the elements in the queue
048     * @param queue  the queue to decorate, must not be null
049     * @param transformer  the transformer to use for conversion, must not be null
050     * @return a new transformed Queue
051     * @throws NullPointerException if queue or transformer is null
052     */
053    public static <E> TransformedQueue<E> transformingQueue(final Queue<E> queue,
054                                                            final Transformer<? super E, ? extends E> transformer) {
055        return new TransformedQueue<>(queue, transformer);
056    }
057
058    /**
059     * Factory method to create a transforming queue that will transform
060     * existing contents of the specified queue.
061     * <p>
062     * If there are any elements already in the queue being decorated, they
063     * will be transformed by this method.
064     * Contrast this with {@link #transformingQueue(Queue, Transformer)}.
065     *
066     * @param <E> the type of the elements in the queue
067     * @param queue  the queue to decorate, must not be null
068     * @param transformer  the transformer to use for conversion, must not be null
069     * @return a new transformed Queue
070     * @throws NullPointerException if queue or transformer is null
071     * @since 4.0
072     */
073    public static <E> TransformedQueue<E> transformedQueue(final Queue<E> queue,
074                                                           final Transformer<? super E, ? extends E> transformer) {
075        // throws IAE if queue or transformer is null
076        final TransformedQueue<E> decorated = new TransformedQueue<>(queue, transformer);
077        if (queue.size() > 0) {
078            @SuppressWarnings("unchecked") // queue is type <E>
079            final E[] values = (E[]) queue.toArray(); // NOPMD - false positive for generics
080            queue.clear();
081            for (final E value : values) {
082                decorated.decorated().add(transformer.transform(value));
083            }
084        }
085        return decorated;
086    }
087
088    //-----------------------------------------------------------------------
089    /**
090     * Constructor that wraps (not copies).
091     * <p>
092     * If there are any elements already in the queue being decorated, they
093     * are NOT transformed.
094     *
095     * @param queue  the queue to decorate, must not be null
096     * @param transformer  the transformer to use for conversion, must not be null
097     * @throws NullPointerException if queue or transformer is null
098     */
099    protected TransformedQueue(final Queue<E> queue, final Transformer<? super E, ? extends E> transformer) {
100        super(queue, transformer);
101    }
102
103    /**
104     * Gets the decorated queue.
105     *
106     * @return the decorated queue
107     */
108    protected Queue<E> getQueue() {
109        return (Queue<E>) decorated();
110    }
111
112    //-----------------------------------------------------------------------
113
114    @Override
115    public boolean offer(final E obj) {
116        return getQueue().offer(transform(obj));
117    }
118
119    @Override
120    public E poll() {
121        return getQueue().poll();
122    }
123
124    @Override
125    public E peek() {
126        return getQueue().peek();
127    }
128
129    @Override
130    public E element() {
131        return getQueue().element();
132    }
133
134    @Override
135    public E remove() {
136        return getQueue().remove();
137    }
138
139}