View Javadoc

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.rpn_computer.node;
33  
34  import java.util.ArrayList;
35  import java.util.Iterator;
36  import java.util.List;
37  
38  import jp.sourceforge.rpn_computer.RpnNode;
39  
40  /**
41   * <p>
42   * ノードの基底抽象クラスです。
43   * </p>
44   * 
45   * @author uguu@users.sourceforge.jp
46   */
47  public abstract class AbstractNode implements RpnNode {
48  
49      private List nodeList = new ArrayList();
50  
51      /**
52       * <p>
53       * 自分の子ノードを追加します。
54       * </p>
55       * 
56       * @param node
57       *            子ノード。nullの場合、{@link NullPointerException}例外をスローします。
58       */
59      protected final void addNode(RpnNode node) {
60          if (node == null) {
61              throw new NullPointerException("nodeがnullです。");
62          }
63          this.nodeList.add(node);
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      public final RpnNode[] getChildren() {
70          return (RpnNode[]) this.nodeList.toArray(new RpnNode[0]);
71      }
72  
73      /**
74       * <p>
75       * 子ノードの文字列表現を返します。
76       * </p>
77       * 
78       * @return 子ノードの文字列表現。
79       */
80      protected final String toChildrenString() {
81          String str = "";
82          for (Iterator i = this.nodeList.iterator(); i.hasNext();) {
83              RpnNode node = (RpnNode) i.next();
84              String[] nodeString = node.toString().split("\n");
85              for (int j = 0; j < nodeString.length; j++) {
86                  str += "\t" + nodeString[j] + "\n";
87              }
88          }
89          return str;
90      }
91  }