1 19 20 package org.netbeans.api.languages; 21 22 import java.util.Collections ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.ListIterator ; 26 27 28 33 public abstract class ASTPath { 34 35 ASTPath () {}; 36 37 42 public abstract ASTItem getLeaf (); 43 44 49 public abstract int size (); 50 51 56 public abstract ASTItem getRoot (); 57 58 63 public abstract ListIterator <ASTItem> listIterator (); 64 65 70 public abstract ListIterator <ASTItem> listIterator (int index); 71 72 77 public abstract ASTItem get (int index); 78 79 84 public abstract ASTPath subPath (int index); 85 86 92 public static ASTPath create (List <ASTItem> path) { 93 if (path.isEmpty ()) return null; 94 return new Token2Path (path); 95 } 96 97 98 104 public static ASTPath create (ASTItem item) { 105 if (item == null) throw new NullPointerException (); 106 return new TokenPath (item); 107 } 108 109 110 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 <ASTItem> listIterator () { 133 return Collections.singletonList (o).listIterator (); 134 } 135 136 public ListIterator <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 (); 143 } 144 145 public ASTPath subPath (int index) { 146 if (index == 0) return this; 147 throw new ArrayIndexOutOfBoundsException (); 148 } 149 150 public String toString () { 151 return "ASTPath " + o; 152 } 153 } 154 155 private static final class Token2Path extends ASTPath { 156 157 private List <ASTItem> path; 158 private int s; 159 160 Token2Path (List <ASTItem> path) { 161 this.path = path; 162 s = path.size (); 163 if (s < 1) 164 throw new IllegalArgumentException (); 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 <ASTItem> listIterator () { 180 return path.listIterator (); 181 } 182 183 public ListIterator <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 toString () { 196 StringBuilder sb = new StringBuilder ("ASTPath "); 197 Iterator <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 |