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