Path: blob/master/src/java.base/share/classes/java/text/StringCharacterIterator.java
41152 views
/*1* Copyright (c) 1996, 2021, 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} implements the44* {@code CharacterIterator} protocol for a {@code String}.45* The {@code StringCharacterIterator} class iterates over the46* entire {@code String}.47*48* @see CharacterIterator49* @since 1.150*/5152public final class StringCharacterIterator implements CharacterIterator53{54private String text;55private int begin;56private int end;57// invariant: begin <= pos <= end58private int pos;5960/**61* Constructs an iterator with an initial index of 0.62*63* @param text the {@code String} to be iterated over64*/65public StringCharacterIterator(String text)66{67this(text, 0);68}6970/**71* Constructs an iterator with the specified initial index.72*73* @param text The String to be iterated over74* @param pos Initial iterator position75*/76public StringCharacterIterator(String text, int pos)77{78this(text, 0, text.length(), pos);79}8081/**82* Constructs an iterator over the given range of the given string, with the83* index set at the specified position.84*85* @param text The String to be iterated over86* @param begin Index of the first character87* @param end Index of the character following the last character88* @param pos Initial iterator position89*/90public StringCharacterIterator(String text, int begin, int end, int pos) {91if (text == null)92throw new NullPointerException();93this.text = text;9495if (begin < 0 || begin > end || end > text.length())96throw new IllegalArgumentException("Invalid substring range");9798if (pos < begin || pos > end)99throw new IllegalArgumentException("Invalid position");100101this.begin = begin;102this.end = end;103this.pos = pos;104}105106/**107* Reset this iterator to point to a new string. This package-visible108* method is used by other java.text classes that want to avoid allocating109* new StringCharacterIterator objects every time their setText method110* is called.111*112* @param text The String to be iterated over113* @since 1.2114*/115public void setText(String text) {116if (text == null)117throw new NullPointerException();118this.text = text;119this.begin = 0;120this.end = text.length();121this.pos = 0;122}123124/**125* Implements CharacterIterator.first() for String.126* @see CharacterIterator#first127*/128public char first()129{130pos = begin;131return current();132}133134/**135* Implements CharacterIterator.last() for String.136* @see CharacterIterator#last137*/138public char last()139{140if (end != begin) {141pos = end - 1;142} else {143pos = end;144}145return current();146}147148/**149* Implements CharacterIterator.setIndex() for String.150* @see CharacterIterator#setIndex151*/152public char setIndex(int p)153{154if (p < begin || p > end)155throw new IllegalArgumentException("Invalid index");156pos = p;157return current();158}159160/**161* Implements CharacterIterator.current() for String.162* @see CharacterIterator#current163*/164public char current()165{166if (pos >= begin && pos < end) {167return text.charAt(pos);168}169else {170return DONE;171}172}173174/**175* Implements CharacterIterator.next() for String.176* @see CharacterIterator#next177*/178public char next()179{180if (pos < end - 1) {181pos++;182return text.charAt(pos);183}184else {185pos = end;186return DONE;187}188}189190/**191* Implements CharacterIterator.previous() for String.192* @see CharacterIterator#previous193*/194public char previous()195{196if (pos > begin) {197pos--;198return text.charAt(pos);199}200else {201return DONE;202}203}204205/**206* Implements CharacterIterator.getBeginIndex() for String.207* @see CharacterIterator#getBeginIndex208*/209public int getBeginIndex()210{211return begin;212}213214/**215* Implements CharacterIterator.getEndIndex() for String.216* @see CharacterIterator#getEndIndex217*/218public int getEndIndex()219{220return end;221}222223/**224* Implements CharacterIterator.getIndex() for String.225* @see CharacterIterator#getIndex226*/227public int getIndex()228{229return pos;230}231232/**233* Compares the equality of two StringCharacterIterator objects.234* @param obj the StringCharacterIterator object to be compared with.235* @return true if the given obj is the same as this236* StringCharacterIterator object; false otherwise.237*/238public boolean equals(Object obj)239{240if (this == obj)241return true;242if (!(obj instanceof StringCharacterIterator that))243return false;244245if (hashCode() != that.hashCode())246return false;247if (!text.equals(that.text))248return false;249if (pos != that.pos || begin != that.begin || end != that.end)250return false;251return true;252}253254/**255* Computes a hashcode for this iterator.256* @return A hash code257*/258public int hashCode()259{260return text.hashCode() ^ pos ^ begin ^ end;261}262263/**264* Creates a copy of this iterator.265* @return A copy of this266*/267public Object clone()268{269try {270StringCharacterIterator other271= (StringCharacterIterator) super.clone();272return other;273}274catch (CloneNotSupportedException e) {275throw new InternalError(e);276}277}278279}280281282