2 * Copyright (C) 2006 uguu@users.sourceforge.jp, All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Clarkware Consulting, Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without prior written permission. For written
18 * permission, please contact clarkware@clarkware.com.
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
21 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
22 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 * CLARKWARE CONSULTING OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
26 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 package jp.sourceforge.expression_computer.command;
34 import jp.sourceforge.expression_computer.Command;
35 import jp.sourceforge.expression_computer.ComputeContext;
36 import jp.sourceforge.expression_computer.Function;
37 import jp.sourceforge.expression_computer.type.FloatingPointLiteral;
38 import jp.sourceforge.expression_computer.util.Validator;
42 * 関数を呼び出し、その結果をスタックにプッシュします。
45 * @author uguu@users.sourceforge.jp
47 public final class FunctionCallCommand implements Command {
51 private int argumentNumber;
60 * nullの場合、{@link NullPointerException}例外をスローします。<br>
61 * 識別子の形式ではない場合、{@link IllegalArgumentException}例外をスローします。
62 * @param argumentNumber
64 * 0未満の場合、{@link IllegalArgumentException}例外をスローします。
66 public FunctionCallCommand(String name, int argumentNumber) {
68 throw new NullPointerException("nameがnullです。");
70 if (!Validator.isIdentifier(name)) {
71 throw new IllegalArgumentException("nameが識別子の形式ではありません。");
73 if (argumentNumber < 0) {
74 throw new IllegalArgumentException("{argumentNumber < 0}です。");
77 this.argumentNumber = argumentNumber;
83 public void execute(ComputeContext context) {
84 Function func = context.getFunction(this.name);
86 double[] arguments = new double[this.argumentNumber];
87 for (int i = (this.argumentNumber - 1); i >= 0; i--) {
88 arguments[i] = context.popStack().getValue(context);
91 double resultValue = func.call(arguments);
93 context.pushStack(new FloatingPointLiteral(resultValue));
103 public String getName() {
114 public int getArgumentNumber() {
115 return this.argumentNumber;
121 public String toString() {
122 return this.getClass().getName() + "[name=" + this.name + ", argumentNumber=" + this.argumentNumber + "]";