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    *      http://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.set.TransformedSet;
25  
26  /**
27   * Decorates another {@link Bag} to transform objects that are added.
28   * <p>
29   * The add methods are affected by this class.
30   * Thus objects must be removed or searched for using their transformed form.
31   * For example, if the transformation converts Strings to Integers, you must
32   * use the Integer form to remove objects.
33   * </p>
34   * <p>
35   * This class is Serializable from Commons Collections 3.1.
36   * </p>
37   *
38   * @param <E> the type of elements in this bag
39   * @since 3.0
40   */
41  public class TransformedBag<E> extends TransformedCollection<E> implements Bag<E> {
42  
43      /** Serialization version */
44      private static final long serialVersionUID = 5421170911299074185L;
45  
46      /**
47       * Factory method to create a transforming bag that will transform
48       * existing contents of the specified bag.
49       * <p>
50       * If there are any elements already in the bag being decorated, they
51       * will be transformed by this method.
52       * Contrast this with {@link #transformingBag(Bag, Transformer)}.
53       *
54       * @param <E> the type of the elements in the bag
55       * @param bag  the bag to decorate, must not be null
56       * @param transformer  the transformer to use for conversion, must not be null
57       * @return a new transformed Bag
58       * @throws NullPointerException if bag or transformer is null
59       * @since 4.0
60       */
61      public static <E> Bag<E> transformedBag(final Bag<E> bag, final Transformer<? super E, ? extends E> transformer) {
62          final TransformedBag<E> decorated = new TransformedBag<>(bag, transformer);
63          if (!bag.isEmpty()) {
64              @SuppressWarnings("unchecked") // Bag is of type E
65              final E[] values = (E[]) bag.toArray(); // NOPMD - false positive for generics
66              bag.clear();
67              for (final E value : values) {
68                  decorated.decorated().add(transformer.transform(value));
69              }
70          }
71          return decorated;
72      }
73  
74      /**
75       * Factory method to create a transforming bag.
76       * <p>
77       * If there are any elements already in the bag being decorated, they
78       * are NOT transformed. Contrast this with {@link #transformedBag(Bag, Transformer)}.
79       *
80       * @param <E> the type of the elements in the bag
81       * @param bag  the bag to decorate, must not be null
82       * @param transformer  the transformer to use for conversion, must not be null
83       * @return a new transformed Bag
84       * @throws NullPointerException if bag or transformer is null
85       * @since 4.0
86       */
87      public static <E> Bag<E> transformingBag(final Bag<E> bag, final Transformer<? super E, ? extends E> transformer) {
88          return new TransformedBag<>(bag, transformer);
89      }
90  
91      /**
92       * Constructor that wraps (not copies).
93       * <p>
94       * If there are any elements already in the bag being decorated, they
95       * are NOT transformed.
96       *
97       * @param bag  the bag to decorate, must not be null
98       * @param transformer  the transformer to use for conversion, must not be null
99       * @throws NullPointerException if bag or transformer is null
100      */
101     protected TransformedBag(final Bag<E> bag, final Transformer<? super E, ? extends E> transformer) {
102         super(bag, transformer);
103     }
104 
105     @Override
106     public boolean add(final E object, final int nCopies) {
107         return getBag().add(transform(object), nCopies);
108     }
109 
110     @Override
111     public boolean equals(final Object object) {
112         return object == this || decorated().equals(object);
113     }
114 
115     /**
116      * Gets the decorated bag.
117      *
118      * @return the decorated bag
119      */
120     protected Bag<E> getBag() {
121         return (Bag<E>) decorated();
122     }
123 
124     @Override
125     public int getCount(final Object object) {
126         return getBag().getCount(object);
127     }
128 
129     @Override
130     public int hashCode() {
131         return decorated().hashCode();
132     }
133 
134     @Override
135     public boolean remove(final Object object, final int nCopies) {
136         return getBag().remove(object, nCopies);
137     }
138 
139     @Override
140     public Set<E> uniqueSet() {
141         final Set<E> set = getBag().uniqueSet();
142         return TransformedSet.<E>transformingSet(set, transformer);
143     }
144 
145 }