Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/text/StringCharacterIterator.java
38829 views
/*1* Copyright (c) 1996, 2013, 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*/2425/*26* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved27* (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved28*29* The original version of this source code and documentation30* is copyrighted and owned by Taligent, Inc., a wholly-owned31* subsidiary of IBM. These materials are provided under terms32* of a License Agreement between Taligent and Sun. This technology33* is protected by multiple US and International patents.34*35* This notice and attribution to Taligent may not be removed.36* Taligent is a registered trademark of Taligent, Inc.37*38*/3940package java.text;4142/**43* <code>StringCharacterIterator</code> implements the44* <code>CharacterIterator</code> protocol for a <code>String</code>.45* The <code>StringCharacterIterator</code> class iterates over the46* entire <code>String</code>.47*48* @see CharacterIterator49*/5051public final class StringCharacterIterator implements CharacterIterator52{53private String text;54private int begin;55private int end;56// invariant: begin <= pos <= end57private int pos;5859/**60* Constructs an iterator with an initial index of 0.61*62* @param text the {@code String} to be iterated over63*/64public StringCharacterIterator(String text)65{66this(text, 0);67}6869/**70* Constructs an iterator with the specified initial index.71*72* @param text The String to be iterated over73* @param pos Initial iterator position74*/75public StringCharacterIterator(String text, int pos)76{77this(text, 0, text.length(), pos);78}7980/**81* Constructs an iterator over the given range of the given string, with the82* index set at the specified position.83*84* @param text The String to be iterated over85* @param begin Index of the first character86* @param end Index of the character following the last character87* @param pos Initial iterator position88*/89public StringCharacterIterator(String text, int begin, int end, int pos) {90if (text == null)91throw new NullPointerException();92this.text = text;9394if (begin < 0 || begin > end || end > text.length())95throw new IllegalArgumentException("Invalid substring range");9697if (pos < begin || pos > end)98throw new IllegalArgumentException("Invalid position");99100this.begin = begin;101this.end = end;102this.pos = pos;103}104105/**106* Reset this iterator to point to a new string. This package-visible107* method is used by other java.text classes that want to avoid allocating108* new StringCharacterIterator objects every time their setText method109* is called.110*111* @param text The String to be iterated over112* @since 1.2113*/114public void setText(String text) {115if (text == null)116throw new NullPointerException();117this.text = text;118this.begin = 0;119this.end = text.length();120this.pos = 0;121}122123/**124* Implements CharacterIterator.first() for String.125* @see CharacterIterator#first126*/127public char first()128{129pos = begin;130return current();131}132133/**134* Implements CharacterIterator.last() for String.135* @see CharacterIterator#last136*/137public char last()138{139if (end != begin) {140pos = end - 1;141} else {142pos = end;143}144return current();145}146147/**148* Implements CharacterIterator.setIndex() for String.149* @see CharacterIterator#setIndex150*/151public char setIndex(int p)152{153if (p < begin || p > end)154throw new IllegalArgumentException("Invalid index");155pos = p;156return current();157}158159/**160* Implements CharacterIterator.current() for String.161* @see CharacterIterator#current162*/163public char current()164{165if (pos >= begin && pos < end) {166return text.charAt(pos);167}168else {169return DONE;170}171}172173/**174* Implements CharacterIterator.next() for String.175* @see CharacterIterator#next176*/177public char next()178{179if (pos < end - 1) {180pos++;181return text.charAt(pos);182}183else {184pos = end;185return DONE;186}187}188189/**190* Implements CharacterIterator.previous() for String.191* @see CharacterIterator#previous192*/193public char previous()194{195if (pos > begin) {196pos--;197return text.charAt(pos);198}199else {200return DONE;201}202}203204/**205* Implements CharacterIterator.getBeginIndex() for String.206* @see CharacterIterator#getBeginIndex207*/208public int getBeginIndex()209{210return begin;211}212213/**214* Implements CharacterIterator.getEndIndex() for String.215* @see CharacterIterator#getEndIndex216*/217public int getEndIndex()218{219return end;220}221222/**223* Implements CharacterIterator.getIndex() for String.224* @see CharacterIterator#getIndex225*/226public int getIndex()227{228return pos;229}230231/**232* Compares the equality of two StringCharacterIterator objects.233* @param obj the StringCharacterIterator object to be compared with.234* @return true if the given obj is the same as this235* StringCharacterIterator object; false otherwise.236*/237public boolean equals(Object obj)238{239if (this == obj)240return true;241if (!(obj instanceof StringCharacterIterator))242return false;243244StringCharacterIterator that = (StringCharacterIterator) obj;245246if (hashCode() != that.hashCode())247return false;248if (!text.equals(that.text))249return false;250if (pos != that.pos || begin != that.begin || end != that.end)251return false;252return true;253}254255/**256* Computes a hashcode for this iterator.257* @return A hash code258*/259public int hashCode()260{261return text.hashCode() ^ pos ^ begin ^ end;262}263264/**265* Creates a copy of this iterator.266* @return A copy of this267*/268public Object clone()269{270try {271StringCharacterIterator other272= (StringCharacterIterator) super.clone();273return other;274}275catch (CloneNotSupportedException e) {276throw new InternalError(e);277}278}279280}281282283