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.Set;
020
021import org.apache.commons.collections4.Bag;
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 Bag} to transform objects that are added.
028 * <p>
029 * The add 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 * This class is Serializable from Commons Collections 3.1.
035 *
036 * @param <E> the type of elements in this bag
037 * @since 3.0
038 */
039public class TransformedBag<E> extends TransformedCollection<E> implements Bag<E> {
040
041    /** Serialization version */
042    private static final long serialVersionUID = 5421170911299074185L;
043
044    /**
045     * Factory method to create a transforming bag.
046     * <p>
047     * If there are any elements already in the bag being decorated, they
048     * are NOT transformed. Contrast this with {@link #transformedBag(Bag, Transformer)}.
049     *
050     * @param <E> the type of the elements in the bag
051     * @param bag  the bag to decorate, must not be null
052     * @param transformer  the transformer to use for conversion, must not be null
053     * @return a new transformed Bag
054     * @throws NullPointerException if bag or transformer is null
055     * @since 4.0
056     */
057    public static <E> Bag<E> transformingBag(final Bag<E> bag, final Transformer<? super E, ? extends E> transformer) {
058        return new TransformedBag<>(bag, transformer);
059    }
060
061    /**
062     * Factory method to create a transforming bag that will transform
063     * existing contents of the specified bag.
064     * <p>
065     * If there are any elements already in the bag being decorated, they
066     * will be transformed by this method.
067     * Contrast this with {@link #transformingBag(Bag, Transformer)}.
068     *
069     * @param <E> the type of the elements in the bag
070     * @param bag  the bag to decorate, must not be null
071     * @param transformer  the transformer to use for conversion, must not be null
072     * @return a new transformed Bag
073     * @throws NullPointerException if bag or transformer is null
074     * @since 4.0
075     */
076    public static <E> Bag<E> transformedBag(final Bag<E> bag, final Transformer<? super E, ? extends E> transformer) {
077        final TransformedBag<E> decorated = new TransformedBag<>(bag, transformer);
078        if (bag.size() > 0) {
079            @SuppressWarnings("unchecked") // Bag is of type E
080            final E[] values = (E[]) bag.toArray(); // NOPMD - false positive for generics
081            bag.clear();
082            for (final E value : values) {
083                decorated.decorated().add(transformer.transform(value));
084            }
085        }
086        return decorated;
087    }
088
089    //-----------------------------------------------------------------------
090    /**
091     * Constructor that wraps (not copies).
092     * <p>
093     * If there are any elements already in the bag being decorated, they
094     * are NOT transformed.
095     *
096     * @param bag  the bag to decorate, must not be null
097     * @param transformer  the transformer to use for conversion, must not be null
098     * @throws NullPointerException if bag or transformer is null
099     */
100    protected TransformedBag(final Bag<E> bag, final Transformer<? super E, ? extends E> transformer) {
101        super(bag, transformer);
102    }
103
104    /**
105     * Gets the decorated bag.
106     *
107     * @return the decorated bag
108     */
109    protected Bag<E> getBag() {
110        return (Bag<E>) decorated();
111    }
112
113    @Override
114    public boolean equals(final Object object) {
115        return object == this || decorated().equals(object);
116    }
117
118    @Override
119    public int hashCode() {
120        return decorated().hashCode();
121    }
122
123    //-----------------------------------------------------------------------
124
125    @Override
126    public int getCount(final Object object) {
127        return getBag().getCount(object);
128    }
129
130    @Override
131    public boolean remove(final Object object, final int nCopies) {
132        return getBag().remove(object, nCopies);
133    }
134
135    //-----------------------------------------------------------------------
136
137    @Override
138    public boolean add(final E object, final int nCopies) {
139        return getBag().add(transform(object), nCopies);
140    }
141
142    @Override
143    public Set<E> uniqueSet() {
144        final Set<E> set = getBag().uniqueSet();
145        return TransformedSet.<E>transformingSet(set, transformer);
146    }
147
148}