OSDN Git Service

CVSリポジトリから移行。
[rpolishcomop/repo.git] / expression-computer / src / main / java / jp / sourceforge / expression_computer / command / FunctionCallCommand.java
1 /*
2  * Copyright (C) 2006 uguu@users.sourceforge.jp, All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  *    1. Redistributions of source code must retain the above copyright
9  *       notice, this list of conditions and the following disclaimer.
10  *
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.
14  *
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.
19  *
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.
30  */
31
32 package jp.sourceforge.expression_computer.command;
33
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;
39
40 /**
41  * <p>
42  * 関数を呼び出し、その結果をスタックにプッシュします。
43  * </p>
44  * 
45  * @author uguu@users.sourceforge.jp
46  */
47 public final class FunctionCallCommand implements Command {
48
49     private String name;
50
51     private int    argumentNumber;
52
53     /**
54      * <p>
55      * インスタンスを初期化します。
56      * </p>
57      * 
58      * @param name
59      *            関数名。<br>
60      *            nullの場合、{@link NullPointerException}例外をスローします。<br>
61      *            識別子の形式ではない場合、{@link IllegalArgumentException}例外をスローします。
62      * @param argumentNumber
63      *            引数の個数。<br>
64      *            0未満の場合、{@link IllegalArgumentException}例外をスローします。
65      */
66     public FunctionCallCommand(String name, int argumentNumber) {
67         if (name == null) {
68             throw new NullPointerException("nameがnullです。");
69         }
70         if (!Validator.isIdentifier(name)) {
71             throw new IllegalArgumentException("nameが識別子の形式ではありません。");
72         }
73         if (argumentNumber < 0) {
74             throw new IllegalArgumentException("{argumentNumber < 0}です。");
75         }
76         this.name = name;
77         this.argumentNumber = argumentNumber;
78     }
79
80     /**
81      * {@inheritDoc}
82      */
83     public void execute(ComputeContext context) {
84         Function func = context.getFunction(this.name);
85
86         double[] arguments = new double[this.argumentNumber];
87         for (int i = (this.argumentNumber - 1); i >= 0; i--) {
88             arguments[i] = context.popStack().getValue(context);
89         }
90
91         double resultValue = func.call(arguments);
92
93         context.pushStack(new FloatingPointLiteral(resultValue));
94     }
95
96     /**
97      * <p>
98      * 関数名を返します。
99      * </p>
100      * 
101      * @return 関数名。
102      */
103     public String getName() {
104         return this.name;
105     }
106
107     /**
108      * <p>
109      * 引数の個数を返します。
110      * </p>
111      * 
112      * @return 引数の個数。
113      */
114     public int getArgumentNumber() {
115         return this.argumentNumber;
116     }
117
118     /**
119      * {@inheritDoc}
120      */
121     public String toString() {
122         return this.getClass().getName() + "[name=" + this.name + ", argumentNumber=" + this.argumentNumber + "]";
123     }
124
125 }