1 /*
2  * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 /*
27  * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
28  * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
29  *
30  *   The original version of this source code and documentation is copyrighted
31  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
32  * materials are provided under terms of a License Agreement between Taligent
33  * and Sun. This technology is protected by multiple US and International
34  * patents. This notice and attribution to Taligent may not be removed.
35  *   Taligent is a registered trademark of Taligent, Inc.
36  *
37  */
38 
39 package java.text;
40 
41 
42 /**
43  * <code>ParsePosition</code> is a simple class used by <code>Format</code>
44  * and its subclasses to keep track of the current position during parsing.
45  * The <code>parseObject</code> method in the various <code>Format</code>
46  * classes requires a <code>ParsePosition</code> object as an argument.
47  *
48  * <p>
49  * By design, as you parse through a string with different formats,
50  * you can use the same <code>ParsePosition</code>, since the index parameter
51  * records the current position.
52  *
53  * @author      Mark Davis
54  * @see         java.text.Format
55  */
56 
57 public class ParsePosition {
58 
59     /**
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      */
65     int index = 0;
66     int errorIndex = -1;
67 
68     /**
69      * Retrieve the current parse position.  On input to a parse method, this
70      * is the index of the character at which parsing will begin; on output, it
71      * is the index of the character following the last character parsed.
72      *
73      * @return the current parse position
74      */
getIndex()75     public int getIndex() {
76         return index;
77     }
78 
79     /**
80      * Set the current parse position.
81      *
82      * @param index the current parse position
83      */
setIndex(int index)84     public void setIndex(int index) {
85         this.index = index;
86     }
87 
88     /**
89      * Create a new ParsePosition with the given initial index.
90      *
91      * @param index initial index
92      */
ParsePosition(int index)93     public ParsePosition(int index) {
94         this.index = index;
95     }
96     /**
97      * Set the index at which a parse error occurred.  Formatters
98      * should set this before returning an error code from their
99      * parseObject method.  The default value is -1 if this is not set.
100      *
101      * @param ei the index at which an error occurred
102      * @since 1.2
103      */
setErrorIndex(int ei)104     public void setErrorIndex(int ei)
105     {
106         errorIndex = ei;
107     }
108 
109     /**
110      * Retrieve the index at which an error occurred, or -1 if the
111      * error index has not been set.
112      *
113      * @return the index at which an error occurred
114      * @since 1.2
115      */
getErrorIndex()116     public int getErrorIndex()
117     {
118         return errorIndex;
119     }
120 
121     /**
122      * Overrides equals
123      */
equals(Object obj)124     public boolean equals(Object obj)
125     {
126         if (obj == null) return false;
127         if (!(obj instanceof ParsePosition))
128             return false;
129         ParsePosition other = (ParsePosition) obj;
130         return (index == other.index && errorIndex == other.errorIndex);
131     }
132 
133     /**
134      * Returns a hash code for this ParsePosition.
135      * @return a hash code value for this object
136      */
hashCode()137     public int hashCode() {
138         return (errorIndex << 16) | index;
139     }
140 
141     /**
142      * Return a string representation of this ParsePosition.
143      * @return  a string representation of this object
144      */
toString()145     public String toString() {
146         return getClass().getName() +
147             "[index=" + index +
148             ",errorIndex=" + errorIndex + ']';
149     }
150 }
151