TransformedQueue.java

  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. import java.util.Queue;

  19. import org.apache.commons.collections4.Transformer;
  20. import org.apache.commons.collections4.collection.TransformedCollection;

  21. /**
  22.  * Decorates another {@link Queue} to transform objects that are added.
  23.  * <p>
  24.  * The add/offer methods are affected by this class.
  25.  * Thus objects must be removed or searched for using their transformed form.
  26.  * For example, if the transformation converts Strings to Integers, you must
  27.  * use the Integer form to remove objects.
  28.  * </p>
  29.  *
  30.  * @param <E> the type of elements held in this queue
  31.  * @since 4.0
  32.  */
  33. public class TransformedQueue<E> extends TransformedCollection<E> implements Queue<E> {

  34.     /** Serialization version */
  35.     private static final long serialVersionUID = -7901091318986132033L;

  36.     /**
  37.      * Factory method to create a transforming queue that will transform
  38.      * existing contents of the specified queue.
  39.      * <p>
  40.      * If there are any elements already in the queue being decorated, they
  41.      * will be transformed by this method.
  42.      * Contrast this with {@link #transformingQueue(Queue, Transformer)}.
  43.      *
  44.      * @param <E> the type of the elements in the queue
  45.      * @param queue  the queue to decorate, must not be null
  46.      * @param transformer  the transformer to use for conversion, must not be null
  47.      * @return a new transformed Queue
  48.      * @throws NullPointerException if queue or transformer is null
  49.      * @since 4.0
  50.      */
  51.     public static <E> TransformedQueue<E> transformedQueue(final Queue<E> queue,
  52.                                                            final Transformer<? super E, ? extends E> transformer) {
  53.         // throws IAE if queue or transformer is null
  54.         final TransformedQueue<E> decorated = new TransformedQueue<>(queue, transformer);
  55.         if (!queue.isEmpty()) {
  56.             @SuppressWarnings("unchecked") // queue is type <E>
  57.             final E[] values = (E[]) queue.toArray(); // NOPMD - false positive for generics
  58.             queue.clear();
  59.             for (final E value : values) {
  60.                 decorated.decorated().add(transformer.apply(value));
  61.             }
  62.         }
  63.         return decorated;
  64.     }

  65.     /**
  66.      * Factory method to create a transforming queue.
  67.      * <p>
  68.      * If there are any elements already in the queue being decorated, they
  69.      * are NOT transformed.
  70.      * Contrast this with {@link #transformedQueue(Queue, Transformer)}.
  71.      *
  72.      * @param <E> the type of the elements in the queue
  73.      * @param queue  the queue to decorate, must not be null
  74.      * @param transformer  the transformer to use for conversion, must not be null
  75.      * @return a new transformed Queue
  76.      * @throws NullPointerException if queue or transformer is null
  77.      */
  78.     public static <E> TransformedQueue<E> transformingQueue(final Queue<E> queue,
  79.                                                             final Transformer<? super E, ? extends E> transformer) {
  80.         return new TransformedQueue<>(queue, transformer);
  81.     }

  82.     /**
  83.      * Constructor that wraps (not copies).
  84.      * <p>
  85.      * If there are any elements already in the queue being decorated, they
  86.      * are NOT transformed.
  87.      *
  88.      * @param queue  the queue to decorate, must not be null
  89.      * @param transformer  the transformer to use for conversion, must not be null
  90.      * @throws NullPointerException if queue or transformer is null
  91.      */
  92.     protected TransformedQueue(final Queue<E> queue, final Transformer<? super E, ? extends E> transformer) {
  93.         super(queue, transformer);
  94.     }

  95.     @Override
  96.     public E element() {
  97.         return getQueue().element();
  98.     }

  99.     /**
  100.      * Gets the decorated queue.
  101.      *
  102.      * @return the decorated queue
  103.      */
  104.     protected Queue<E> getQueue() {
  105.         return (Queue<E>) decorated();
  106.     }

  107.     @Override
  108.     public boolean offer(final E obj) {
  109.         return getQueue().offer(transform(obj));
  110.     }

  111.     @Override
  112.     public E peek() {
  113.         return getQueue().peek();
  114.     }

  115.     @Override
  116.     public E poll() {
  117.         return getQueue().poll();
  118.     }

  119.     @Override
  120.     public E remove() {
  121.         return getQueue().remove();
  122.     }

  123. }