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.node;
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;
43 * Relational Expressionを表すノードです。
46 * @author uguu@users.sourceforge.jp
48 public final class RelationalExpressionNode extends AbstractNode implements OperandNode {
58 * 式の要素。"項 (演算子 項)+"という順序で並んでいる必要があります。<br>
59 * nullの場合、配列にnullがある場合、{@link NullPointerException}例外をスローします。<br>
60 * 配列の要素の並び順が間違えている場合、配列の要素数が3未満の場合、配列の要素数が奇数ではない場合、{@link IllegalArgumentException}例外をスローします。<br>
62 public RelationalExpressionNode(Node[] nodes) {
64 throw new NullPointerException("nodesがnullです。");
66 for (int i = 0; i < nodes.length; i++) {
67 if (nodes[i] == null) {
68 throw new NullPointerException("nodes[" + i + "]がnullです。");
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 + "]が演算子ではありません。");
76 if (nodes.length < 3) {
77 throw new IllegalArgumentException("nodes.length < 3です。");
79 if ((nodes.length % 2) != 1) {
80 throw new IllegalArgumentException("nodes.lengthが奇数ではありません。");
83 this.nodes = (Node[]) nodes.clone();
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);
100 public Node[] getChildren() {
101 return (Node[]) this.nodes.clone();
107 public String toString() {
108 return this.getClass().getName() + this.toChildrenString();
113 * RelationalExpressionNodeの演算子を表す抽象基底ノードです。
116 * @author uguu@users.sourceforge.jp
118 public abstract static class OperatorNode extends AbstractNode {
123 public final Node[] getChildren() {
131 * RelationalExpressionNodeの">"演算子を表すノードです。
134 * @author uguu@users.sourceforge.jp
136 public static final class GreaterThanNode extends RelationalExpressionNode.OperatorNode {
141 public void compile(CompileContext context) {
142 context.getCommandList().add(new GreaterThanCommand());
148 public String toString() {
149 return this.getClass().getName();
156 * RelationalExpressionNodeの"<"演算子を表すノードです。
159 * @author uguu@users.sourceforge.jp
161 public static final class LessThanNode extends RelationalExpressionNode.OperatorNode {
166 public void compile(CompileContext context) {
167 context.getCommandList().add(new LessThanCommand());
173 public String toString() {
174 return this.getClass().getName();
181 * RelationalExpressionNodeの">="演算子を表すノードです。
184 * @author uguu@users.sourceforge.jp
186 public static final class GreaterThanEqualNode extends RelationalExpressionNode.OperatorNode {
191 public void compile(CompileContext context) {
192 context.getCommandList().add(new GreaterThanEqualCommand());
198 public String toString() {
199 return this.getClass().getName();
206 * RelationalExpressionNodeの"<="演算子を表すノードです。
209 * @author uguu@users.sourceforge.jp
211 public static final class LessThanEqualNode extends RelationalExpressionNode.OperatorNode {
216 public void compile(CompileContext context) {
217 context.getCommandList().add(new LessThanEqualCommand());
223 public String toString() {
224 return this.getClass().getName();