001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: BeanComparator.java 4940 2004-06-11 08:16:53Z danesa $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.webapp.jonasadmin.common;
025:
026: import java.util.Comparator;
027:
028: import org.apache.commons.beanutils.PropertyUtils;
029:
030: /**
031: * BeanComparator is a comparator for beans.
032: * It's used to compare a bean with its own properties or with its own string value
033: * (return by the <code>toString()</code> method).
034: *
035: * @author Michel-Ange ANTON
036: */
037:
038: public class BeanComparator implements Comparator {
039:
040: // --------------------------------------------------------- Instance Variables
041:
042: private String[] m_Orders = null;
043:
044: // --------------------------------------------------------- Constructors
045:
046: /**
047: * Comparator by default (use the <code>toString()</code> methods).
048: */
049: public BeanComparator() {
050: m_Orders = null;
051: }
052:
053: /**
054: * Comparator in order of the properties.
055: *
056: * @param p_Orders A array of the names of the properties
057: */
058: public BeanComparator(String[] p_Orders) {
059: m_Orders = p_Orders;
060: }
061:
062: // --------------------------------------------------------- Public Methods
063:
064: /**
065: * Compare two beans instances.
066: *
067: * @param p_O1 The first bean to compare
068: * @param p_O2 The second bean to compare
069: * @return 0 if equals, < 0 if first bean lower than second bean, > 0 if first bean upper than second bean.
070: */
071: public int compare(Object p_O1, Object p_O2) {
072: if (m_Orders != null) {
073: return compareOrder(p_O1, p_O2);
074: }
075: return compareDefault(p_O1, p_O2);
076: }
077:
078: /**
079: * Indentical bean.
080: *
081: * @param p_Obj The bean to compare.
082: * @return True if identical.
083: */
084: public boolean equals(Object p_Obj) {
085: if (this .getClass().getName()
086: .equals(p_Obj.getClass().getName())) {
087: return (compare(this , p_Obj) == 0);
088: }
089: return false;
090: }
091:
092: /**
093: * Compare the null of two objects.
094: *
095: * @param p_O1 The first object to compare
096: * @param p_O2 The second object to compare
097: * @return 0 if the two are null or the two not null, < 0 if second object is null, > 0 if first object is null.
098: */
099: public static int compareNull(Object p_O1, Object p_O2) {
100: int iRet = 0;
101: if (p_O1 != p_O2) {
102: if (p_O1 == null) {
103: iRet = 1;
104: } else if (p_O2 == null) {
105: iRet = -1;
106: }
107: }
108: return iRet;
109: }
110:
111: /**
112: * Compare two strings with the ignore case
113: * but if is identical, compare normaly with case.
114: *
115: * @param p_O1 The first string to compare
116: * @param p_O2 The second string to compare
117: * @return 0 if equals, < 0 if first string lower than second string, > 0 if first string upper than second string.
118: */
119: public static int compareString(String p_S1, String p_S2) {
120: int iRet = compareNull(p_S1, p_S2);
121: if (iRet == 0) {
122: try {
123: iRet = p_S1.compareToIgnoreCase(p_S2);
124: if (iRet == 0) {
125: iRet = p_S1.compareTo(p_S2);
126: }
127: } catch (NullPointerException e) {
128: iRet = 0;
129: }
130: }
131: return iRet;
132: }
133:
134: // --------------------------------------------------------- Protected Methods
135:
136: /**
137: * Compare the beans like a string (used the <code>toString()</code> method of the object).
138: * Used when order is unknown.
139: *
140: * @param p_O1 The first bean to compare
141: * @param p_O2 The second bean to compare
142: * @return 0 if equals, < 0 if first bean lower than second bean, > 0 if first bean upper than second bean.
143: */
144: protected int compareDefault(Object p_O1, Object p_O2) {
145: int iRet = compareNull(p_O1, p_O2);
146: if (iRet == 0) {
147: iRet = compareString(p_O1.toString(), p_O2.toString());
148: }
149: return iRet;
150: }
151:
152: /**
153: * Compare the beans with the properties in order given.
154: * Used when order is known.
155: *
156: * @param p_O1 The first bean to compare
157: * @param p_O2 The second bean to compare
158: * @return 0 if equals, < 0 if first bean lower than second bean, > 0 if first bean upper than second bean.
159: */
160: protected int compareOrder(Object p_O1, Object p_O2) {
161: int iRet = 0;
162: Object oValue1, oValue2;
163: try {
164: for (int i = 0; i < m_Orders.length; i++) {
165: // Get values
166: oValue1 = PropertyUtils.getProperty(p_O1, m_Orders[i]);
167: oValue2 = PropertyUtils.getProperty(p_O2, m_Orders[i]);
168: // Compare
169: iRet = compareNull(oValue1, oValue2);
170: if (iRet == 0) {
171: try {
172: iRet = compareString(oValue1.toString(),
173: oValue2.toString());
174: } catch (NullPointerException e) {
175: iRet = 0;
176: }
177: }
178: // Continue in order if always equals
179: if (iRet != 0) {
180: break;
181: }
182: }
183: } catch (Exception e) {
184: System.err.println("[" + e.getClass().getName() + "] "
185: + e.getMessage());
186: iRet = 0;
187: }
188: return iRet;
189: }
190: }
|