001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.collections4.comparators;
018
019import java.io.Serializable;
020import java.util.Comparator;
021import java.util.Objects;
022
023import org.apache.commons.collections4.ComparatorUtils;
024import org.apache.commons.collections4.Transformer;
025
026/**
027 * Decorates another Comparator with transformation behavior. That is, the
028 * return value from the transform operation will be passed to the decorated
029 * {@link Comparator#compare(Object,Object) compare} method.
030 * <p>
031 * This class is Serializable from Commons Collections 4.0.
032 * </p>
033 *
034 * @param <I> the input type to the transformer
035 * @param <O> the output type from the transformer
036 *
037 * @since 2.1
038 *
039 * @see org.apache.commons.collections4.Transformer
040 * @see org.apache.commons.collections4.comparators.ComparableComparator
041 */
042public class TransformingComparator<I, O> implements Comparator<I>, Serializable {
043
044    /** Serialization version from Collections 4.0. */
045    private static final long serialVersionUID = 3456940356043606220L;
046
047    /** The decorated comparator. */
048    private final Comparator<O> decorated;
049    /** The transformer being used. */
050    private final Transformer<? super I, ? extends O> transformer;
051
052    /**
053     * Constructs an instance with the given Transformer and a
054     * {@link ComparableComparator ComparableComparator}.
055     *
056     * @param transformer what will transform the arguments to {@code compare}
057     */
058    public TransformingComparator(final Transformer<? super I, ? extends O> transformer) {
059        this(transformer, ComparatorUtils.NATURAL_COMPARATOR);
060    }
061
062    /**
063     * Constructs an instance with the given Transformer and Comparator.
064     *
065     * @param transformer  what will transform the arguments to {@code compare}
066     * @param decorated  the decorated Comparator
067     */
068    public TransformingComparator(final Transformer<? super I, ? extends O> transformer,
069                                  final Comparator<O> decorated) {
070        this.decorated = decorated;
071        this.transformer = transformer;
072    }
073
074    /**
075     * Returns the result of comparing the values from the transform operation.
076     *
077     * @param obj1  the first object to transform then compare
078     * @param obj2  the second object to transform then compare
079     * @return negative if obj1 is less, positive if greater, zero if equal
080     */
081    @Override
082    public int compare(final I obj1, final I obj2) {
083        final O value1 = this.transformer.transform(obj1);
084        final O value2 = this.transformer.transform(obj2);
085        return this.decorated.compare(value1, value2);
086    }
087
088    /**
089     * Returns {@code true} iff <i>that</i> Object is
090     * a {@link Comparator} whose ordering is known to be
091     * equivalent to mine.
092     * <p>
093     * This implementation returns {@code true}
094     * iff {@code <i>that</i>} is a {@link TransformingComparator}
095     * whose attributes are equal to mine.
096     *
097     * @param object  the object to compare to
098     * @return true if equal
099     */
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