OSDN Git Service

CVSリポジトリから移行。
[rpolishcomop/repo.git] / expression-computer / src / main / java / jp / sourceforge / expression_computer / node / RelationalExpressionNode.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.node;
33
34 import jp.sourceforge.expression_computer.CompileContext;
35 import jp.sourceforge.expression_computer.Node;
36 import jp.sourceforge.expression_computer.command.GreaterThanCommand;
37 import jp.sourceforge.expression_computer.command.GreaterThanEqualCommand;
38 import jp.sourceforge.expression_computer.command.LessThanCommand;
39 import jp.sourceforge.expression_computer.command.LessThanEqualCommand;
40
41 /**
42  * <p>
43  * Relational Expressionを表すノードです。
44  * </p>
45  * 
46  * @author uguu@users.sourceforge.jp
47  */
48 public final class RelationalExpressionNode extends AbstractNode implements OperandNode {
49
50     private Node[] nodes;
51
52     /**
53      * <p>
54      * インスタンスを初期化します。
55      * </p>
56      * 
57      * @param nodes
58      *            式の要素。"項 (演算子 項)+"という順序で並んでいる必要があります。<br>
59      *            nullの場合、配列にnullがある場合、{@link NullPointerException}例外をスローします。<br>
60      *            配列の要素の並び順が間違えている場合、配列の要素数が3未満の場合、配列の要素数が奇数ではない場合、{@link IllegalArgumentException}例外をスローします。<br>
61      */
62     public RelationalExpressionNode(Node[] nodes) {
63         if (nodes == null) {
64             throw new NullPointerException("nodesがnullです。");
65         }
66         for (int i = 0; i < nodes.length; i++) {
67             if (nodes[i] == null) {
68                 throw new NullPointerException("nodes[" + i + "]がnullです。");
69             }
70             if ((i % 2 == 0) && !(nodes[i] instanceof OperandNode)) {
71                 throw new IllegalArgumentException("nodes[" + i + "]が項ではありません。");
72             } else if ((i % 2 == 1) && !(nodes[i] instanceof RelationalExpressionNode.OperatorNode)) {
73                 throw new IllegalArgumentException("nodes[" + i + "]が演算子ではありません。");
74             }
75         }
76         if (nodes.length < 3) {
77             throw new IllegalArgumentException("nodes.length < 3です。");
78         }
79         if ((nodes.length % 2) != 1) {
80             throw new IllegalArgumentException("nodes.lengthが奇数ではありません。");
81         }
82
83         this.nodes = (Node[]) nodes.clone();
84     }
85
86     /**
87      * {@inheritDoc}
88      */
89     public void compile(CompileContext context) {
90         this.nodes[0].compile(context);
91         for (int i = 2; i < this.nodes.length; i += 2) {
92             this.nodes[i].compile(context);
93             this.nodes[i - 1].compile(context);
94         }
95     }
96
97     /**
98      * {@inheritDoc}
99      */
100     public Node[] getChildren() {
101         return (Node[]) this.nodes.clone();
102     }
103
104     /**
105      * {@inheritDoc}
106      */
107     public String toString() {
108         return this.getClass().getName() + this.toChildrenString();
109     }
110
111     /**
112      * <p>
113      * RelationalExpressionNodeの演算子を表す抽象基底ノードです。
114      * </p>
115      * 
116      * @author uguu@users.sourceforge.jp
117      */
118     public abstract static class OperatorNode extends AbstractNode {
119
120         /**
121          * {@inheritDoc}
122          */
123         public final Node[] getChildren() {
124             return new Node[0];
125         }
126
127     }
128
129     /**
130      * <p>
131      * RelationalExpressionNodeの">"演算子を表すノードです。
132      * </p>
133      * 
134      * @author uguu@users.sourceforge.jp
135      */
136     public static final class GreaterThanNode extends RelationalExpressionNode.OperatorNode {
137
138         /**
139          * {@inheritDoc}
140          */
141         public void compile(CompileContext context) {
142             context.getCommandList().add(new GreaterThanCommand());
143         }
144
145         /**
146          * {@inheritDoc}
147          */
148         public String toString() {
149             return this.getClass().getName();
150         }
151
152     }
153
154     /**
155      * <p>
156      * RelationalExpressionNodeの"<"演算子を表すノードです。
157      * </p>
158      * 
159      * @author uguu@users.sourceforge.jp
160      */
161     public static final class LessThanNode extends RelationalExpressionNode.OperatorNode {
162
163         /**
164          * {@inheritDoc}
165          */
166         public void compile(CompileContext context) {
167             context.getCommandList().add(new LessThanCommand());
168         }
169
170         /**
171          * {@inheritDoc}
172          */
173         public String toString() {
174             return this.getClass().getName();
175         }
176
177     }
178
179     /**
180      * <p>
181      * RelationalExpressionNodeの">="演算子を表すノードです。
182      * </p>
183      * 
184      * @author uguu@users.sourceforge.jp
185      */
186     public static final class GreaterThanEqualNode extends RelationalExpressionNode.OperatorNode {
187
188         /**
189          * {@inheritDoc}
190          */
191         public void compile(CompileContext context) {
192             context.getCommandList().add(new GreaterThanEqualCommand());
193         }
194
195         /**
196          * {@inheritDoc}
197          */
198         public String toString() {
199             return this.getClass().getName();
200         }
201
202     }
203
204     /**
205      * <p>
206      * RelationalExpressionNodeの"<="演算子を表すノードです。
207      * </p>
208      * 
209      * @author uguu@users.sourceforge.jp
210      */
211     public static final class LessThanEqualNode extends RelationalExpressionNode.OperatorNode {
212
213         /**
214          * {@inheritDoc}
215          */
216         public void compile(CompileContext context) {
217             context.getCommandList().add(new LessThanEqualCommand());
218         }
219
220         /**
221          * {@inheritDoc}
222          */
223         public String toString() {
224             return this.getClass().getName();
225         }
226
227     }
228
229 }