KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > beanutils > BeanComparator


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
17 package org.apache.commons.beanutils;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.Comparator JavaDoc;
21 import org.apache.commons.beanutils.PropertyUtils;
22 import org.apache.commons.collections.comparators.ComparableComparator;
23
24 /**
25  * <p>
26  * This comparator compares two beans by the specified bean property.
27  * It is also possible to compare beans based on nested, indexed,
28  * combined, mapped bean properties. Please see the {@link PropertyUtilsBean}
29  * documentation for all property name possibilities.
30  *
31  * </p><p>
32  * <strong>Note:</strong> The BeanComparator passes the values of the specified
33  * bean property to a ComparableComparator, if no comparator is
34  * specified in the constructor. If you are comparing two beans based
35  * on a property that could contain "null" values, a suitable <code>Comparator</code>
36  * or <code>ComparatorChain</code> should be supplied in the constructor.
37  * </p>
38  *
39  * @author <a href"mailto:epugh@upstate.com">Eric Pugh</a>
40  * @author Tim O'Brien
41  */

42 public class BeanComparator implements Comparator JavaDoc, Serializable JavaDoc {
43
44     private String JavaDoc property;
45     private Comparator JavaDoc comparator;
46
47     /**
48      * <p>Constructs a Bean Comparator without a property set.
49      * </p><p>
50      * <strong>Note</strong> that this is intended to be used
51      * only in bean-centric environments.
52      * </p><p>
53      * Until {@link #setProperty} is called with a non-null value.
54      * this comparator will compare the Objects only.
55      * </p>
56      */

57     public BeanComparator() {
58         this( null );
59     }
60
61     /**
62      * <p>Constructs a property-based comparator for beans.
63      * This compares two beans by the property
64      * specified in the property parameter. This constructor creates
65      * a <code>BeanComparator</code> that uses a <code>ComparableComparator</code>
66      * to compare the property values.
67      * </p>
68      *
69      * <p>Passing "null" to this constructor will cause the BeanComparator
70      * to compare objects based on natural order, that is
71      * <code>java.lang.Comparable</code>.
72      * </p>
73      *
74      * @param property String Name of a bean property, which may contain the
75      * name of a simple, nested, indexed, mapped, or combined
76      * property. See {@link PropertyUtilsBean} for property query language syntax.
77      * If the property passed in is null then the actual objects will be compared
78      */

79     public BeanComparator( String JavaDoc property ) {
80         this( property, ComparableComparator.getInstance() );
81     }
82
83     /**
84      * Constructs a property-based comparator for beans.
85      * This constructor creates
86      * a BeanComparator that uses the supplied Comparator to compare
87      * the property values.
88      *
89      * @param property Name of a bean property, can contain the name
90      * of a simple, nested, indexed, mapped, or combined
91      * property. See {@link PropertyUtilsBean} for property query language
92      * syntax.
93      * @param comparator BeanComparator will pass the values of the
94      * specified bean property to this Comparator.
95      * If your bean property is not a comparable or
96      * contains null values, a suitable comparator
97      * may be supplied in this constructor.
98      */

99     public BeanComparator( String JavaDoc property, Comparator JavaDoc comparator ) {
100         setProperty( property );
101         this.comparator = comparator;
102     }
103
104     /**
105      * Sets the method to be called to compare two JavaBeans
106      *
107      * @param property String method name to call to compare
108      * If the property passed in is null then the actual objects will be compared
109      */

110     public void setProperty( String JavaDoc property ) {
111         this.property = property;
112     }
113
114
115     /**
116      * Gets the property attribute of the BeanComparator
117      *
118      * @return String method name to call to compare.
119      * A null value indicates that the actual objects will be compared
120      */

121     public String JavaDoc getProperty() {
122         return property;
123     }
124
125
126     /**
127      * Gets the Comparator being used to compare beans.
128      */

129     public Comparator JavaDoc getComparator() {
130         return comparator;
131     }
132
133
134     /**
135      * Compare two JavaBeans by their shared property.
136      * If {@link #getProperty} is null then the actual objects will be compared.
137      *
138      * @param o1 Object The first bean to get data from to compare against
139      * @param o2 Object The second bean to get data from to compare
140      * @return int negative or positive based on order
141      */

142     public int compare( Object JavaDoc o1, Object JavaDoc o2 ) {
143         
144         if ( property == null ) {
145             // compare the actual objects
146
return comparator.compare( o1, o2 );
147         }
148         
149         try {
150             Object JavaDoc value1 = PropertyUtils.getProperty( o1, property );
151             Object JavaDoc value2 = PropertyUtils.getProperty( o2, property );
152             return comparator.compare( value1, value2 );
153         }
154         catch ( Exception JavaDoc e ) {
155             throw new ClassCastException JavaDoc( e.toString() );
156         }
157     }
158     
159     /**
160      * Two <code>BeanComparator</code>'s are equals if and only if
161      * the wrapped comparators and the property names to be compared
162      * are equal.
163      */

164     public boolean equals(Object JavaDoc o) {
165         if (this == o) return true;
166         if (!(o instanceof BeanComparator)) return false;
167
168         final BeanComparator beanComparator = (BeanComparator) o;
169
170         if (!comparator.equals(beanComparator.comparator)) return false;
171         if (property != null)
172         {
173             if (!property.equals(beanComparator.property)) return false;
174         }
175         else
176         {
177             return (beanComparator.property == null);
178         }
179
180         return true;
181     }
182
183     /**
184      * Hashcode compatible with equals.
185      */

186     public int hashCode() {
187         int result;
188         result = comparator.hashCode();
189         return result;
190     }
191 }
192
Popular Tags