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