KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > ast > ListNode


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2004-2005 Thomas E Enebo <enebo@acm.org>
15  * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either of the GNU General Public License Version 2 or later (the "GPL"),
19  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the CPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the CPL, the GPL or the LGPL.
28  ***** END LICENSE BLOCK *****/

29 package org.jruby.ast;
30
31 import java.util.ArrayList JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.ListIterator JavaDoc;
35
36 import org.jruby.ast.visitor.NodeVisitor;
37 import org.jruby.evaluator.Instruction;
38 import org.jruby.lexer.yacc.ISourcePosition;
39
40 /**
41  * All Nodes which have a list representation inherit this. This is also used
42  * as generic container for additional information that is not directly evaluated.
43  * In particular, f_arg production rule uses this to capture arg information for
44  * the editor projects who want position info saved.
45  */

46 public class ListNode extends Node {
47     private static final long serialVersionUID = 1L;
48     
49     private List JavaDoc list = null;
50
51     /**
52      * Create a new ListNode.
53      *
54      * @param id type of listnode
55      * @param firstNode first element of the list
56      */

57     public ListNode(ISourcePosition position, int id, Node firstNode) {
58         this(position, id);
59         
60         add(firstNode);
61     }
62     
63     public ListNode(ISourcePosition position, int id) {
64         super(position, id);
65     }
66
67     public ListNode(ISourcePosition position) {
68         super(position, NodeTypes.LISTNODE);
69     }
70     
71     public ListNode add(Node node) {
72         // Ruby Grammar productions return plenty of nulls.
73
if (node == null) return this;
74         if (list == null) list = new ArrayList JavaDoc();
75
76         list.add(node);
77         setPosition(getPosition().union(node.getPosition()));
78         return this;
79     }
80
81     public Iterator JavaDoc iterator() {
82         return list == null ? EMPTY_LIST.iterator() : list.iterator();
83     }
84     
85     public ListIterator JavaDoc reverseIterator() {
86         return list == null ? EMPTY_LIST.listIterator() : list.listIterator(list.size());
87     }
88     
89     public int size() {
90         return list == null ? 0 : list.size();
91     }
92     
93     
94     /**
95      * Add all elements in other list to this list node.
96      *
97      * @param other list which has elements
98      * @return this instance for method chaining
99      */

100     public ListNode addAll(ListNode other) {
101         if (other != null) {
102             if (list == null) list = new ArrayList JavaDoc();
103             list.addAll(other.list);
104             
105             if (list.size() > 0) {
106                 setPosition(getPosition().union(getLast().getPosition()));
107             }
108         }
109         return this;
110     }
111     
112     /**
113      * Add other element to this list
114      *
115      * @param other list which has elements
116      * @return this instance for method chaining
117      */

118     public ListNode addAll(Node other) {
119         return add(other);
120     }
121     
122     public Node getLast() {
123         return list == null ? null : (Node) list.get(list.size() - 1);
124     }
125     
126     public String JavaDoc toString() {
127         String JavaDoc string = super.toString();
128         if (list == null) {
129             return string + ": {}";
130         }
131         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
132         for (int i = 0; i < list.size(); i++) {
133             b.append(list.get(i));
134             if (i + 1 < list.size()) {
135                 b.append(", ");
136             }
137         }
138         return string + ": {" + b.toString() + "}";
139     }
140     
141     public List JavaDoc childNodes() {
142         return list == null ? EMPTY_LIST : list;
143     }
144     
145     public Instruction accept(NodeVisitor visitor) {
146         throw new RuntimeException JavaDoc("Base class ListNode should never be evaluated");
147     }
148     
149     public Node get(int idx) {
150         return (Node)list.get(idx);
151     }
152 }
153
Popular Tags