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.Comparator;
20
21 import org.apache.commons.collections.SortedBag;
22 import org.apache.commons.collections.Transformer;
23
24 /**
25 * Decorates another {@link SortedBag} to transform objects that are added.
26 * <p>
27 * The add methods are affected by this class.
28 * Thus objects must be removed or searched for using their transformed form.
29 * For example, if the transformation converts Strings to Integers, you must
30 * use the Integer form to remove objects.
31 * <p>
32 * This class is Serializable from Commons Collections 3.1.
33 *
34 * @since 3.0
35 * @version $Id: TransformedSortedBag.java 1451177 2013-02-28 11:42:31Z tn $
36 */
37 public class TransformedSortedBag<E> extends TransformedBag<E> implements SortedBag<E> {
38
39 /** Serialization version */
40 private static final long serialVersionUID = -251737742649401930L;
41
42 /**
43 * Factory method to create a transforming sorted bag.
44 * <p>
45 * If there are any elements already in the bag being decorated, they
46 * are NOT transformed. Contrast this with {@link #transformedSortedBag(SortedBag, Transformer)}.
47 *
48 * @param <E> the type of the elements in the bag
49 * @param bag the bag to decorate, must not be null
50 * @param transformer the transformer to use for conversion, must not be null
51 * @return a new transformed SortedBag
52 * @throws IllegalArgumentException if bag or transformer is null
53 */
54 public static <E> TransformedSortedBag<E> transformingSortedBag(final SortedBag<E> bag,
55 final Transformer<? super E, ? extends E> transformer) {
56 return new TransformedSortedBag<E>(bag, transformer);
57 }
58
59 /**
60 * Factory method to create a transforming sorted bag that will transform
61 * existing contents of the specified sorted bag.
62 * <p>
63 * If there are any elements already in the bag being decorated, they
64 * will be transformed by this method.
65 * Contrast this with {@link #transformingSortedBag(SortedBag, Transformer)}.
66 *
67 * @param <E> the type of the elements in the bag
68 * @param bag the bag to decorate, must not be null
69 * @param transformer the transformer to use for conversion, must not be null
70 * @return a new transformed SortedBag
71 * @throws IllegalArgumentException if bag or transformer is null
72 * @since 4.0
73 */
74 public static <E> TransformedSortedBag<E> transformedSortedBag(final SortedBag<E> bag,
75 final Transformer<? super E, ? extends E> transformer) {
76
77 final TransformedSortedBag<E> decorated = new TransformedSortedBag<E>(bag, transformer);
78 if (transformer != null && bag != null && bag.size() > 0) {
79 @SuppressWarnings("unchecked") // bag is type E
80 final E[] values = (E[]) bag.toArray();
81 bag.clear();
82 for (final E value : values) {
83 decorated.decorated().add(transformer.transform(value));
84 }
85 }
86 return decorated;
87 }
88
89 //-----------------------------------------------------------------------
90 /**
91 * Constructor that wraps (not copies).
92 * <p>
93 * If there are any elements already in the bag being decorated, they
94 * are NOT transformed.
95 *
96 * @param bag the bag to decorate, must not be null
97 * @param transformer the transformer to use for conversion, must not be null
98 * @throws IllegalArgumentException if bag or transformer is null
99 */
100 protected TransformedSortedBag(final SortedBag<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 SortedBag<E> getSortedBag() {
110 return (SortedBag<E>) collection;
111 }
112
113 //-----------------------------------------------------------------------
114
115 public E first() {
116 return getSortedBag().first();
117 }
118
119 public E last() {
120 return getSortedBag().last();
121 }
122
123 public Comparator<? super E> comparator() {
124 return getSortedBag().comparator();
125 }
126
127 }