001: /*
002: * This file is part of JGAP.
003: *
004: * JGAP offers a dual license model containing the LGPL as well as the MPL.
005: *
006: * For licensing information please see the file license.txt included with JGAP
007: * or have a look at the top of class org.jgap.Chromosome which representatively
008: * includes the JGAP license policy applicable for any file delivered with JGAP.
009: */
010: package org.jgap.impl;
011:
012: import org.jgap.*;
013: import org.jgap.util.*;
014: import java.io.*;
015:
016: /**
017: * Default implementation for comparing Comparables. Boolean values are also
018: * covered with this handler as the Boolean class has no compareTo method.
019: *
020: * @author Klaus Meffert
021: * @since 2.6
022: */
023: public class DefaultCompareToHandler implements ICompareToHandler,
024: ICloneable, Serializable, Comparable {
025: /** String containing the CVS revision. Read out via reflection!*/
026: private static final String CVS_REVISION = "$Revision: 1.7 $";
027:
028: public boolean isHandlerFor(final Object a_obj, final Class a_clazz) {
029: Class clazz;
030: if (a_clazz == null) {
031: if (a_obj == null) {
032: return false;
033: }
034: clazz = a_obj.getClass();
035: } else {
036: clazz = a_clazz;
037: }
038: if (Comparable.class.isAssignableFrom(clazz)) {
039: return true;
040: } else {
041: if (clazz != null && Boolean.class == clazz) {
042: return true;
043: } else {
044: return false;
045: }
046: }
047: }
048:
049: public Object perform(final Object a_obj, final Class a_class,
050: final Object a_params) throws Exception {
051: int i;
052: if (a_obj == null) {
053: if (a_params != null) {
054: i = -1;
055: } else {
056: i = 0;
057: }
058: } else if (a_params == null) {
059: i = 1;
060: } else {
061: if (a_obj.getClass() == Boolean.class) {
062: boolean b1 = ((Boolean) a_obj).booleanValue();
063: boolean b2 = ((Boolean) a_params).booleanValue();
064: if (b1 == b2) {
065: i = 0;
066: } else if (b1) {
067: i = 1;
068: } else
069: i = -1;
070: } else {
071: i = ((Comparable) a_obj).compareTo(a_params);
072: }
073: }
074: return new Integer(i);
075: }
076:
077: /**
078: * @return deep clone of this instance
079: *
080: * @author Klaus Meffert
081: * @since 3.2
082: */
083: public Object clone() {
084: return new DefaultCompareToHandler();
085: }
086:
087: /**
088: * @param a_other sic
089: * @return as always
090: *
091: * @author Klaus Meffert
092: * @since 3.2
093: */
094: public int compareTo(Object a_other) {
095: if (a_other.getClass().equals(getClass())) {
096: return 0;
097: } else {
098: return getClass().getName().compareTo(
099: a_other.getClass().getName());
100: }
101: }
102: }
|