    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 * </p>
    032 *
    033 * @param <E> the type of elements held in this queue
    034 * @since 4.0
    035 */
    036public class TransformedQueue<E> extends TransformedCollection<E> implements Queue<E> {
    037
    038    /** Serialization version */
    039    private static final long serialVersionUID = -7901091318986132033L;
    040
    041    /**
    042     * Factory method to create a transforming queue that will transform
    043     * existing contents of the specified queue.
    044     * <p>
    045     * If there are any elements already in the queue being decorated, they
    046     * will be transformed by this method.
    047     * Contrast this with {@link #transformingQueue(Queue, Transformer)}.
    048     *
    049     * @param <E> the type of the elements in the queue
    050     * @param queue  the queue to decorate, must not be null
    051     * @param transformer  the transformer to use for conversion, must not be null
    052     * @return a new transformed Queue
    053     * @throws NullPointerException if queue or transformer is null
    054     * @since 4.0
    055     */
    056    public static <E> TransformedQueue<E> transformedQueue(final Queue<E> queue,
    057                                                           final Transformer<? super E, ? extends E> transformer) {
    058        // throws IAE if queue or transformer is null
    059        final TransformedQueue<E> decorated = new TransformedQueue<>(queue, transformer);
    060        if (!queue.isEmpty()) {
    061            @SuppressWarnings("unchecked") // queue is type <E>
    062            final E[] values = (E[]) queue.toArray(); // NOPMD - false positive for generics
    063            queue.clear();
    064            for (final E value : values) {
    065                decorated.decorated().add(transformer.transform(value));
    066            }
    067        }
    068        return decorated;
    069    }
    070
    071    /**
    072     * Factory method to create a transforming queue.
    073     * <p>
    074     * If there are any elements already in the queue being decorated, they
    075     * are NOT transformed.
    076     * Contrast this with {@link #transformedQueue(Queue, Transformer)}.
    077     *
    078     * @param <E> the type of the elements in the queue
    079     * @param queue  the queue to decorate, must not be null
    080     * @param transformer  the transformer to use for conversion, must not be null
    081     * @return a new transformed Queue
    082     * @throws NullPointerException if queue or transformer is null
    083     */
    084    public static <E> TransformedQueue<E> transformingQueue(final Queue<E> queue,
    085                                                            final Transformer<? super E, ? extends E> transformer) {
    086        return new TransformedQueue<>(queue, transformer);
    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    @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}