KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > languages > ASTPath


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.languages;
21
22 import java.util.Collections JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.ListIterator JavaDoc;
26
27
28 /**
29  * Represents path in AST tree.
30  *
31  * @author Jan Jancura
32  */

33 public abstract class ASTPath {
34
35     ASTPath () {};
36     
37     /**
38      * Returns last ASTItem in this path.
39      *
40      * @return last ASTItem in this path
41      */

42     public abstract ASTItem getLeaf ();
43     
44     /**
45      * Returns size of this path.
46      *
47      * @return size of this path
48      */

49     public abstract int size ();
50     
51     /**
52      * Returns first ASTItem in this path.
53      *
54      * @return first ASTItem in this path
55      */

56     public abstract ASTItem getRoot ();
57     
58     /**
59      * Returns iterator for this path.
60      *
61      * @return iterator for this path
62      */

63     public abstract ListIterator JavaDoc<ASTItem> listIterator ();
64     
65     /**
66      * Returns iterator for this path.
67      *
68      * @return iterator for this path
69      */

70     public abstract ListIterator JavaDoc<ASTItem> listIterator (int index);
71     
72     /**
73      * Returns ASTItem on given index.
74      *
75      * @return ASTItem on given index
76      */

77     public abstract ASTItem get (int index);
78     
79     /**
80      * Returns subpath of this path from given index.
81      *
82      * @return subpath of this path from given index
83      */

84     public abstract ASTPath subPath (int index);
85
86     /**
87      * Returns new path from {@link javax.util.List}, or null if the path is empty.
88      *
89      * @param path list of ASTItems or null, if the path is empty
90      * @return new ASTPath
91      */

92     public static ASTPath create (List JavaDoc<ASTItem> path) {
93         if (path.isEmpty ()) return null;
94         return new Token2Path (path);
95     }
96
97
98     /**
99      * Creates new singleton path.
100      *
101      * @param item
102      * @return new ASTPath
103      */

104     public static ASTPath create (ASTItem item) {
105         if (item == null) throw new NullPointerException JavaDoc ();
106         return new TokenPath (item);
107     }
108     
109     
110     // innerclasses ............................................................
111

112     private static final class TokenPath extends ASTPath {
113
114         private ASTItem o;
115         
116         TokenPath (ASTItem o) {
117             this.o = o;
118         }
119         
120         public ASTItem getLeaf () {
121             return o;
122         }
123         
124         public int size () {
125             return 1;
126         }
127         
128         public ASTItem getRoot () {
129             return o;
130         }
131         
132         public ListIterator JavaDoc<ASTItem> listIterator () {
133             return Collections.singletonList (o).listIterator ();
134         }
135         
136         public ListIterator JavaDoc<ASTItem> listIterator (int index) {
137             return Collections.singletonList (o).listIterator (index);
138         }
139         
140         public ASTItem get (int index) {
141             if (index == 0) return o;
142             throw new ArrayIndexOutOfBoundsException JavaDoc ();
143         }
144         
145         public ASTPath subPath (int index) {
146             if (index == 0) return this;
147             throw new ArrayIndexOutOfBoundsException JavaDoc ();
148         }
149         
150         public String JavaDoc toString () {
151             return "ASTPath " + o;
152         }
153     }
154
155     private static final class Token2Path extends ASTPath {
156
157         private List JavaDoc<ASTItem> path;
158         private int s;
159         
160         Token2Path (List JavaDoc<ASTItem> path) {
161             this.path = path;
162             s = path.size ();
163             if (s < 1)
164                 throw new IllegalArgumentException JavaDoc ();
165         }
166         
167         public ASTItem getLeaf () {
168             return path.get (s - 1);
169         }
170         
171         public int size () {
172             return s;
173         }
174         
175         public ASTItem getRoot () {
176             return path.get (0);
177         }
178         
179         public ListIterator JavaDoc<ASTItem> listIterator () {
180             return path.listIterator ();
181         }
182         
183         public ListIterator JavaDoc<ASTItem> listIterator (int index) {
184             return path.listIterator (index);
185         }
186         
187         public ASTItem get (int index) {
188             return path.get (index);
189         }
190         
191         public ASTPath subPath (int index) {
192             return new Token2Path (path.subList (0, index + 1));
193         }
194         
195         public String JavaDoc toString () {
196             StringBuilder JavaDoc sb = new StringBuilder JavaDoc ("ASTPath ");
197             Iterator JavaDoc<ASTItem> it = path.iterator ();
198             if (it.hasNext ()) {
199                 ASTItem item = it.next ();
200                 if (item instanceof ASTNode)
201                     sb.append (((ASTNode) item).getNT ());
202                 else
203                     sb.append (item);
204             }
205             while (it.hasNext ()) {
206                 ASTItem item = it.next ();
207                 if (item instanceof ASTNode)
208                     sb.append (", ").append (((ASTNode) item).getNT ());
209                 else
210                     sb.append (", ").append (item);
211             }
212             return sb.toString ();
213         }
214     }
215 }
216
Popular Tags