1 19 package org.openide.filesystems; 20 21 import java.util.ArrayList ; 22 import java.util.Enumeration ; 23 import java.util.List ; 24 import java.util.NoSuchElementException ; 25 import java.util.StringTokenizer ; 26 27 28 31 final class PathElements { 32 private static final String DELIMITER = "/"; 34 35 private String name; 36 37 38 private StringTokenizer tokenizer; 39 40 41 private List <String > tokens; 42 43 44 public PathElements(String name) { 45 this.name = name; 46 tokenizer = new StringTokenizer (name, DELIMITER); 47 tokens = new ArrayList <String >(10); 48 } 49 50 53 public String getOriginalName() { 54 return name; 55 } 56 57 public Enumeration <String > getEnumeration() { 58 return new EnumerationImpl(this); 59 } 60 61 boolean contains(int i) { 62 if (tokens.size() <= i) { 63 scanUpTo(i); 64 } 65 66 return (tokens.size() > i); 67 } 68 69 String get(int i) throws NoSuchElementException { 70 if (tokens.size() <= i) { 71 scanUpTo(i); 72 } 73 74 if (tokens.size() <= i) { 75 throw new NoSuchElementException (); 76 } 77 78 return tokens.get(i); 79 } 80 81 private synchronized void scanUpTo(int i) { 82 if (tokenizer == null) { 83 return; 84 } 85 86 if (tokens.size() > i) { 87 return; 88 } 89 90 for (int k = tokens.size() - 1; (k < i) && tokenizer.hasMoreTokens(); k++) { 91 tokens.add(tokenizer.nextToken()); 92 } 93 94 if (!tokenizer.hasMoreTokens()) { 95 tokenizer = null; 96 } 97 } 98 99 100 static final class EnumerationImpl implements Enumeration <String > { 101 private PathElements elements; 102 private int pos; 103 104 EnumerationImpl(PathElements elements) { 105 this.elements = elements; 106 this.pos = 0; 107 } 108 109 110 public boolean hasMoreElements() { 111 return elements.contains(pos); 112 } 113 114 115 public String nextElement() throws NoSuchElementException { 116 return elements.get(pos++); 117 } 118 } 119 } 120 | Popular Tags |