KickJava   Java API By Example, From Geeks To Geeks.

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


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.jgap.gp.impl.*;
15
16 /**
17  * Pops a value from the stack after it has been pushed onto it (PushCommand)
18  *
19  * @author Klaus Meffert
20  * @since 3.0
21  */

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

47   public String JavaDoc getName() {
48     return "Pop";
49   }
50
51   public int execute_int(ProgramChromosome c, int n, Object JavaDoc[] args) {
52     check(c);
53     // Pop from stack.
54
// ---------------
55
if (getGPConfiguration().stackSize() < 1) {
56       throw new IllegalStateException JavaDoc("pop without push");
57     }
58     return ( (Integer JavaDoc) getGPConfiguration().popFromStack()).
59         intValue();
60   }
61
62   public long execute_long(ProgramChromosome c, int n, Object JavaDoc[] args) {
63     check(c);
64     if (getGPConfiguration().stackSize() < 1) {
65       throw new IllegalStateException JavaDoc("pop without push");
66     }
67     return ( (Long JavaDoc) getGPConfiguration().popFromStack()).longValue();
68   }
69
70   public double execute_double(ProgramChromosome c, int n, Object JavaDoc[] args) {
71     check(c);
72     if (getGPConfiguration().stackSize() < 1) {
73       throw new IllegalStateException JavaDoc("pop without push");
74     }
75     return ( (Double JavaDoc) getGPConfiguration().popFromStack()).doubleValue();
76   }
77
78   public float execute_float(ProgramChromosome c, int n, Object JavaDoc[] args) {
79     check(c);
80     if (getGPConfiguration().stackSize() < 1) {
81       throw new IllegalStateException JavaDoc("pop without push");
82     }
83     return ( (Float JavaDoc) getGPConfiguration().popFromStack()).floatValue();
84   }
85
86   public Object JavaDoc execute_object(ProgramChromosome c, int n, Object JavaDoc[] args) {
87     check(c);
88     if (getGPConfiguration().stackSize() < 1) {
89       throw new IllegalStateException JavaDoc("pop without push");
90     }
91     return getGPConfiguration().popFromStack();
92   }
93
94   public boolean isValid(ProgramChromosome a_program) {
95     return a_program.getCommandOfClass(0, Push.class) >= 0;
96   }
97 }
98
Popular Tags