ListNode.java

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

  31. import java.util.ArrayList;
  32. import java.util.List;
  33. import org.jruby.ast.visitor.NodeVisitor;
  34. import org.jruby.lexer.yacc.ISourcePosition;

  35. /**
  36.  * All Nodes which have a list representation inherit this.  This is also used
  37.  * as generic container for additional information that is not directly evaluated.
  38.  * In particular, f_arg production rule uses this to capture arg information for
  39.  * the editor projects who want position info saved.
  40.  */
  41. public class ListNode extends Node {
  42.     private List<Node> list;

  43.     /**
  44.      * Create a new ListNode.
  45.      *
  46.      * @param position where list is
  47.      * @param firstNode first element of the list
  48.      */
  49.     public ListNode(ISourcePosition position, Node firstNode) {
  50.         this(position);
  51.        
  52.         list = new ArrayList<Node>(4);
  53.         list.add(firstNode);
  54.     }
  55.    
  56.     public ListNode(ISourcePosition position) {
  57.         super(position);
  58.        
  59.         list = new ArrayList<Node>(0);
  60.     }

  61.     public NodeType getNodeType() {
  62.         return NodeType.LISTNODE;
  63.     }
  64.    
  65.     public ListNode add(Node node) {
  66.         // Ruby Grammar productions return plenty of nulls.
  67.         if (node == null || node == NilImplicitNode.NIL) {
  68.             list.add(NilImplicitNode.NIL);

  69.             return this;
  70.         }

  71.         list.add(node);

  72.         if (getPosition() == null) setPosition(node.getPosition());

  73.         return this;
  74.     }
  75.    
  76.     public ListNode prepend(Node node) {
  77.         // Ruby Grammar productions return plenty of nulls.
  78.         if (node == null) return this;
  79.        
  80.         list.add(0, node);
  81.        
  82.         setPosition(node.getPosition());
  83.         return this;
  84.     }
  85.    
  86.     public int size() {
  87.         return list.size();
  88.     }
  89.    
  90.    
  91.     /**
  92.      * Add all elements in other list to this list node.
  93.      *
  94.      * @param other list which has elements
  95.      * @return this instance for method chaining
  96.      */
  97.     public ListNode addAll(ListNode other) {
  98.         if (other != null && other.size() > 0) {
  99.             list.addAll(other.list);

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

  118.     public boolean isEmpty() {
  119.         return list.isEmpty();
  120.     }
  121.    
  122.     public List<Node> childNodes() {
  123.         return list;
  124.     }
  125.    
  126.     public <T> T accept(NodeVisitor<T> visitor) {
  127.         return visitor.visitListNode(this);
  128.     }
  129.    
  130.     public Node get(int idx) {
  131.         return list.get(idx);
  132.     }
  133. }