001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.api.languages;
043:
044: import java.util.Collections;
045: import java.util.Iterator;
046: import java.util.List;
047: import java.util.ListIterator;
048:
049: /**
050: * Represents path in AST tree.
051: *
052: * @author Jan Jancura
053: */
054: public abstract class ASTPath {
055:
056: ASTPath() {
057: };
058:
059: /**
060: * Returns last ASTItem in this path.
061: *
062: * @return last ASTItem in this path
063: */
064: public abstract ASTItem getLeaf();
065:
066: /**
067: * Returns size of this path.
068: *
069: * @return size of this path
070: */
071: public abstract int size();
072:
073: /**
074: * Returns first ASTItem in this path.
075: *
076: * @return first ASTItem in this path
077: */
078: public abstract ASTItem getRoot();
079:
080: /**
081: * Returns iterator for this path.
082: *
083: * @return iterator for this path
084: */
085: public abstract ListIterator<ASTItem> listIterator();
086:
087: /**
088: * Returns iterator for this path.
089: *
090: * @return iterator for this path
091: */
092: public abstract ListIterator<ASTItem> listIterator(int index);
093:
094: /**
095: * Returns ASTItem on given index.
096: *
097: * @return ASTItem on given index
098: */
099: public abstract ASTItem get(int index);
100:
101: /**
102: * Returns subpath of this path from given index.
103: *
104: * @return subpath of this path from given index
105: */
106: public abstract ASTPath subPath(int index);
107:
108: /**
109: * Returns new path from {@link javax.util.List}, or null if the path is empty.
110: *
111: * @param path list of ASTItems or null, if the path is empty
112: * @return new ASTPath
113: */
114: public static ASTPath create(List<ASTItem> path) {
115: if (path.isEmpty())
116: return null;
117: return new Token2Path(path);
118: }
119:
120: /**
121: * Creates new singleton path.
122: *
123: * @param item
124: * @return new ASTPath
125: */
126: public static ASTPath create(ASTItem item) {
127: if (item == null)
128: throw new NullPointerException();
129: return new TokenPath(item);
130: }
131:
132: // innerclasses ............................................................
133:
134: private static final class TokenPath extends ASTPath {
135:
136: private ASTItem o;
137:
138: TokenPath(ASTItem o) {
139: this .o = o;
140: }
141:
142: public ASTItem getLeaf() {
143: return o;
144: }
145:
146: public int size() {
147: return 1;
148: }
149:
150: public ASTItem getRoot() {
151: return o;
152: }
153:
154: public ListIterator<ASTItem> listIterator() {
155: return Collections.singletonList(o).listIterator();
156: }
157:
158: public ListIterator<ASTItem> listIterator(int index) {
159: return Collections.singletonList(o).listIterator(index);
160: }
161:
162: public ASTItem get(int index) {
163: if (index == 0)
164: return o;
165: throw new ArrayIndexOutOfBoundsException();
166: }
167:
168: public ASTPath subPath(int index) {
169: if (index == 0)
170: return this ;
171: throw new ArrayIndexOutOfBoundsException();
172: }
173:
174: public String toString() {
175: return "ASTPath " + o;
176: }
177: }
178:
179: private static final class Token2Path extends ASTPath {
180:
181: private List<ASTItem> path;
182:
183: Token2Path(List<ASTItem> path) {
184: this .path = path;
185: if (path.size() < 1)
186: throw new IllegalArgumentException();
187: }
188:
189: public ASTItem getLeaf() {
190: return path.get(path.size() - 1);
191: }
192:
193: public int size() {
194: return path.size();
195: }
196:
197: public ASTItem getRoot() {
198: return path.get(0);
199: }
200:
201: public ListIterator<ASTItem> listIterator() {
202: return path.listIterator();
203: }
204:
205: public ListIterator<ASTItem> listIterator(int index) {
206: return path.listIterator(index);
207: }
208:
209: public ASTItem get(int index) {
210: return path.get(index);
211: }
212:
213: public ASTPath subPath(int index) {
214: return new Token2Path(path.subList(0, index + 1));
215: }
216:
217: public String toString() {
218: StringBuilder sb = new StringBuilder("ASTPath ");
219: Iterator<ASTItem> it = path.iterator();
220: if (it.hasNext()) {
221: ASTItem item = it.next();
222: if (item instanceof ASTNode)
223: sb.append(((ASTNode) item).getNT());
224: else
225: sb.append(item);
226: }
227: while (it.hasNext()) {
228: ASTItem item = it.next();
229: if (item instanceof ASTNode)
230: sb.append(", ").append(((ASTNode) item).getNT());
231: else
232: sb.append(", ").append(item);
233: }
234: return sb.toString();
235: }
236: }
237: }
|