KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > gp > function > Increment


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.gp.function;
11
12 import org.jgap.*;
13 import org.jgap.gp.*;
14 import org.apache.commons.lang.builder.CompareToBuilder;
15 import org.apache.commons.lang.builder.EqualsBuilder;
16 import org.jgap.gp.impl.*;
17
18 /**
19  * The increment operation.
20  *
21  * @author Konrad Odell
22  * @author Klaus Meffert
23  * @since 3.0
24  */

25 public class Increment
26     extends MathCommand {
27   /** String containing the CVS revision. Read out via reflection!*/
28   private static final String JavaDoc CVS_REVISION = "$Revision: 1.8 $";
29
30   private int m_increment;
31
32   /**
33    * Constructor for using an increment of 1.
34    *
35    * @param a_conf the configuration to use
36    * @param a_type the type of the terminal to increment (e.g. IntegerClass)
37    * @throws InvalidConfigurationException
38    *
39    * @author Klaus Meffert
40    * @since 3.0
41    */

42   public Increment(final GPConfiguration a_conf, Class JavaDoc a_type)
43       throws InvalidConfigurationException {
44     this(a_conf, a_type, 1);
45   }
46
47   /**
48    * Constructor to freely choose increment.
49    *
50    * @param a_conf the configuration to use
51    * @param a_type the type of the terminal to increment (e.g. IntegerClass)
52    * @param a_increment the increment to use, may also be negative
53    * @throws InvalidConfigurationException
54    *
55    * @author Klaus Meffert
56    * @since 3.0
57    */

58   public Increment(final GPConfiguration a_conf, Class JavaDoc a_type, int a_increment)
59       throws InvalidConfigurationException {
60     this(a_conf, a_type, a_increment, 0, 0);
61   }
62
63   public Increment(final GPConfiguration a_conf, Class JavaDoc a_type, int a_increment,
64                    int a_subReturnType, int a_subChildType)
65       throws InvalidConfigurationException {
66     super(a_conf, 1, a_type, a_subReturnType, a_subChildType);
67     m_increment = a_increment;
68   }
69
70   public String JavaDoc toString() {
71     if (m_increment == 1) {
72       return "INC(&1)";
73     }
74     else {
75       return "INC(" + m_increment + ", &1)";
76     }
77   }
78
79   /**
80    * @return textual name of this command
81    *
82    * @author Klaus Meffert
83    * @since 3.2
84    */

85   public String JavaDoc getName() {
86     return "INC";
87   }
88
89   public int execute_int(ProgramChromosome c, int n, Object JavaDoc[] args) {
90     return c.execute_int(n, 0, args) + m_increment;
91   }
92
93   public long execute_long(ProgramChromosome c, int n, Object JavaDoc[] args) {
94     return c.execute_long(n, 0, args) + m_increment;
95   }
96
97   public float execute_float(ProgramChromosome c, int n, Object JavaDoc[] args) {
98     return c.execute_float(n, 0, args) + m_increment;
99   }
100
101   public double execute_double(ProgramChromosome c, int n, Object JavaDoc[] args) {
102     return c.execute_double(n, 0, args) + m_increment;
103   }
104
105   public Object JavaDoc execute_object(ProgramChromosome c, int n, Object JavaDoc[] args) {
106     return ( (Compatible) c.execute_object(n, 0, args)).execute_increment();
107   }
108
109   protected interface Compatible {
110     public Object JavaDoc execute_increment();
111   }
112   /**
113    * The compareTo-method.
114    *
115    * @param a_other the other object to compare
116    * @return -1, 0, 1
117    *
118    * @author Klaus Meffert
119    * @since 3.0
120    */

121   public int compareTo(Object JavaDoc a_other) {
122     if (a_other == null) {
123       return 1;
124     }
125     else {
126       Increment other = (Increment) a_other;
127       return new CompareToBuilder()
128           .append(m_increment, other.m_increment)
129           .toComparison();
130     }
131   }
132
133   /**
134    * The equals-method.
135    *
136    * @param a_other the other object to compare
137    * @return true if the objects are seen as equal
138    *
139    * @author Klaus Meffert
140    * @since 3.0
141    */

142   public boolean equals(Object JavaDoc a_other) {
143     if (a_other == null) {
144       return false;
145     }
146     else {
147       try {
148         Increment other = (Increment) a_other;
149         return new EqualsBuilder()
150             .append(m_increment, other.m_increment)
151             .isEquals();
152       } catch (ClassCastException JavaDoc cex) {
153         return false;
154       }
155     }
156   }
157 }
158
Popular Tags