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.collections.bag;
18
19 import java.util.Set;
20
21 import org.apache.commons.collections.Bag;
22 import org.apache.commons.collections.Transformer;
23 import org.apache.commons.collections.collection.TransformedCollection;
24 import org.apache.commons.collections.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 * This class is Serializable from Commons Collections 3.1.
35 *
36 * @since 3.0
37 * @version $Id: TransformedBag.java 1451177 2013-02-28 11:42:31Z tn $
38 */
39 public class TransformedBag<E> extends TransformedCollection<E> implements Bag<E> {
40
41 /** Serialization version */
42 private static final long serialVersionUID = 5421170911299074185L;
43
44 /**
45 * Factory method to create a transforming bag.
46 * <p>
47 * If there are any elements already in the bag being decorated, they
48 * are NOT transformed. Contrast this with {@link #transformedBag(Bag, Transformer)}.
49 *
50 * @param <E> the type of the elements in the bag
51 * @param bag the bag to decorate, must not be null
52 * @param transformer the transformer to use for conversion, must not be null
53 * @return a new transformed Bag
54 * @throws IllegalArgumentException if bag or transformer is null
55 */
56 public static <E> Bag<E> transformingBag(final Bag<E> bag, final Transformer<? super E, ? extends E> transformer) {
57 return new TransformedBag<E>(bag, transformer);
58 }
59
60 /**
61 * Factory method to create a transforming bag that will transform
62 * existing contents of the specified bag.
63 * <p>
64 * If there are any elements already in the bag being decorated, they
65 * will be transformed by this method.
66 * Contrast this with {@link #transformingBag(Bag, Transformer)}.
67 *
68 * @param <E> the type of the elements in the bag
69 * @param bag the bag to decorate, must not be null
70 * @param transformer the transformer to use for conversion, must not be null
71 * @return a new transformed Bag
72 * @throws IllegalArgumentException if bag or transformer is null
73 * @since 4.0
74 */
75 public static <E> Bag<E> transformedBag(final Bag<E> bag, final Transformer<? super E, ? extends E> transformer) {
76 final TransformedBag<E> decorated = new TransformedBag<E>(bag, transformer);
77 if (transformer != null && bag != null && bag.size() > 0) {
78 @SuppressWarnings("unchecked") // Bag is of type E
79 final E[] values = (E[]) bag.toArray();
80 bag.clear();
81 for (final E value : values) {
82 decorated.decorated().add(transformer.transform(value));
83 }
84 }
85 return decorated;
86 }
87
88 //-----------------------------------------------------------------------
89 /**
90 * Constructor that wraps (not copies).
91 * <p>
92 * If there are any elements already in the bag being decorated, they
93 * are NOT transformed.
94 *
95 * @param bag the bag to decorate, must not be null
96 * @param transformer the transformer to use for conversion, must not be null
97 * @throws IllegalArgumentException if bag or transformer is null
98 */
99 protected TransformedBag(final Bag<E> bag, final Transformer<? super E, ? extends E> transformer) {
100 super(bag, transformer);
101 }
102
103 /**
104 * Gets the decorated bag.
105 *
106 * @return the decorated bag
107 */
108 protected Bag<E> getBag() {
109 return (Bag<E>) collection;
110 }
111
112 //-----------------------------------------------------------------------
113
114 public int getCount(final Object object) {
115 return getBag().getCount(object);
116 }
117
118 public boolean remove(final Object object, final int nCopies) {
119 return getBag().remove(object, nCopies);
120 }
121
122 //-----------------------------------------------------------------------
123
124 public boolean add(final E object, final int nCopies) {
125 return getBag().add(transform(object), nCopies);
126 }
127
128 public Set<E> uniqueSet() {
129 final Set<E> set = getBag().uniqueSet();
130 return TransformedSet.<E>transformingSet(set, transformer);
131 }
132
133 }