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.comparators;
18  
19  import java.io.Serializable;
20  import java.util.Comparator;
21  import java.util.Objects;
22  
23  import org.apache.commons.collections4.ComparatorUtils;
24  import org.apache.commons.collections4.Transformer;
25  
26  /**
27   * Decorates another Comparator with transformation behavior. That is, the
28   * return value from the transform operation will be passed to the decorated
29   * {@link Comparator#compare(Object,Object) compare} method.
30   * <p>
31   * This class is Serializable from Commons Collections 4.0.
32   * </p>
33   *
34   * @param <I> the input type to the transformer
35   * @param <O> the output type from the transformer
36   *
37   * @since 2.1
38   *
39   * @see org.apache.commons.collections4.Transformer
40   * @see org.apache.commons.collections4.comparators.ComparableComparator
41   */
42  public class TransformingComparator<I, O> implements Comparator<I>, Serializable {
43  
44      /** Serialization version from Collections 4.0. */
45      private static final long serialVersionUID = 3456940356043606220L;
46  
47      /** The decorated comparator. */
48      private final Comparator<O> decorated;
49      /** The transformer being used. */
50      private final Transformer<? super I, ? extends O> transformer;
51  
52      /**
53       * Constructs an instance with the given Transformer and a
54       * {@link ComparableComparator ComparableComparator}.
55       *
56       * @param transformer what will transform the arguments to {@code compare}
57       */
58      public TransformingComparator(final Transformer<? super I, ? extends O> transformer) {
59          this(transformer, ComparatorUtils.NATURAL_COMPARATOR);
60      }
61  
62      /**
63       * Constructs an instance with the given Transformer and Comparator.
64       *
65       * @param transformer  what will transform the arguments to {@code compare}
66       * @param decorated  the decorated Comparator
67       */
68      public TransformingComparator(final Transformer<? super I, ? extends O> transformer,
69                                    final Comparator<O> decorated) {
70          this.decorated = decorated;
71          this.transformer = transformer;
72      }
73  
74      /**
75       * Returns the result of comparing the values from the transform operation.
76       *
77       * @param obj1  the first object to transform then compare
78       * @param obj2  the second object to transform then compare
79       * @return negative if obj1 is less, positive if greater, zero if equal
80       */
81      @Override
82      public int compare(final I obj1, final I obj2) {
83          final O value1 = this.transformer.transform(obj1);
84          final O value2 = this.transformer.transform(obj2);
85          return this.decorated.compare(value1, value2);
86      }
87  
88      /**
89       * Returns {@code true} iff <i>that</i> Object is
90       * a {@link Comparator} whose ordering is known to be
91       * equivalent to mine.
92       * <p>
93       * This implementation returns {@code true}
94       * iff {@code <i>that</i>} is a {@link TransformingComparator}
95       * whose attributes are equal to mine.
96       *
97       * @param object  the object to compare to
98       * @return true if equal
99       */
100     @Override
101     public boolean equals(final Object object) {
102         if (this == object) {
103             return true;
104         }
105         if (null == object) {
106             return false;
107         }
108         if (object.getClass().equals(this.getClass())) {
109             final TransformingComparator<?, ?> comp = (TransformingComparator<?, ?>) object;
110             return Objects.equals(decorated, comp.decorated) &&
111                    Objects.equals(transformer, comp.transformer);
112         }
113         return false;
114     }
115 
116     /**
117      * Implement a hash code for this comparator that is consistent with
118      * {@link #equals(Object) equals}.
119      *
120      * @return a hash code for this comparator.
121      */
122     @Override
123     public int hashCode() {
124         int total = 17;
125         total = total*37 + (decorated == null ? 0 : decorated.hashCode());
126         total = total*37 + (transformer == null ? 0 : transformer.hashCode());
127         return total;
128     }
129 
130 }
131