KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > taglib > core > grid > util > EntityFieldComparator


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
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 com.blandware.atleap.webapp.taglib.core.grid.util;
17
18 import org.apache.commons.beanutils.PropertyUtils;
19
20 import java.util.Comparator JavaDoc;
21
22 /**
23  * <p>Class used to compare fields of entities. Property may be indexed, mapped, or nested.
24  * </p>
25  * <p><a HREF="EntityFieldComparator.java.htm"><i>View Source</i></a></p>
26  *
27  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
28  * @version $Revision: 1.2 $ $Date: 2005/08/05 17:48:33 $
29  */

30 public class EntityFieldComparator implements Comparator JavaDoc {
31
32     // ~ Static variables
33

34     // ~ Instance variables
35

36     /**
37      * Name of field to sort by
38      */

39     protected String JavaDoc property;
40
41     /**
42      * Order of sorting by default is always ascending
43      * Can be reversed using <code>reverseOrder()</code> method
44      */

45     protected int order = 1;
46
47     /**
48      * Creates new instance of SortField
49      */

50     public EntityFieldComparator() {
51     }
52
53     /**
54      * Creates new instance of SortField
55      *
56      * @param property Name of property to compare
57      */

58     public EntityFieldComparator(String JavaDoc property) {
59         this.property = property;
60     }
61
62     /**
63      * <p>
64      * Compares two objects by property. If no <code>property</code> is specified,
65      * an attempt is taken to compare them as Comparable. Else firstly value of
66      * given property is extracted from each object. Null property is less than
67      * non-null one. If both properties are non-null, they are compared as
68      * Comparable.
69      * </p>
70      * <p>
71      * Note that if order was reversed, the comparison will work vice-versa
72      * </p>
73      *
74      * @param o1 First object
75      * @param o2 Second object
76      * @return -1 if first is less then second, +1 if it's more and 0 if equal
77      */

78     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
79         if ( o1 == null ) {
80             throw new NullPointerException JavaDoc("First argument must not be null");
81         }
82
83         if ( o2 == null ) {
84             throw new NullPointerException JavaDoc("Second argument must not be null");
85         }
86
87         // if property was not specified, check if objects implement comparable interface
88
// if they are, compare them, otherwise return 0 (assume that they are equal, because there is no way to compare them)
89
if ( property == null ) {
90             if ( o1 instanceof Comparable JavaDoc && o2 instanceof Comparable JavaDoc ) {
91                 Comparable JavaDoc c1 = (Comparable JavaDoc) o1;
92                 Comparable JavaDoc c2 = (Comparable JavaDoc) o2;
93                 return order * c1.compareTo(c2);
94             } else {
95                 return 0;
96             }
97         }
98
99         Object JavaDoc p1 = null;
100         Object JavaDoc p2 = null;
101         try {
102             p1 = PropertyUtils.getProperty(o1, property);
103         } catch ( Exception JavaDoc e ) {
104             throw new IllegalArgumentException JavaDoc("Exception " + e.getClass().getName() + " with message '" + e.getMessage() + "' while trying to obtain value of property '" + property + "' from instance of " + o1.getClass().getName());
105         }
106
107         try {
108             p2 = PropertyUtils.getProperty(o2, property);
109         } catch ( Exception JavaDoc e ) {
110             throw new IllegalArgumentException JavaDoc("Exception " + e.getClass().getName() + " with message '" + e.getMessage() + "' while trying to obtain value of property '" + property + "' from instance of " + o2.getClass().getName());
111         }
112
113         // assume, that null is less than any object which is not null
114
if ( p1 == null && p2 == null ) {
115             return 0;
116         } else if ( p1 == null && p2 != null ) {
117             return -1 * order;
118         } else if ( p1 != null & p2 == null ) {
119             return order;
120         }
121
122         if ( !(p1 instanceof Comparable JavaDoc) ) {
123             throw new ClassCastException JavaDoc("Value of property '" + property + "' must implement java.lang.Comparable interface");
124         }
125
126         Comparable JavaDoc c1 = (Comparable JavaDoc) p1;
127         Comparable JavaDoc c2 = (Comparable JavaDoc) p2;
128
129         return order * (c1.compareTo(c2));
130     }
131
132     /**
133      * Reverses the order of comparing
134      *
135      */

136     public void reverseOrder() {
137         order = -order;
138     }
139
140 }
141
Popular Tags