001 /*
002 * Copyright 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 /*
027 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
028 * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
029 *
030 * The original version of this source code and documentation is copyrighted
031 * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
032 * materials are provided under terms of a License Agreement between Taligent
033 * and Sun. This technology is protected by multiple US and International
034 * patents. This notice and attribution to Taligent may not be removed.
035 * Taligent is a registered trademark of Taligent, Inc.
036 *
037 */
038
039 package java.text;
040
041 /**
042 * <code>ParsePosition</code> is a simple class used by <code>Format</code>
043 * and its subclasses to keep track of the current position during parsing.
044 * The <code>parseObject</code> method in the various <code>Format</code>
045 * classes requires a <code>ParsePosition</code> object as an argument.
046 *
047 * <p>
048 * By design, as you parse through a string with different formats,
049 * you can use the same <code>ParsePosition</code>, since the index parameter
050 * records the current position.
051 *
052 * @version 1.26 05/05/07
053 * @author Mark Davis
054 * @see java.text.Format
055 */
056
057 public class ParsePosition {
058
059 /**
060 * Input: the place you start parsing.
061 * <br>Output: position where the parse stopped.
062 * This is designed to be used serially,
063 * with each call setting index up for the next one.
064 */
065 int index = 0;
066 int errorIndex = -1;
067
068 /**
069 * Retrieve the current parse position. On input to a parse method, this
070 * is the index of the character at which parsing will begin; on output, it
071 * is the index of the character following the last character parsed.
072 */
073 public int getIndex() {
074 return index;
075 }
076
077 /**
078 * Set the current parse position.
079 */
080 public void setIndex(int index) {
081 this .index = index;
082 }
083
084 /**
085 * Create a new ParsePosition with the given initial index.
086 */
087 public ParsePosition(int index) {
088 this .index = index;
089 }
090
091 /**
092 * Set the index at which a parse error occurred. Formatters
093 * should set this before returning an error code from their
094 * parseObject method. The default value is -1 if this is not set.
095 * @since 1.2
096 */
097 public void setErrorIndex(int ei) {
098 errorIndex = ei;
099 }
100
101 /**
102 * Retrieve the index at which an error occurred, or -1 if the
103 * error index has not been set.
104 * @since 1.2
105 */
106 public int getErrorIndex() {
107 return errorIndex;
108 }
109
110 /**
111 * Overrides equals
112 */
113 public boolean equals(Object obj) {
114 if (obj == null)
115 return false;
116 if (!(obj instanceof ParsePosition))
117 return false;
118 ParsePosition other = (ParsePosition) obj;
119 return (index == other.index && errorIndex == other.errorIndex);
120 }
121
122 /**
123 * Returns a hash code for this ParsePosition.
124 * @return a hash code value for this object
125 */
126 public int hashCode() {
127 return (errorIndex << 16) | index;
128 }
129
130 /**
131 * Return a string representation of this ParsePosition.
132 * @return a string representation of this object
133 */
134 public String toString() {
135 return getClass().getName() + "[index=" + index
136 + ",errorIndex=" + errorIndex + ']';
137 }
138 }
|