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