Source Code Cross Referenced for BeanComparator.java in  » J2EE » JOnAS-4.8.6 » org » objectweb » jonas » webapp » jonasadmin » common » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » JOnAS 4.8.6 » org.objectweb.jonas.webapp.jonasadmin.common 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.