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