KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > impl > DefaultCompareToHandler


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.impl;
11
12 import org.jgap.*;
13 import org.jgap.util.*;
14 import java.io.*;
15
16 /**
17  * Default implementation for comparing Comparables. Boolean values are also
18  * covered with this handler as the Boolean class has no compareTo method.
19  *
20  * @author Klaus Meffert
21  * @since 2.6
22  */

23 public class DefaultCompareToHandler
24     implements ICompareToHandler, ICloneable, Serializable, Comparable JavaDoc {
25   /** String containing the CVS revision. Read out via reflection!*/
26   private static final String JavaDoc CVS_REVISION = "$Revision: 1.6 $";
27
28   public boolean isHandlerFor(final Object JavaDoc a_obj, final Class JavaDoc a_clazz) {
29     Class JavaDoc clazz;
30     if (a_clazz == null) {
31       if (a_obj == null) {
32         return false;
33       }
34       clazz = a_obj.getClass();
35     }
36     else {
37       clazz = a_clazz;
38     }
39     if (Comparable JavaDoc.class.isAssignableFrom(clazz)) {
40       return true;
41     }
42     else {
43       if (clazz != null && Boolean JavaDoc.class == clazz) {
44         return true;
45       }
46       else {
47         return false;
48       }
49     }
50   }
51
52   public Object JavaDoc perform(final Object JavaDoc a_obj, final Class JavaDoc a_class,
53                         final Object JavaDoc a_params)
54       throws Exception JavaDoc {
55     int i;
56     if (a_obj == null) {
57       if (a_params != null) {
58         i = -1;
59       }
60       else {
61         i = 0;
62       }
63     }
64     else if (a_params == null) {
65       i = 1;
66     }
67     else {
68       if (a_obj.getClass() == Boolean JavaDoc.class) {
69         boolean b1 = ( (Boolean JavaDoc) a_obj).booleanValue();
70         boolean b2 = ( (Boolean JavaDoc) a_params).booleanValue();
71         if (b1 == b2) {
72           i = 0;
73         }
74         else if (b1) {
75           i = 1;
76         }
77         else
78           i = -1;
79       }
80       else {
81         i = ( (Comparable JavaDoc) a_obj).compareTo(a_params);
82       }
83     }
84     return new Integer JavaDoc(i);
85   }
86
87   /**
88    * @return deep clone of this instance
89    *
90    * @author Klaus Meffert
91    * @since 3.2
92    */

93   public Object JavaDoc clone() {
94     return new DefaultCompareToHandler();
95   }
96
97   /**
98    * @param a_other sic
99    * @return as always
100    *
101    * @author Klaus Meffert
102    * @since 3.2
103    */

104   public int compareTo(Object JavaDoc a_other) {
105     if (a_other.getClass().equals(getClass())) {
106       return 0;
107     }
108     else {
109       return getClass().getName().compareTo(a_other.getClass().getName());
110     }
111   }
112 }
113
Popular Tags