KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > webapp > jonasadmin > common > BeanComparator


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: BeanComparator.java,v 1.2 2004/06/11 08:16:53 danesa Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.webapp.jonasadmin.common;
27
28 import java.util.Comparator JavaDoc;
29
30 import org.apache.commons.beanutils.PropertyUtils;
31
32 /**
33  * BeanComparator is a comparator for beans.
34  * It's used to compare a bean with its own properties or with its own string value
35  * (return by the <code>toString()</code> method).
36  *
37  * @author Michel-Ange ANTON
38  */

39
40 public class BeanComparator implements Comparator JavaDoc {
41
42 // --------------------------------------------------------- Instance Variables
43

44     private String JavaDoc[] m_Orders = null;
45
46 // --------------------------------------------------------- Constructors
47

48     /**
49      * Comparator by default (use the <code>toString()</code> methods).
50      */

51     public BeanComparator() {
52         m_Orders = null;
53     }
54
55     /**
56      * Comparator in order of the properties.
57      *
58      * @param p_Orders A array of the names of the properties
59      */

60     public BeanComparator(String JavaDoc[] p_Orders) {
61         m_Orders = p_Orders;
62     }
63
64 // --------------------------------------------------------- Public Methods
65

66     /**
67      * Compare two beans instances.
68      *
69      * @param p_O1 The first bean to compare
70      * @param p_O2 The second bean to compare
71      * @return 0 if equals, < 0 if first bean lower than second bean, > 0 if first bean upper than second bean.
72      */

73     public int compare(Object JavaDoc p_O1, Object JavaDoc p_O2) {
74         if (m_Orders != null) {
75             return compareOrder(p_O1, p_O2);
76         }
77         return compareDefault(p_O1, p_O2);
78     }
79
80     /**
81      * Indentical bean.
82      *
83      * @param p_Obj The bean to compare.
84      * @return True if identical.
85      */

86     public boolean equals(Object JavaDoc p_Obj) {
87         if (this.getClass().getName().equals(p_Obj.getClass().getName())) {
88             return (compare(this, p_Obj) == 0);
89         }
90         return false;
91     }
92
93     /**
94      * Compare the null of two objects.
95      *
96      * @param p_O1 The first object to compare
97      * @param p_O2 The second object to compare
98      * @return 0 if the two are null or the two not null, < 0 if second object is null, > 0 if first object is null.
99      */

100     public static int compareNull(Object JavaDoc p_O1, Object JavaDoc p_O2) {
101         int iRet = 0;
102         if (p_O1 != p_O2) {
103             if (p_O1 == null) {
104                 iRet = 1;
105             }
106             else if (p_O2 == null) {
107                 iRet = -1;
108             }
109         }
110         return iRet;
111     }
112
113     /**
114      * Compare two strings with the ignore case
115      * but if is identical, compare normaly with case.
116      *
117      * @param p_O1 The first string to compare
118      * @param p_O2 The second string to compare
119      * @return 0 if equals, < 0 if first string lower than second string, > 0 if first string upper than second string.
120      */

121     public static int compareString(String JavaDoc p_S1, String JavaDoc p_S2) {
122         int iRet = compareNull(p_S1, p_S2);
123         if (iRet == 0) {
124             try {
125                 iRet = p_S1.compareToIgnoreCase(p_S2);
126                 if (iRet == 0) {
127                     iRet = p_S1.compareTo(p_S2);
128                 }
129             }
130             catch (NullPointerException JavaDoc e) {
131                 iRet = 0;
132             }
133         }
134         return iRet;
135     }
136
137 // --------------------------------------------------------- Protected Methods
138

139     /**
140      * Compare the beans like a string (used the <code>toString()</code> method of the object).
141      * Used when order is unknown.
142      *
143      * @param p_O1 The first bean to compare
144      * @param p_O2 The second bean to compare
145      * @return 0 if equals, < 0 if first bean lower than second bean, > 0 if first bean upper than second bean.
146      */

147     protected int compareDefault(Object JavaDoc p_O1, Object JavaDoc p_O2) {
148         int iRet = compareNull(p_O1, p_O2);
149         if (iRet == 0) {
150             iRet = compareString(p_O1.toString(), p_O2.toString());
151         }
152         return iRet;
153     }
154
155     /**
156      * Compare the beans with the properties in order given.
157      * Used when order is known.
158      *
159      * @param p_O1 The first bean to compare
160      * @param p_O2 The second bean to compare
161      * @return 0 if equals, < 0 if first bean lower than second bean, > 0 if first bean upper than second bean.
162      */

163     protected int compareOrder(Object JavaDoc p_O1, Object JavaDoc p_O2) {
164         int iRet = 0;
165         Object JavaDoc oValue1, oValue2;
166         try {
167             for (int i = 0; i < m_Orders.length; i++) {
168                 // Get values
169
oValue1 = PropertyUtils.getProperty(p_O1, m_Orders[i]);
170                 oValue2 = PropertyUtils.getProperty(p_O2, m_Orders[i]);
171                 // Compare
172
iRet = compareNull(oValue1, oValue2);
173                 if (iRet == 0) {
174                     try {
175                         iRet = compareString(oValue1.toString(), oValue2.toString());
176                     }
177                     catch (NullPointerException JavaDoc e) {
178                         iRet = 0;
179                     }
180                 }
181                 // Continue in order if always equals
182
if (iRet != 0) {
183                     break;
184                 }
185             }
186         }
187         catch (Exception JavaDoc e) {
188             System.err.println("[" + e.getClass().getName() + "] " + e.getMessage());
189             iRet = 0;
190         }
191         return iRet;
192     }
193 }
Popular Tags