Source Code Cross Referenced for GenericComparator.java in  » Database-ORM » ProjectJulp » org » julp » util » 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 » Database ORM » ProjectJulp » org.julp.util.common 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.julp.util.common;
002:
003:        import java.lang.Class;
004:        import java.lang.reflect.Method;
005:        import java.io.Serializable;
006:        import java.util.Comparator;
007:        import java.lang.reflect.InvocationTargetException;
008:
009:        public class GenericComparator implements  Comparator, Serializable,
010:                Cloneable {
011:
012:            protected String methodName = null;
013:            protected Class[] parmClass = null;
014:            protected Object[] parms = null;
015:            protected boolean reverse = false;
016:            /** If compared Objects are not instaceof String this is ignored */
017:            protected boolean caseInsensitiveOrder;
018:
019:            public GenericComparator() {
020:            }
021:
022:            public GenericComparator(String methodName) {
023:                this .methodName = methodName;
024:                this .parmClass = new Class[0];
025:            }
026:
027:            public GenericComparator(String methodName, boolean reverse) {
028:                this .methodName = methodName;
029:                this .parmClass = new Class[0];
030:                this .reverse = reverse;
031:            }
032:
033:            public GenericComparator(String methodName, Object[] parms) {
034:                this .methodName = methodName;
035:                this .parmClass = new Class[parms.length];
036:                this .parms = parms;
037:                for (int i = 0; i < parms.length; i++) {
038:                    this .parmClass[i] = parms.getClass();
039:                }
040:            }
041:
042:            public GenericComparator(String methodName, Object[] parms,
043:                    boolean reverse) {
044:                this .methodName = methodName;
045:                this .parmClass = new Class[parms.length];
046:                this .reverse = reverse;
047:                this .parms = parms;
048:                for (int i = 0; i < parms.length; i++) {
049:                    this .parmClass[i] = parms.getClass();
050:                }
051:            }
052:
053:            public int compare(Object o1, Object o2) {
054:                Method objMethod = null;
055:                Object object1 = null;
056:                Object object2 = null;
057:                if (!(o1.getClass().isInstance(o2))) {
058:                    throw new ClassCastException("Classes do not match "
059:                            + o1.getClass().getName() + " : "
060:                            + o2.getClass().getName());
061:                }
062:                try {
063:                    objMethod = o1.getClass().getMethod(methodName, parmClass);
064:                } catch (NoSuchMethodException e) {
065:                    throw new RuntimeException("Method name: "
066:                            + this .methodName + " not found in class "
067:                            + o1.getClass().getName());
068:                }
069:                try {
070:                    object1 = objMethod.invoke(o1, parms);
071:                    object2 = objMethod.invoke(o2, parms);
072:                } catch (IllegalAccessException e1) {
073:                    throw new RuntimeException("Access denied to method "
074:                            + this .methodName + " in class "
075:                            + o1.getClass().getName());
076:                } catch (InvocationTargetException e2) {
077:                    throw new RuntimeException(this .methodName + " in class "
078:                            + o1.getClass().getName() + " threw an exception: "
079:                            + e2.getTargetException().getMessage(), e2
080:                            .getTargetException());
081:                }
082:
083:                if (reverse) {
084:                    if (object1 == null && object2 != null) {
085:                        return -1;
086:                    } else if (object1 != null && object2 == null) {
087:                        return -1;
088:                    } else if (object1 == null && object2 == null) {
089:                        return -1;
090:                    } else {
091:                        if (object1 instanceof  String
092:                                && isCaseInsensitiveOrder()) {
093:                            return -((String) object1)
094:                                    .compareToIgnoreCase((String) object2);
095:                        } else {
096:                            return -((Comparable) object1).compareTo(object2);
097:                        }
098:                    }
099:                } else {
100:                    if (object1 == null && object2 != null) {
101:                        return 1;
102:                    } else if (object1 != null && object2 == null) {
103:                        return -1;
104:                    } else if (object1 == null && object2 == null) {
105:                        return -1;
106:                    } else {
107:                        if (object1 instanceof  String
108:                                && isCaseInsensitiveOrder()) {
109:                            return ((String) object1)
110:                                    .compareToIgnoreCase((String) object2);
111:                        } else {
112:                            return ((Comparable) object1).compareTo(object2);
113:                        }
114:                    }
115:                }
116:            }
117:
118:            /** Getter for property methodName.
119:             * @return Value of property methodName.
120:             *
121:             */
122:            public java.lang.String getMethodName() {
123:                return methodName;
124:            }
125:
126:            /** Setter for property methodName.
127:             * @param methodName New value of property methodName.
128:             *
129:             */
130:            public void setMethodName(java.lang.String methodName) {
131:                this .methodName = methodName;
132:            }
133:
134:            /** Getter for property parmClass.
135:             * @return Value of property parmClass.
136:             *
137:             */
138:            public java.lang.Class[] getParmClass() {
139:                return this .parmClass;
140:            }
141:
142:            /** Setter for property parmClass.
143:             * @param parmClass New value of property parmClass.
144:             *
145:             */
146:            public void setParmClass(java.lang.Class[] parmClass) {
147:                this .parmClass = parmClass;
148:            }
149:
150:            /** Getter for property parms.
151:             * @return Value of property parms.
152:             *
153:             */
154:            public java.lang.Object[] getParms() {
155:                return this .parms;
156:            }
157:
158:            /** Setter for property parms.
159:             * @param parms New value of property parms.
160:             *
161:             */
162:            public void setParms(java.lang.Object[] parms) {
163:                this .parms = parms;
164:            }
165:
166:            /** Getter for property reverse.
167:             * @return Value of property reverse.
168:             *
169:             */
170:            public boolean isReverse() {
171:                return reverse;
172:            }
173:
174:            /** Setter for property reverse.
175:             * @param reverse New value of property reverse.
176:             *
177:             */
178:            public void setReverse(boolean reverse) {
179:                this .reverse = reverse;
180:            }
181:
182:            /**
183:             * Getter for property caseInsensitiveOrder.
184:             * @return Value of property caseInsensitiveOrder.
185:             */
186:            public boolean isCaseInsensitiveOrder() {
187:                return caseInsensitiveOrder;
188:            }
189:
190:            /**
191:             * Setter for property caseInsensitiveOrder.
192:             * @param caseInsensitiveOrder New value of property caseInsensitiveOrder.
193:             */
194:            public void setCaseInsensitiveOrder(boolean caseInsensitiveOrder) {
195:                this.caseInsensitiveOrder = caseInsensitiveOrder;
196:            }
197:
198:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.