Path: blob/master/src/java.base/share/classes/sun/util/locale/StringTokenIterator.java
41159 views
/*1* Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24/*25*******************************************************************************26* Copyright (C) 2009, International Business Machines Corporation and *27* others. All Rights Reserved. *28*******************************************************************************29*/30package sun.util.locale;3132public class StringTokenIterator {33private String text;34private String dlms; // null if a single char delimiter35private char delimiterChar; // delimiter if a single char delimiter3637private String token;38private int start;39private int end;40private boolean done;4142public StringTokenIterator(String text, String dlms) {43this.text = text;44if (dlms.length() == 1) {45delimiterChar = dlms.charAt(0);46} else {47this.dlms = dlms;48}49setStart(0);50}5152public String first() {53setStart(0);54return token;55}5657public String current() {58return token;59}6061public int currentStart() {62return start;63}6465public int currentEnd() {66return end;67}6869public boolean isDone() {70return done;71}7273public String next() {74if (hasNext()) {75start = end + 1;76end = nextDelimiter(start);77token = text.substring(start, end);78} else {79start = end;80token = null;81done = true;82}83return token;84}8586public boolean hasNext() {87return (end < text.length());88}8990public StringTokenIterator setStart(int offset) {91if (offset > text.length()) {92throw new IndexOutOfBoundsException();93}94start = offset;95end = nextDelimiter(start);96token = text.substring(start, end);97done = false;98return this;99}100101public StringTokenIterator setText(String text) {102this.text = text;103setStart(0);104return this;105}106107private int nextDelimiter(int start) {108int textlen = this.text.length();109if (dlms == null) {110for (int idx = start; idx < textlen; idx++) {111if (text.charAt(idx) == delimiterChar) {112return idx;113}114}115} else {116int dlmslen = dlms.length();117for (int idx = start; idx < textlen; idx++) {118char c = text.charAt(idx);119for (int i = 0; i < dlmslen; i++) {120if (c == dlms.charAt(i)) {121return idx;122}123}124}125}126return textlen;127}128}129130131