Path: blob/master/src/java.base/share/classes/java/text/ParsePosition.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 documentation is copyrighted30* and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These31* materials are provided under terms of a License Agreement between Taligent32* and Sun. This technology is protected by multiple US and International33* patents. This notice and attribution to Taligent may not be removed.34* Taligent is a registered trademark of Taligent, Inc.35*36*/3738package java.text;394041/**42* {@code ParsePosition} is a simple class used by {@code Format}43* and its subclasses to keep track of the current position during parsing.44* The {@code parseObject} method in the various {@code Format}45* classes requires a {@code ParsePosition} object as an argument.46*47* <p>48* By design, as you parse through a string with different formats,49* you can use the same {@code ParsePosition}, since the index parameter50* records the current position.51*52* @author Mark Davis53* @since 1.154* @see java.text.Format55*/5657public class ParsePosition {5859/**60* Input: the place you start parsing.61* <br>Output: position where the parse stopped.62* This is designed to be used serially,63* with each call setting index up for the next one.64*/65int index = 0;66int errorIndex = -1;6768/**69* Retrieve the current parse position. On input to a parse method, this70* is the index of the character at which parsing will begin; on output, it71* is the index of the character following the last character parsed.72*73* @return the current parse position74*/75public int getIndex() {76return index;77}7879/**80* Set the current parse position.81*82* @param index the current parse position83*/84public void setIndex(int index) {85this.index = index;86}8788/**89* Create a new ParsePosition with the given initial index.90*91* @param index initial index92*/93public ParsePosition(int index) {94this.index = index;95}96/**97* Set the index at which a parse error occurred. Formatters98* should set this before returning an error code from their99* parseObject method. The default value is -1 if this is not set.100*101* @param ei the index at which an error occurred102* @since 1.2103*/104public void setErrorIndex(int ei)105{106errorIndex = ei;107}108109/**110* Retrieve the index at which an error occurred, or -1 if the111* error index has not been set.112*113* @return the index at which an error occurred114* @since 1.2115*/116public int getErrorIndex()117{118return errorIndex;119}120121/**122* Overrides equals123*/124public boolean equals(Object obj)125{126if (obj == null) return false;127if (!(obj instanceof ParsePosition other))128return false;129return (index == other.index && errorIndex == other.errorIndex);130}131132/**133* Returns a hash code for this ParsePosition.134* @return a hash code value for this object135*/136public int hashCode() {137return (errorIndex << 16) | index;138}139140/**141* Return a string representation of this ParsePosition.142* @return a string representation of this object143*/144public String toString() {145return getClass().getName() +146"[index=" + index +147",errorIndex=" + errorIndex + ']';148}149}150151152