KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Pushes a value onto the stack.
20  *
21  * @author Klaus Meffert
22  * @since 3.0
23  */

24 public class Push
25     extends CommandGene {
26   /** String containing the CVS revision. Read out via reflection!*/
27   private final static String JavaDoc CVS_REVISION = "$Revision: 1.9 $";
28
29   private Class JavaDoc m_type;
30
31   public Push(final GPConfiguration a_conf, Class JavaDoc a_type)
32       throws InvalidConfigurationException {
33     this(a_conf, a_type, 0, 0);
34   }
35
36   public Push(final GPConfiguration a_conf, Class JavaDoc a_type, int a_subReturnType,
37               int a_subChildType)
38       throws InvalidConfigurationException {
39     super(a_conf, 1, CommandGene.VoidClass, a_subReturnType, a_subChildType);
40     m_type = a_type;
41   }
42
43   public String JavaDoc toString() {
44     return "push &1";
45   }
46
47   /**
48    * @return textual name of this command
49    *
50    * @author Klaus Meffert
51    * @since 3.2
52    */

53   public String JavaDoc getName() {
54     return "Push";
55   }
56
57   public void execute_void(ProgramChromosome c, int n, Object JavaDoc[] args) {
58     check(c);
59     int value = c.execute_int(n, 0, args);
60     // Push onto stack.
61
// ----------------
62
pushIt(new Integer JavaDoc(value));
63   }
64
65   public int execute_int(ProgramChromosome c, int n, Object JavaDoc[] args) {
66     check(c);
67     int value = c.execute_int(n, 0, args);
68     // Push onto stack.
69
// ----------------
70
pushIt(new Integer JavaDoc(value));
71     return value;
72   }
73
74   public long execute_long(ProgramChromosome c, int n, Object JavaDoc[] args) {
75     check(c);
76     long value = c.execute_long(n, 0, args);
77     // Push onto stack.
78
// ----------------
79
pushIt(new Long JavaDoc(value));
80     return value;
81   }
82
83   public double execute_double(ProgramChromosome c, int n, Object JavaDoc[] args) {
84     check(c);
85     double value = c.execute_double(n, 0, args);
86     pushIt(new Double JavaDoc(value));
87     return value;
88   }
89
90   public float execute_float(ProgramChromosome c, int n, Object JavaDoc[] args) {
91     check(c);
92     float value = c.execute_float(n, 0, args);
93     pushIt(new Float JavaDoc(value));
94     return value;
95   }
96
97   public Object JavaDoc execute_object(ProgramChromosome c, int n, Object JavaDoc[] args) {
98     check(c);
99     Object JavaDoc value = c.execute_object(n, 0, args);
100     pushIt(value);
101     return value;
102   }
103
104   public boolean isAffectGlobalState() {
105     return true; /**@todo use this information*/
106   }
107
108   public boolean isValid(ProgramChromosome a_program) {
109     /**@todo consider n (execute_int...)*/
110     return a_program.getCommandOfClass(0, Pop.class) >= 0;
111   }
112
113   /**
114    * Helper method.
115    * @param a_value the value to push onto the stack
116    */

117   protected void pushIt(Object JavaDoc a_value) {
118     getGPConfiguration().pushToStack(a_value);
119   }
120
121   public Class JavaDoc getChildType(IGPProgram a_ind, int a_chromNum) {
122     return m_type;
123   }
124
125   /**
126    * The compareTo-method.
127    *
128    * @param a_other the other object to compare
129    * @return -1, 0, 1
130    *
131    * @author Klaus Meffert
132    * @since 3.0
133    */

134   public int compareTo(Object JavaDoc a_other) {
135     if (a_other == null) {
136       return 1;
137     }
138     else {
139       Push other = (Push) a_other;
140       return new CompareToBuilder()
141           .append(m_type, other.m_type)
142           .toComparison();
143     }
144   }
145
146   /**
147    * The equals-method.
148    *
149    * @param a_other the other object to compare
150    * @return true if the objects are seen as equal
151    *
152    * @author Klaus Meffert
153    * @since 3.0
154    */

155   public boolean equals(Object JavaDoc a_other) {
156     if (a_other == null) {
157       return false;
158     }
159     else {
160       try {
161         Push other = (Push) a_other;
162         return new EqualsBuilder()
163             .append(m_type, other.m_type)
164             .isEquals();
165       } catch (ClassCastException JavaDoc cex) {
166         return false;
167       }
168     }
169   }
170 }
171
Popular Tags