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: }
|