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.Comparator;
20
21 import org.apache.commons.collections4.SortedBag;
22 import org.apache.commons.collections4.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 * <p>
33 * This class is Serializable from Commons Collections 3.1.
34 * </p>
35 *
36 * @param <E> the type of elements in this bag
37 * @since 3.0
38 */
39 public class TransformedSortedBag<E> extends TransformedBag<E> implements SortedBag<E> {
40
41 /** Serialization version */
42 private static final long serialVersionUID = -251737742649401930L;
43
44 /**
45 * Factory method to create a transforming sorted bag that will transform
46 * existing contents of the specified sorted bag.
47 * <p>
48 * If there are any elements already in the bag being decorated, they
49 * will be transformed by this method.
50 * Contrast this with {@link #transformingSortedBag(SortedBag, Transformer)}.
51 *
52 * @param <E> the type of the elements in the bag
53 * @param bag the bag to decorate, must not be null
54 * @param transformer the transformer to use for conversion, must not be null
55 * @return a new transformed SortedBag
56 * @throws NullPointerException if bag or transformer is null
57 * @since 4.0
58 */
59 public static <E> TransformedSortedBag<E> transformedSortedBag(final SortedBag<E> bag,
60 final Transformer<? super E, ? extends E> transformer) {
61
62 final TransformedSortedBag<E> decorated = new TransformedSortedBag<>(bag, transformer);
63 if (!bag.isEmpty()) {
64 @SuppressWarnings("unchecked") // bag is 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.apply(value));
69 }
70 }
71 return decorated;
72 }
73
74 /**
75 * Factory method to create a transforming sorted bag.
76 * <p>
77 * If there are any elements already in the bag being decorated, they
78 * are NOT transformed. Contrast this with {@link #transformedSortedBag(SortedBag, 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 SortedBag
84 * @throws NullPointerException if bag or transformer is null
85 * @since 4.0
86 */
87 public static <E> TransformedSortedBag<E> transformingSortedBag(final SortedBag<E> bag,
88 final Transformer<? super E, ? extends E> transformer) {
89 return new TransformedSortedBag<>(bag, transformer);
90 }
91
92 /**
93 * Constructor that wraps (not copies).
94 * <p>
95 * If there are any elements already in the bag being decorated, they
96 * are NOT transformed.
97 *
98 * @param bag the bag to decorate, must not be null
99 * @param transformer the transformer to use for conversion, must not be null
100 * @throws NullPointerException if bag or transformer is null
101 */
102 protected TransformedSortedBag(final SortedBag<E> bag, final Transformer<? super E, ? extends E> transformer) {
103 super(bag, transformer);
104 }
105
106 @Override
107 public Comparator<? super E> comparator() {
108 return getSortedBag().comparator();
109 }
110
111 @Override
112 public E first() {
113 return getSortedBag().first();
114 }
115
116 /**
117 * Gets the decorated bag.
118 *
119 * @return the decorated bag
120 */
121 protected SortedBag<E> getSortedBag() {
122 return (SortedBag<E>) decorated();
123 }
124
125 @Override
126 public E last() {
127 return getSortedBag().last();
128 }
129
130 }