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 * @since 4.0
033 * @version $Id: TransformedQueue.html 972421 2015-11-14 20:00:04Z tn $
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 IllegalArgumentException 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<E>(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 IllegalArgumentException 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<E>(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 IllegalArgumentException 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    public boolean offer(final E obj) {
115        return getQueue().offer(transform(obj));
116    }
117
118    public E poll() {
119        return getQueue().poll();
120    }
121
122    public E peek() {
123        return getQueue().peek();
124    }
125
126    public E element() {
127        return getQueue().element();
128    }
129
130    public E remove() {
131        return getQueue().remove();
132    }
133
134}