1 29 package org.jruby.ast; 30 31 import java.util.ArrayList ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.ListIterator ; 35 36 import org.jruby.ast.visitor.NodeVisitor; 37 import org.jruby.evaluator.Instruction; 38 import org.jruby.lexer.yacc.ISourcePosition; 39 40 46 public class ListNode extends Node { 47 private static final long serialVersionUID = 1L; 48 49 private List list = null; 50 51 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 if (node == null) return this; 74 if (list == null) list = new ArrayList (); 75 76 list.add(node); 77 setPosition(getPosition().union(node.getPosition())); 78 return this; 79 } 80 81 public Iterator iterator() { 82 return list == null ? EMPTY_LIST.iterator() : list.iterator(); 83 } 84 85 public ListIterator 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 100 public ListNode addAll(ListNode other) { 101 if (other != null) { 102 if (list == null) list = new ArrayList (); 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 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 toString() { 127 String string = super.toString(); 128 if (list == null) { 129 return string + ": {}"; 130 } 131 StringBuffer b = new StringBuffer (); 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 childNodes() { 142 return list == null ? EMPTY_LIST : list; 143 } 144 145 public Instruction accept(NodeVisitor visitor) { 146 throw new RuntimeException ("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 |