    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.bag;
    018
    019import java.util.Comparator;
    020
    021import org.apache.commons.collections4.SortedBag;
    022import org.apache.commons.collections4.Transformer;
    023
    024/**
    025 * Decorates another {@link SortedBag} to transform objects that are added.
    026 * <p>
    027 * The add 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 * <p>
    033 * This class is Serializable from Commons Collections 3.1.
    034 * </p>
    035 *
    036 * @param <E> the type of elements in this bag
    037 * @since 3.0
    038 */
    039public class TransformedSortedBag<E> extends TransformedBag<E> implements SortedBag<E> {
    040
    041    /** Serialization version */
    042    private static final long serialVersionUID = -251737742649401930L;
    043
    044    /**
    045     * Factory method to create a transforming sorted bag that will transform
    046     * existing contents of the specified sorted bag.
    047     * <p>
    048     * If there are any elements already in the bag being decorated, they
    049     * will be transformed by this method.
    050     * Contrast this with {@link #transformingSortedBag(SortedBag, Transformer)}.
    051     *
    052     * @param <E> the type of the elements in the bag
    053     * @param bag  the bag to decorate, must not be null
    054     * @param transformer  the transformer to use for conversion, must not be null
    055     * @return a new transformed SortedBag
    056     * @throws NullPointerException if bag or transformer is null
    057     * @since 4.0
    058     */
    059    public static <E> TransformedSortedBag<E> transformedSortedBag(final SortedBag<E> bag,
    060            final Transformer<? super E, ? extends E> transformer) {
    061
    062        final TransformedSortedBag<E>  decorated = new TransformedSortedBag<>(bag, transformer);
    063        if (!bag.isEmpty()) {
    064            @SuppressWarnings("unchecked") // bag is type E
    065            final E[] values = (E[]) bag.toArray(); // NOPMD - false positive for generics
    066            bag.clear();
    067            for (final E value : values) {
    068                decorated.decorated().add(transformer.apply(value));
    069            }
    070        }
    071        return decorated;
    072    }
    073
    074    /**
    075     * Factory method to create a transforming sorted bag.
    076     * <p>
    077     * If there are any elements already in the bag being decorated, they
    078     * are NOT transformed. Contrast this with {@link #transformedSortedBag(SortedBag, Transformer)}.
    079     *
    080     * @param <E> the type of the elements in the bag
    081     * @param bag  the bag to decorate, must not be null
    082     * @param transformer  the transformer to use for conversion, must not be null
    083     * @return a new transformed SortedBag
    084     * @throws NullPointerException if bag or transformer is null
    085     * @since 4.0
    086     */
    087    public static <E> TransformedSortedBag<E> transformingSortedBag(final SortedBag<E> bag,
    088            final Transformer<? super E, ? extends E> transformer) {
    089        return new TransformedSortedBag<>(bag, transformer);
    090    }
    091
    092    /**
    093     * Constructor that wraps (not copies).
    094     * <p>
    095     * If there are any elements already in the bag being decorated, they
    096     * are NOT transformed.
    097     *
    098     * @param bag  the bag to decorate, must not be null
    099     * @param transformer  the transformer to use for conversion, must not be null
    100     * @throws NullPointerException if bag or transformer is null
    101     */
    102    protected TransformedSortedBag(final SortedBag<E> bag, final Transformer<? super E, ? extends E> transformer) {
    103        super(bag, transformer);
    104    }
    105
    106    @Override
    107    public Comparator<? super E> comparator() {
    108        return getSortedBag().comparator();
    109    }
    110
    111    @Override
    112    public E first() {
    113        return getSortedBag().first();
    114    }
    115
    116    /**
    117     * Gets the decorated bag.
    118     *
    119     * @return the decorated bag
    120     */
    121    protected SortedBag<E> getSortedBag() {
    122        return (SortedBag<E>) decorated();
    123    }
    124
    125    @Override
    126    public E last() {
    127        return getSortedBag().last();
    128    }
    129
    130}