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.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.
46       * <p>
47       * If there are any elements already in the bag being decorated, they
48       * are NOT transformed. Contrast this with {@link #transformedSortedBag(SortedBag, 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 SortedBag
54       * @throws NullPointerException if bag or transformer is null
55       * @since 4.0
56       */
57      public static <E> TransformedSortedBag<E> transformingSortedBag(final SortedBag<E> bag,
58              final Transformer<? super E, ? extends E> transformer) {
59          return new TransformedSortedBag<>(bag, transformer);
60      }
61  
62      /**
63       * Factory method to create a transforming sorted bag that will transform
64       * existing contents of the specified sorted bag.
65       * <p>
66       * If there are any elements already in the bag being decorated, they
67       * will be transformed by this method.
68       * Contrast this with {@link #transformingSortedBag(SortedBag, Transformer)}.
69       *
70       * @param <E> the type of the elements in the bag
71       * @param bag  the bag to decorate, must not be null
72       * @param transformer  the transformer to use for conversion, must not be null
73       * @return a new transformed SortedBag
74       * @throws NullPointerException if bag or transformer is null
75       * @since 4.0
76       */
77      public static <E> TransformedSortedBag<E> transformedSortedBag(final SortedBag<E> bag,
78              final Transformer<? super E, ? extends E> transformer) {
79  
80          final TransformedSortedBag<E>  decorated = new TransformedSortedBag<>(bag, transformer);
81          if (bag.size() > 0) {
82              @SuppressWarnings("unchecked") // bag is type E
83              final E[] values = (E[]) bag.toArray(); // NOPMD - false positive for generics
84              bag.clear();
85              for (final E value : values) {
86                  decorated.decorated().add(transformer.transform(value));
87              }
88          }
89          return decorated;
90      }
91  
92      //-----------------------------------------------------------------------
93      /**
94       * Constructor that wraps (not copies).
95       * <p>
96       * If there are any elements already in the bag being decorated, they
97       * are NOT transformed.
98       *
99       * @param bag  the bag to decorate, must not be null
100      * @param transformer  the transformer to use for conversion, must not be null
101      * @throws NullPointerException if bag or transformer is null
102      */
103     protected TransformedSortedBag(final SortedBag<E> bag, final Transformer<? super E, ? extends E> transformer) {
104         super(bag, transformer);
105     }
106 
107     /**
108      * Gets the decorated bag.
109      *
110      * @return the decorated bag
111      */
112     protected SortedBag<E> getSortedBag() {
113         return (SortedBag<E>) decorated();
114     }
115 
116     //-----------------------------------------------------------------------
117 
118     @Override
119     public E first() {
120         return getSortedBag().first();
121     }
122 
123     @Override
124     public E last() {
125         return getSortedBag().last();
126     }
127 
128     @Override
129     public Comparator<? super E> comparator() {
130         return getSortedBag().comparator();
131     }
132 
133 }