KickJava   Java API By Example, From Geeks To Geeks.

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


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
14 /**
15  * Base class for all Genes based on numbers.
16  *
17  * @author Klaus Meffert
18  * @since 1.1 (most code moved and adapted from IntegerGene)
19  */

20 public abstract class NumberGene
21     extends BaseGene {
22   /** String containing the CVS revision. Read out via reflection!*/
23   private static final String JavaDoc CVS_REVISION = "$Revision: 1.22 $";
24
25   /**
26    * References the internal value (allele) of this Gene
27    * E.g., for DoubleGene this is of type Double
28    */

29   private Object JavaDoc m_value;
30
31   public NumberGene(Configuration a_config) throws InvalidConfigurationException {
32     super(a_config);
33   }
34
35   /**
36    * Compares this NumberGene with the specified object (which must also
37    * be a NumberGene) for order, which is determined by the number value of
38    * this Gene compared to the one provided for comparison.
39    *
40    * @param a_other the NumberGene to be compared to this NumberGene
41    * @return a negative integer, zero, or a positive integer as this object
42    * is less than, equal to, or greater than the object provided for comparison
43    *
44    * @throws ClassCastException if the specified object's type prevents it
45    * from being compared to this NumberGene
46    *
47    * @author Klaus Meffert
48    * @since 1.1
49    */

50   public int compareTo(final Object JavaDoc a_other) {
51     NumberGene otherGene = (NumberGene) a_other;
52     // First, if the other gene (or its value) is null, then this is
53
// the greater allele. Otherwise, just use the overridden compareToNative
54
// method to perform the comparison.
55
// ----------------------------------------------------------------------
56
if (otherGene == null) {
57       return 1;
58     }
59     else if (otherGene.m_value == null) {
60       // Check if type corresponds (because we could have a type not inherited
61
// from NumberGene).
62
// ---------------------------------------------------------------------
63
if (!otherGene.getClass().equals(getClass())) {
64         throw new ClassCastException JavaDoc(
65             "Comparison not possible: different types!");
66       }
67       // If our value is also null, then we're the same. Otherwise,
68
// this is the greater gene.
69
// ----------------------------------------------------------
70
if (m_value == null) {
71         if (isCompareApplicationData()) {
72           return compareApplicationData(getApplicationData(),
73                                         otherGene.getApplicationData());
74         }
75         else {
76           return 0;
77         }
78       }
79       else {
80         return 1;
81       }
82     }
83     else {
84       try {
85         if (!otherGene.getClass().equals(getClass())) {
86           throw new ClassCastException JavaDoc(
87               "Comparison not possible: different types!");
88         }
89         if (m_value == null) {
90           return -1;
91         }
92         int res = compareToNative(m_value, otherGene.m_value);
93         if (res == 0) {
94           if (isCompareApplicationData()) {
95             return compareApplicationData(getApplicationData(),
96                                           otherGene.getApplicationData());
97           }
98           else {
99             return 0;
100           }
101         }
102         else {
103           return res;
104         }
105       }
106       catch (ClassCastException JavaDoc e) {
107         throw e;
108       }
109     }
110   }
111
112   /**
113    * Compares to objects by first casting them into their expected type
114    * (e.g. Integer for IntegerGene) and then calling the compareTo-method
115    * of the casted type.
116    * @param a_o1 first object to be compared, always is not null
117    * @param a_o2 second object to be compared, always is not null
118    * @return a negative integer, zero, or a positive integer as this object
119    * is less than, equal to, or greater than the object provided for comparison
120    *
121    * @author Klaus Meffert
122    * @since 1.1
123    */

124   protected abstract int compareToNative(Object JavaDoc a_o1, Object JavaDoc a_o2);
125
126   /**
127    * Sets the value (allele) of this Gene to the new given value. This class
128    * expects the value to be an instance of current type (e.g. Integer).
129    * If the value is above or below the upper or lower bounds, it will be
130    * mappped to within the allowable range.
131    *
132    * @param a_newValue the new value of this Gene instance
133    *
134    * @author Klaus Meffert
135    * @since 1.1
136    */

137   public void setAllele(final Object JavaDoc a_newValue) {
138     if (getConstraintChecker() != null) {
139       if (!getConstraintChecker().verify(this, a_newValue, null, -1)) {
140         return;
141       }
142     }
143     m_value = a_newValue;
144     // If the value isn't between the upper and lower bounds of this
145
// Gene, map it to a value within those bounds.
146
// -------------------------------------------------------------
147
mapValueToWithinBounds();
148   }
149
150   /**
151    * Maps the value of this NumberGene to within the bounds specified by
152    * the m_upperBounds and m_lowerBounds instance variables. The value's
153    * relative position within the integer range will be preserved within the
154    * bounds range (in other words, if the value is about halfway between the
155    * integer max and min, then the resulting value will be about halfway
156    * between the upper bounds and lower bounds). If the value is null or
157    * is already within the bounds, it will be left unchanged.
158    *
159    * @author Klaus Meffert
160    * @since 1.1
161    */

162   protected abstract void mapValueToWithinBounds();
163
164   protected Object JavaDoc getInternalValue() {
165     return m_value;
166   }
167 }
168
Popular Tags