View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4.bag;
18  
19  import java.util.Set;
20  
21  import org.apache.commons.collections4.Bag;
22  import org.apache.commons.collections4.Transformer;
23  import org.apache.commons.collections4.collection.TransformedCollection;
24  import org.apache.commons.collections4.multiset.TransformedMultiSet;
25  import org.apache.commons.collections4.set.TransformedSet;
26  
27  /**
28   * Decorates another {@link Bag} to transform objects that are added.
29   * <p>
30   * The add methods are affected by this class.
31   * Thus objects must be removed or searched for using their transformed form.
32   * For example, if the transformation converts Strings to Integers, you must
33   * use the Integer form to remove objects.
34   * </p>
35   * <p>
36   * This class is Serializable from Commons Collections 3.1.
37   * </p>
38   *
39   * @param <E> The type of elements in this bag
40   * @since 3.0
41   * @deprecated Since 4.6.0, use {@link TransformedMultiSet} instead.
42   */
43  @Deprecated
44  public class TransformedBag<E> extends TransformedCollection<E> implements Bag<E> {
45  
46      /** Serialization version */
47      private static final long serialVersionUID = 5421170911299074185L;
48  
49      /**
50       * Factory method to create a transforming bag that will transform
51       * existing contents of the specified bag.
52       * <p>
53       * If there are any elements already in the bag being decorated, they
54       * will be transformed by this method.
55       * Contrast this with {@link #transformingBag(Bag, Transformer)}.
56       *
57       * @param <E> The type of the elements in the bag
58       * @param bag  The bag to decorate, must not be null
59       * @param transformer  The transformer to use for conversion, must not be null
60       * @return A new transformed Bag
61       * @throws NullPointerException if bag or transformer is null
62       * @since 4.0
63       */
64      public static <E> Bag<E> transformedBag(final Bag<E> bag, final Transformer<? super E, ? extends E> transformer) {
65          final TransformedBag<E> decorated = new TransformedBag<>(bag, transformer);
66          if (!bag.isEmpty()) {
67              @SuppressWarnings("unchecked") // Bag is of type E
68              final E[] values = (E[]) bag.toArray(); // NOPMD - false positive for generics
69              bag.clear();
70              for (final E value : values) {
71                  decorated.decorated().add(transformer.apply(value));
72              }
73          }
74          return decorated;
75      }
76  
77      /**
78       * Factory method to create a transforming bag.
79       * <p>
80       * If there are any elements already in the bag being decorated, they
81       * are NOT transformed. Contrast this with {@link #transformedBag(Bag, Transformer)}.
82       *
83       * @param <E> The type of the elements in the bag
84       * @param bag  The bag to decorate, must not be null
85       * @param transformer  The transformer to use for conversion, must not be null
86       * @return A new transformed Bag
87       * @throws NullPointerException if bag or transformer is null
88       * @since 4.0
89       */
90      public static <E> Bag<E> transformingBag(final Bag<E> bag, final Transformer<? super E, ? extends E> transformer) {
91          return new TransformedBag<>(bag, transformer);
92      }
93  
94      /**
95       * Constructor that wraps (not copies).
96       * <p>
97       * If there are any elements already in the bag being decorated, they
98       * are NOT transformed.
99       *
100      * @param bag  The bag to decorate, must not be null
101      * @param transformer  The transformer to use for conversion, must not be null
102      * @throws NullPointerException if bag or transformer is null
103      */
104     protected TransformedBag(final Bag<E> bag, final Transformer<? super E, ? extends E> transformer) {
105         super(bag, transformer);
106     }
107 
108     @Override
109     public boolean add(final E object, final int nCopies) {
110         return getBag().add(transform(object), nCopies);
111     }
112 
113     @Override
114     public boolean equals(final Object object) {
115         return object == this || decorated().equals(object);
116     }
117 
118     /**
119      * Gets the decorated bag.
120      *
121      * @return The decorated bag
122      */
123     protected Bag<E> getBag() {
124         return (Bag<E>) decorated();
125     }
126 
127     @Override
128     public int getCount(final Object object) {
129         return getBag().getCount(object);
130     }
131 
132     @Override
133     public int hashCode() {
134         return decorated().hashCode();
135     }
136 
137     @Override
138     public boolean remove(final Object object, final int nCopies) {
139         return getBag().remove(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 }