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;
021
022import org.apache.commons.collections4.ComparatorUtils;
023import org.apache.commons.collections4.Transformer;
024
025/**
026 * Decorates another Comparator with transformation behavior. That is, the
027 * return value from the transform operation will be passed to the decorated
028 * {@link Comparator#compare(Object,Object) compare} method.
029 * <p>
030 * This class is Serializable from Commons Collections 4.0.
031 *
032 * @since 2.1
033 * @version $Id: TransformingComparator.html 972421 2015-11-14 20:00:04Z tn $
034 *
035 * @see org.apache.commons.collections4.Transformer
036 * @see org.apache.commons.collections4.comparators.ComparableComparator
037 */
038public class TransformingComparator<I, O> implements Comparator<I>, Serializable {
039
040    /** Serialization version from Collections 4.0. */
041    private static final long serialVersionUID = 3456940356043606220L;
042
043    /** The decorated comparator. */
044    private final Comparator<O> decorated;
045    /** The transformer being used. */
046    private final Transformer<? super I, ? extends O> transformer;
047
048    //-----------------------------------------------------------------------
049    /**
050     * Constructs an instance with the given Transformer and a
051     * {@link ComparableComparator ComparableComparator}.
052     *
053     * @param transformer what will transform the arguments to <code>compare</code>
054     */
055    @SuppressWarnings("unchecked")
056    public TransformingComparator(final Transformer<? super I, ? extends O> transformer) {
057        this(transformer, ComparatorUtils.NATURAL_COMPARATOR);
058    }
059
060    /**
061     * Constructs an instance with the given Transformer and Comparator.
062     *
063     * @param transformer  what will transform the arguments to <code>compare</code>
064     * @param decorated  the decorated Comparator
065     */
066    public TransformingComparator(final Transformer<? super I, ? extends O> transformer,
067                                  final Comparator<O> decorated) {
068        this.decorated = decorated;
069        this.transformer = transformer;
070    }
071
072    //-----------------------------------------------------------------------
073    /**
074     * Returns the result of comparing the values from the transform operation.
075     *
076     * @param obj1  the first object to transform then compare
077     * @param obj2  the second object to transform then compare
078     * @return negative if obj1 is less, positive if greater, zero if equal
079     */
080    public int compare(final I obj1, final I obj2) {
081        final O value1 = this.transformer.transform(obj1);
082        final O value2 = this.transformer.transform(obj2);
083        return this.decorated.compare(value1, value2);
084    }
085
086    //-----------------------------------------------------------------------
087    /**
088     * Implement a hash code for this comparator that is consistent with
089     * {@link #equals(Object) equals}.
090     *
091     * @return a hash code for this comparator.
092     */
093    @Override
094    public int hashCode() {
095        int total = 17;
096        total = total*37 + (decorated == null ? 0 : decorated.hashCode());
097        total = total*37 + (transformer == null ? 0 : transformer.hashCode());
098        return total;
099    }
100
101    /**
102     * Returns <code>true</code> iff <i>that</i> Object is
103     * is a {@link Comparator} whose ordering is known to be
104     * equivalent to mine.
105     * <p>
106     * This implementation returns <code>true</code>
107     * iff <code><i>that</i></code> is a {@link TransformingComparator}
108     * whose attributes are equal to mine.
109     *
110     * @param object  the object to compare to
111     * @return true if equal
112     */
113    @Override
114    public boolean equals(final Object object) {
115        if (this == object) {
116            return true;
117        }
118        if (null == object) {
119            return false;
120        }
121        if (object.getClass().equals(this.getClass())) {
122            final TransformingComparator<?, ?> comp = (TransformingComparator<?, ?>) object;
123            return null == decorated ? null == comp.decorated : decorated.equals(comp.decorated) &&
124                    null == transformer ? null == comp.transformer : transformer.equals(comp.transformer);
125        }
126        return false;
127    }
128
129}
130