TransformingComparator.java

  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. import java.io.Serializable;
  19. import java.util.Comparator;
  20. import java.util.Objects;

  21. import org.apache.commons.collections4.ComparatorUtils;
  22. import org.apache.commons.collections4.Transformer;

  23. /**
  24.  * Decorates another Comparator with transformation behavior. That is, the
  25.  * return value from the transform operation will be passed to the decorated
  26.  * {@link Comparator#compare(Object,Object) compare} method.
  27.  * <p>
  28.  * This class is Serializable from Commons Collections 4.0.
  29.  * </p>
  30.  *
  31.  * @param <I> the type of the input to the function
  32.  * @param <O> the type of the result of the function
  33.  * @since 2.1
  34.  * @see org.apache.commons.collections4.Transformer
  35.  * @see org.apache.commons.collections4.comparators.ComparableComparator
  36.  */
  37. public class TransformingComparator<I, O> implements Comparator<I>, Serializable {

  38.     /** Serialization version from Collections 4.0. */
  39.     private static final long serialVersionUID = 3456940356043606220L;

  40.     /** The decorated comparator. */
  41.     private final Comparator<O> decorated;

  42.     /** The transformer being used. */
  43.     private final Transformer<? super I, ? extends O> transformer;

  44.     /**
  45.      * Constructs an instance with the given Transformer and a
  46.      * {@link ComparableComparator ComparableComparator}.
  47.      *
  48.      * @param transformer what will transform the arguments to {@code compare}
  49.      */
  50.     public TransformingComparator(final Transformer<? super I, ? extends O> transformer) {
  51.         this(transformer, ComparatorUtils.NATURAL_COMPARATOR);
  52.     }

  53.     /**
  54.      * Constructs an instance with the given Transformer and Comparator.
  55.      *
  56.      * @param transformer  what will transform the arguments to {@code compare}
  57.      * @param decorated  the decorated Comparator
  58.      */
  59.     public TransformingComparator(final Transformer<? super I, ? extends O> transformer,
  60.                                   final Comparator<O> decorated) {
  61.         this.decorated = decorated;
  62.         this.transformer = transformer;
  63.     }

  64.     /**
  65.      * Returns the result of comparing the values from the transform operation.
  66.      *
  67.      * @param obj1  the first object to transform then compare
  68.      * @param obj2  the second object to transform then compare
  69.      * @return negative if obj1 is less, positive if greater, zero if equal
  70.      */
  71.     @Override
  72.     public int compare(final I obj1, final I obj2) {
  73.         final O value1 = transformer.apply(obj1);
  74.         final O value2 = transformer.apply(obj2);
  75.         return decorated.compare(value1, value2);
  76.     }

  77.     /**
  78.      * Returns {@code true} iff <em>that</em> Object is
  79.      * a {@link Comparator} whose ordering is known to be
  80.      * equivalent to mine.
  81.      * <p>
  82.      * This implementation returns {@code true}
  83.      * iff {@code <em>that</em>} is a {@link TransformingComparator}
  84.      * whose attributes are equal to mine.
  85.      *
  86.      * @param object  the object to compare to
  87.      * @return true if equal
  88.      */
  89.     @Override
  90.     public boolean equals(final Object object) {
  91.         if (this == object) {
  92.             return true;
  93.         }
  94.         if (null == object) {
  95.             return false;
  96.         }
  97.         if (object.getClass().equals(this.getClass())) {
  98.             final TransformingComparator<?, ?> comp = (TransformingComparator<?, ?>) object;
  99.             return Objects.equals(decorated, comp.decorated) &&
  100.                    Objects.equals(transformer, comp.transformer);
  101.         }
  102.         return false;
  103.     }

  104.     /**
  105.      * Implement a hash code for this comparator that is consistent with
  106.      * {@link #equals(Object) equals}.
  107.      *
  108.      * @return a hash code for this comparator.
  109.      */
  110.     @Override
  111.     public int hashCode() {
  112.         int total = 17;
  113.         total = total * 37 + (decorated == null ? 0 : decorated.hashCode());
  114.         return total * 37 + (transformer == null ? 0 : transformer.hashCode());
  115.     }

  116. }