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.Set;
020
021import org.apache.commons.collections4.MultiSet;
022import org.apache.commons.collections4.Transformer;
023import org.apache.commons.collections4.collection.TransformedCollection;
024import org.apache.commons.collections4.set.TransformedSet;
025
026/**
027 * Decorates another {@link MultiSet} to transform objects that are added.
028 * <p>
029 * The add and setCount methods are affected by this class.
030 * Thus objects must be removed or searched for using their transformed form.
031 * For example, if the transformation converts Strings to Integers, you must
032 * use the Integer form to remove objects.
033 * </p>
034 *
035 * @param <E> The type held in the multiset
036 * @since 4.6.0
037 */
038public class TransformedMultiSet<E> extends TransformedCollection<E> implements MultiSet<E> {
039
040    /** Serialization version */
041    private static final long serialVersionUID = 20260710L;
042
043    /**
044     * Factory method to create a transforming multiset that will transform
045     * existing contents of the specified multiset.
046     * <p>
047     * If there are any elements already in the multiset being decorated, they
048     * will be transformed by this method.
049     * Contrast this with {@link #transformingMultiSet(MultiSet, Transformer)}.
050     * </p>
051     *
052     * @param <E> The type of the elements in the multiset.
053     * @param multiset  The multiset to decorate, must not be null.
054     * @param transformer  The transformer to use for conversion, must not be null.
055     * @return A new transformed MultiSet.
056     * @throws NullPointerException if multiset or transformer is null.
057     */
058    public static <E> TransformedMultiSet<E> transformedMultiSet(final MultiSet<E> multiset,
059            final Transformer<? super E, ? extends E> transformer) {
060        final TransformedMultiSet<E> decorated = new TransformedMultiSet<>(multiset, transformer);
061        if (!multiset.isEmpty()) {
062            @SuppressWarnings("unchecked") // multiset is of type E
063            final E[] values = (E[]) multiset.toArray(); // NOPMD - false positive for generics
064            multiset.clear();
065            for (final E value : values) {
066                decorated.decorated().add(transformer.apply(value));
067            }
068        }
069        return decorated;
070    }
071
072    /**
073     * Factory method to create a transforming multiset.
074     * <p>
075     * If there are any elements already in the multiset being decorated, they
076     * are NOT transformed. Contrast this with {@link #transformedMultiSet(MultiSet, Transformer)}.
077     * </p>
078     *
079     *
080     * @param <E> The type of the elements in the multiset.
081     * @param multiset  The multiset to decorate, must not be null.
082     * @param transformer  The transformer to use for conversion, must not be null.
083     * @return A new transformed MultiSet.
084     * @throws NullPointerException if multiset or transformer is null.
085     */
086    public static <E> TransformedMultiSet<E> transformingMultiSet(final MultiSet<E> multiset,
087            final Transformer<? super E, ? extends E> transformer) {
088        return new TransformedMultiSet<>(multiset, transformer);
089    }
090
091    /**
092     * Constructor that wraps (not copies).
093     * <p>
094     * If there are any elements already in the multiset being decorated, they
095     * are NOT transformed.
096     * </p>
097     *
098     * @param multiset  The multiset to decorate, must not be null.
099     * @param transformer  The transformer to use for conversion, must not be null.
100     * @throws NullPointerException if multiset or transformer is null.
101     */
102    protected TransformedMultiSet(final MultiSet<E> multiset, final Transformer<? super E, ? extends E> transformer) {
103        super(multiset, transformer);
104    }
105
106    @Override
107    public int add(final E object, final int occurrences) {
108        return getMultiSet().add(transform(object), occurrences);
109    }
110
111    @Override
112    public Set<MultiSet.Entry<E>> entrySet() {
113        return getMultiSet().entrySet();
114    }
115
116    @Override
117    public boolean equals(final Object object) {
118        return object == this || decorated().equals(object);
119    }
120
121    @Override
122    public int getCount(final Object object) {
123        return getMultiSet().getCount(object);
124    }
125
126    /**
127     * Gets the decorated multiset.
128     *
129     * @return The decorated multiset.
130     */
131    protected MultiSet<E> getMultiSet() {
132        return (MultiSet<E>) decorated();
133    }
134
135    @Override
136    public int hashCode() {
137        return decorated().hashCode();
138    }
139
140    @Override
141    public int remove(final Object object, final int occurrences) {
142        return getMultiSet().remove(object, occurrences);
143    }
144
145    @Override
146    public int setCount(final E object, final int count) {
147        return getMultiSet().setCount(transform(object), count);
148    }
149
150    @Override
151    public Set<E> uniqueSet() {
152        return TransformedSet.<E>transformingSet(getMultiSet().uniqueSet(), transformer);
153    }
154
155}