6501
|
1 |
/*
|
9224
|
2 |
* Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
|
6501
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
/*
|
|
26 |
*******************************************************************************
|
|
27 |
* Copyright (C) 2009, International Business Machines Corporation and *
|
|
28 |
* others. All Rights Reserved. *
|
|
29 |
*******************************************************************************
|
|
30 |
*/
|
|
31 |
package sun.util.locale;
|
|
32 |
|
|
33 |
public class StringTokenIterator {
|
9224
|
34 |
private String text;
|
|
35 |
private String dlms; // null if a single char delimiter
|
|
36 |
private char delimiterChar; // delimiter if a single char delimiter
|
6501
|
37 |
|
9224
|
38 |
private String token;
|
|
39 |
private int start;
|
|
40 |
private int end;
|
|
41 |
private boolean done;
|
6501
|
42 |
|
|
43 |
public StringTokenIterator(String text, String dlms) {
|
9224
|
44 |
this.text = text;
|
|
45 |
if (dlms.length() == 1) {
|
|
46 |
delimiterChar = dlms.charAt(0);
|
|
47 |
} else {
|
|
48 |
this.dlms = dlms;
|
|
49 |
}
|
6501
|
50 |
setStart(0);
|
|
51 |
}
|
|
52 |
|
|
53 |
public String first() {
|
|
54 |
setStart(0);
|
9224
|
55 |
return token;
|
6501
|
56 |
}
|
|
57 |
|
|
58 |
public String current() {
|
9224
|
59 |
return token;
|
6501
|
60 |
}
|
|
61 |
|
|
62 |
public int currentStart() {
|
9224
|
63 |
return start;
|
6501
|
64 |
}
|
|
65 |
|
|
66 |
public int currentEnd() {
|
9224
|
67 |
return end;
|
6501
|
68 |
}
|
|
69 |
|
|
70 |
public boolean isDone() {
|
9224
|
71 |
return done;
|
6501
|
72 |
}
|
|
73 |
|
|
74 |
public String next() {
|
|
75 |
if (hasNext()) {
|
9224
|
76 |
start = end + 1;
|
|
77 |
end = nextDelimiter(start);
|
|
78 |
token = text.substring(start, end);
|
6501
|
79 |
} else {
|
9224
|
80 |
start = end;
|
|
81 |
token = null;
|
|
82 |
done = true;
|
6501
|
83 |
}
|
9224
|
84 |
return token;
|
6501
|
85 |
}
|
|
86 |
|
|
87 |
public boolean hasNext() {
|
9224
|
88 |
return (end < text.length());
|
6501
|
89 |
}
|
|
90 |
|
|
91 |
public StringTokenIterator setStart(int offset) {
|
9224
|
92 |
if (offset > text.length()) {
|
6501
|
93 |
throw new IndexOutOfBoundsException();
|
|
94 |
}
|
9224
|
95 |
start = offset;
|
|
96 |
end = nextDelimiter(start);
|
|
97 |
token = text.substring(start, end);
|
|
98 |
done = false;
|
6501
|
99 |
return this;
|
|
100 |
}
|
|
101 |
|
|
102 |
public StringTokenIterator setText(String text) {
|
9224
|
103 |
this.text = text;
|
6501
|
104 |
setStart(0);
|
|
105 |
return this;
|
|
106 |
}
|
|
107 |
|
|
108 |
private int nextDelimiter(int start) {
|
9224
|
109 |
int textlen = this.text.length();
|
|
110 |
if (dlms == null) {
|
|
111 |
for (int idx = start; idx < textlen; idx++) {
|
|
112 |
if (text.charAt(idx) == delimiterChar) {
|
|
113 |
return idx;
|
6501
|
114 |
}
|
|
115 |
}
|
9224
|
116 |
} else {
|
|
117 |
int dlmslen = dlms.length();
|
|
118 |
for (int idx = start; idx < textlen; idx++) {
|
|
119 |
char c = text.charAt(idx);
|
|
120 |
for (int i = 0; i < dlmslen; i++) {
|
|
121 |
if (c == dlms.charAt(i)) {
|
|
122 |
return idx;
|
|
123 |
}
|
|
124 |
}
|
|
125 |
}
|
6501
|
126 |
}
|
9224
|
127 |
return textlen;
|
6501
|
128 |
}
|
|
129 |
}
|