/Users/lyon/j4p/src/javassist/compiler/ast/ASTList.java

1    /* 
2     * Javassist, a Java-bytecode translator toolkit. 
3     * Copyright (C) 1999-2003 Shigeru Chiba. All Rights Reserved. 
4     * 
5     * The contents of this file are subject to the Mozilla Public License Version 
6     * 1.1 (the "License"); you may not use this file except in compliance with 
7     * the License.  Alternatively, the contents of this file may be used under 
8     * the terms of the GNU Lesser General Public License Version 2.1 or later. 
9     * 
10    * Software distributed under the License is distributed on an "AS IS" basis, 
11    * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
12    * for the specific language governing rights and limitations under the 
13    * License. 
14    */ 
15    
16   package javassist.compiler.ast; 
17    
18   import javassist.compiler.CompileError; 
19    
20   /** 
21    * A linked list. 
22    * The right subtree must be an ASTList object or null. 
23    */ 
24   public class ASTList extends ASTree { 
25       private ASTree left; 
26       private ASTList right; 
27    
28       public ASTList(ASTree _head, ASTList _tail) { 
29           left = _head; 
30           right = _tail; 
31       } 
32    
33       public ASTList(ASTree _head) { 
34           left = _head; 
35           right = null; 
36       } 
37    
38       public static ASTList make(ASTree e1, ASTree e2, ASTree e3) { 
39           return new ASTList(e1, new ASTList(e2, new ASTList(e3))); 
40       } 
41    
42       public ASTree getLeft() { 
43           return left; 
44       } 
45    
46       public ASTree getRight() { 
47           return right; 
48       } 
49    
50       public void setLeft(ASTree _left) { 
51           left = _left; 
52       } 
53    
54       public void setRight(ASTree _right) { 
55           right = (ASTList) _right; 
56       } 
57    
58       /** 
59        * Returns the car part of the list. 
60        */ 
61       public ASTree head() { 
62           return left; 
63       } 
64    
65       public void setHead(ASTree _head) { 
66           left = _head; 
67       } 
68    
69       /** 
70        * Returns the cdr part of the list. 
71        */ 
72       public ASTList tail() { 
73           return right; 
74       } 
75    
76       public void setTail(ASTList _tail) { 
77           right = _tail; 
78       } 
79    
80       public void accept(Visitor v) throws CompileError { 
81           v.atASTList(this); 
82       } 
83    
84       public String toString() { 
85           StringBuffer sbuf = new StringBuffer(); 
86           sbuf.append("(<"); 
87           sbuf.append(getTag()); 
88           sbuf.append('>'); 
89           ASTList list = this; 
90           while (list != null) { 
91               sbuf.append(' '); 
92               ASTree a = list.left; 
93               sbuf.append(a == null ? "<null>" : a.toString()); 
94               list = list.right; 
95           } 
96    
97           sbuf.append(')'); 
98           return sbuf.toString(); 
99       } 
100   
101      /** 
102       * Returns the number of the elements in this list. 
103       */ 
104      public int length() { 
105          return length(this); 
106      } 
107   
108      public static int length(ASTList list) { 
109          if (list == null) 
110              return 0; 
111   
112          int n = 0; 
113          while (list != null) { 
114              list = list.right; 
115              ++n; 
116          } 
117   
118          return n; 
119      } 
120   
121      /** 
122       * Returns a sub list of the list.  The sub list begins with the 
123       * n-th element of the list. 
124       * 
125       * @param nth       zero or more than zero. 
126       */ 
127      public ASTList sublist(int nth) { 
128          ASTList list = this; 
129          while (nth-- > 0) 
130              list = list.right; 
131   
132          return list; 
133      } 
134   
135      /** 
136       * Substitutes <code>newObj</code> for <code>oldObj</code> in the 
137       * list. 
138       */ 
139      public boolean subst(ASTree newObj, ASTree oldObj) { 
140          for (ASTList list = this; list != null; list = list.right) 
141              if (list.left == oldObj) { 
142                  list.left = newObj; 
143                  return true; 
144              } 
145   
146          return false; 
147      } 
148   
149      /** 
150       * Appends an object to a list. 
151       */ 
152      public static ASTList append(ASTList a, ASTree b) { 
153          return concat(a, new ASTList(b)); 
154      } 
155   
156      /** 
157       * Concatenates two lists. 
158       */ 
159      public static ASTList concat(ASTList a, ASTList b) { 
160          if (a == null) 
161              return b; 
162          else { 
163              ASTList list = a; 
164              while (list.right != null) 
165                  list = list.right; 
166   
167              list.right = b; 
168              return a; 
169          } 
170      } 
171  } 
172