KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > comparators > TransformingComparator


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.comparators;
17
18 import java.util.Comparator JavaDoc;
19
20 import org.apache.commons.collections.Transformer;
21
22 /**
23  * Decorates another Comparator with transformation behavior. That is, the
24  * return value from the transform operation will be passed to the decorated
25  * {@link Comparator#compare(Object,Object) compare} method.
26  *
27  * @since Commons Collections 2.0 (?)
28  * @version $Revision$ $Date$
29  *
30  * @see org.apache.commons.collections.Transformer
31  * @see org.apache.commons.collections.comparators.ComparableComparator
32  */

33 public class TransformingComparator implements Comparator JavaDoc {
34     
35     /** The decorated comparator. */
36     protected Comparator JavaDoc decorated;
37     /** The transformer being used. */
38     protected Transformer transformer;
39
40     //-----------------------------------------------------------------------
41
/**
42      * Constructs an instance with the given Transformer and a
43      * {@link ComparableComparator ComparableComparator}.
44      *
45      * @param transformer what will transform the arguments to <code>compare</code>
46      */

47     public TransformingComparator(Transformer transformer) {
48         this(transformer, new ComparableComparator());
49     }
50
51     /**
52      * Constructs an instance with the given Transformer and Comparator.
53      *
54      * @param transformer what will transform the arguments to <code>compare</code>
55      * @param decorated the decorated Comparator
56      */

57     public TransformingComparator(Transformer transformer, Comparator JavaDoc decorated) {
58         this.decorated = decorated;
59         this.transformer = transformer;
60     }
61
62     //-----------------------------------------------------------------------
63
/**
64      * Returns the result of comparing the values from the transform operation.
65      *
66      * @param obj1 the first object to transform then compare
67      * @param obj2 the second object to transform then compare
68      * @return negative if obj1 is less, positive if greater, zero if equal
69      */

70     public int compare(Object JavaDoc obj1, Object JavaDoc obj2) {
71         Object JavaDoc value1 = this.transformer.transform(obj1);
72         Object JavaDoc value2 = this.transformer.transform(obj2);
73         return this.decorated.compare(value1, value2);
74     }
75
76 }
77
78
Popular Tags