KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nfunk > jep > function > PostfixMathCommand


1 /*****************************************************************************
2
3 JEP - Java Math Expression Parser 2.3.0
4       October 3 2004
5       (c) Copyright 2004, Nathan Funk and Richard Morris
6       See LICENSE.txt for license information.
7
8 *****************************************************************************/

9 package org.nfunk.jep.function;
10 import java.util.*;
11 import org.nfunk.jep.*;
12
13 /**
14  * Function classes extend this class. It is an implementation of the
15  * PostfixMathCommandI interface.
16  * <p>
17  * It includes a numberOfParameters member, that is checked when parsing the
18  * expression. This member should be initialized to an appropriate value for
19  * all classes extending this class. If an arbitrary number of parameters
20  * should be allowed, initialize this member to -1.
21  */

22 public class PostfixMathCommand implements PostfixMathCommandI
23 {
24     /**
25      * Number of parameters a the function requires. Initialize this value to
26      * -1 if any number of parameters should be allowed.
27      */

28     protected int numberOfParameters;
29     
30     /**
31      * Number of parameters to be used for the next run() invocation. Applies
32      * only if the required umber of parameters is variable
33      * (numberOfParameters = -1).
34      */

35     protected int curNumberOfParameters;
36     
37     /**
38      * Creates a new PostfixMathCommand class.
39      */

40     public PostfixMathCommand() {
41         numberOfParameters = 0;
42         curNumberOfParameters = 0;
43     }
44
45     /**
46      * Check whether the stack is not null, throw a ParseException if it is.
47      */

48     protected void checkStack(Stack inStack) throws ParseException {
49         /* Check if stack is null */
50         if (null == inStack) {
51             throw new ParseException("Stack argument null");
52         }
53     }
54
55     /**
56      * Return the required number of parameters.
57      */

58     public int getNumberOfParameters() {
59         return numberOfParameters;
60     }
61     
62     /**
63      * Sets the number of current number of parameters used in the next call
64      * of run(). This method is only called when the reqNumberOfParameters is
65      * -1.
66      */

67     public void setCurNumberOfParameters(int n) {
68         curNumberOfParameters = n;
69     }
70     
71     /**
72      * Throws an exception because this method should never be called under
73      * normal circumstances. Each function should use it's own run() method
74      * for evaluating the function. This includes popping off the parameters
75      * from the stack, and pushing the result back on the stack.
76      */

77     public void run(Stack s) throws ParseException {
78         throw new ParseException("run() method of PostfixMathCommand called");
79     }
80 }
81
Popular Tags