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 */
017 package org.apache.commons.collections.comparators;
018
019 import java.util.Comparator;
020
021 import org.apache.commons.collections.Transformer;
022
023 /**
024 * Decorates another Comparator with transformation behavior. That is, the
025 * return value from the transform operation will be passed to the decorated
026 * {@link Comparator#compare(Object,Object) compare} method.
027 *
028 * @since Commons Collections 2.0 (?)
029 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
030 *
031 * @see org.apache.commons.collections.Transformer
032 * @see org.apache.commons.collections.comparators.ComparableComparator
033 */
034 public class TransformingComparator implements Comparator {
035
036 /** The decorated comparator. */
037 protected Comparator decorated;
038 /** The transformer being used. */
039 protected Transformer transformer;
040
041 //-----------------------------------------------------------------------
042 /**
043 * Constructs an instance with the given Transformer and a
044 * {@link ComparableComparator ComparableComparator}.
045 *
046 * @param transformer what will transform the arguments to <code>compare</code>
047 */
048 public TransformingComparator(Transformer transformer) {
049 this(transformer, new ComparableComparator());
050 }
051
052 /**
053 * Constructs an instance with the given Transformer and Comparator.
054 *
055 * @param transformer what will transform the arguments to <code>compare</code>
056 * @param decorated the decorated Comparator
057 */
058 public TransformingComparator(Transformer transformer, Comparator decorated) {
059 this.decorated = decorated;
060 this.transformer = transformer;
061 }
062
063 //-----------------------------------------------------------------------
064 /**
065 * Returns the result of comparing the values from the transform operation.
066 *
067 * @param obj1 the first object to transform then compare
068 * @param obj2 the second object to transform then compare
069 * @return negative if obj1 is less, positive if greater, zero if equal
070 */
071 public int compare(Object obj1, Object obj2) {
072 Object value1 = this.transformer.transform(obj1);
073 Object value2 = this.transformer.transform(obj2);
074 return this.decorated.compare(value1, value2);
075 }
076
077 }
078