1 /*
2  * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
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 {
34     private String text;
35     private String dlms;        // null if a single char delimiter
36     private char delimiterChar; // delimiter if a single char delimiter
37 
38     private String token;
39     private int start;
40     private int end;
41     private boolean done;
42 
StringTokenIterator(String text, String dlms)43     public StringTokenIterator(String text, String dlms) {
44         this.text = text;
45         if (dlms.length() == 1) {
46             delimiterChar = dlms.charAt(0);
47         } else {
48             this.dlms = dlms;
49         }
50         setStart(0);
51     }
52 
first()53     public String first() {
54         setStart(0);
55         return token;
56     }
57 
current()58     public String current() {
59         return token;
60     }
61 
currentStart()62     public int currentStart() {
63         return start;
64     }
65 
currentEnd()66     public int currentEnd() {
67         return end;
68     }
69 
isDone()70     public boolean isDone() {
71         return done;
72     }
73 
next()74     public String next() {
75         if (hasNext()) {
76             start = end + 1;
77             end = nextDelimiter(start);
78             token = text.substring(start, end);
79         } else {
80             start = end;
81             token = null;
82             done = true;
83         }
84         return token;
85     }
86 
hasNext()87     public boolean hasNext() {
88         return (end < text.length());
89     }
90 
setStart(int offset)91     public StringTokenIterator setStart(int offset) {
92         if (offset > text.length()) {
93             throw new IndexOutOfBoundsException();
94         }
95         start = offset;
96         end = nextDelimiter(start);
97         token = text.substring(start, end);
98         done = false;
99         return this;
100     }
101 
setText(String text)102     public StringTokenIterator setText(String text) {
103         this.text = text;
104         setStart(0);
105         return this;
106     }
107 
nextDelimiter(int start)108     private int nextDelimiter(int start) {
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;
114                 }
115             }
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             }
126         }
127         return textlen;
128     }
129 }
130