KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > filesystems > PathElements


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 package org.openide.filesystems;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.NoSuchElementException JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26
27
28 /**
29  * @author Ales Novak
30  */

31 final class PathElements {
32     private static final String JavaDoc DELIMITER = "/"; // NOI18N
33

34     /** Original name */
35     private String JavaDoc name;
36
37     /** tokenizer */
38     private StringTokenizer JavaDoc tokenizer;
39
40     /** tokens */
41     private List JavaDoc<String JavaDoc> tokens;
42
43     /** Creates new PathElements */
44     public PathElements(String JavaDoc name) {
45         this.name = name;
46         tokenizer = new StringTokenizer JavaDoc(name, DELIMITER);
47         tokens = new ArrayList JavaDoc<String JavaDoc>(10);
48     }
49
50     /**
51      * @return original name
52      */

53     public String JavaDoc getOriginalName() {
54         return name;
55     }
56
57     public Enumeration JavaDoc<String JavaDoc> 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 JavaDoc get(int i) throws NoSuchElementException JavaDoc {
70         if (tokens.size() <= i) {
71             scanUpTo(i);
72         }
73
74         if (tokens.size() <= i) {
75             throw new NoSuchElementException JavaDoc();
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     /** Impl of enumeration */
100     static final class EnumerationImpl implements Enumeration JavaDoc<String JavaDoc> {
101         private PathElements elements;
102         private int pos;
103
104         EnumerationImpl(PathElements elements) {
105             this.elements = elements;
106             this.pos = 0;
107         }
108
109         /** From Enumeration */
110         public boolean hasMoreElements() {
111             return elements.contains(pos);
112         }
113
114         /** From Enumeration */
115         public String JavaDoc nextElement() throws NoSuchElementException JavaDoc {
116             return elements.get(pos++);
117         }
118     }
119 }
120
Popular Tags