1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package java.text;
19 
20 /**
21  * Tracks the current position in a parsed string. In case of an error the error
22  * index can be set to the position where the error occurred without having to
23  * change the parse position.
24  */
25 public class ParsePosition {
26 
27     private int currentPosition, errorIndex = -1;
28 
29     /**
30      * Constructs a new {@code ParsePosition} with the specified index.
31      *
32      * @param index
33      *            the index to begin parsing.
34      */
ParsePosition(int index)35     public ParsePosition(int index) {
36         currentPosition = index;
37     }
38 
39     /**
40      * Compares the specified object to this {@code ParsePosition} and indicates
41      * if they are equal. In order to be equal, {@code object} must be an
42      * instance of {@code ParsePosition} and it must have the same index and
43      * error index.
44      *
45      * @param object
46      *            the object to compare with this object.
47      * @return {@code true} if the specified object is equal to this
48      *         {@code ParsePosition}; {@code false} otherwise.
49      * @see #hashCode
50      */
51     @Override
equals(Object object)52     public boolean equals(Object object) {
53         if (!(object instanceof ParsePosition)) {
54             return false;
55         }
56         ParsePosition pos = (ParsePosition) object;
57         return currentPosition == pos.currentPosition
58                 && errorIndex == pos.errorIndex;
59     }
60 
61     /**
62      * Returns the index at which the parse could not continue.
63      *
64      * @return the index of the parse error or -1 if there is no error.
65      */
getErrorIndex()66     public int getErrorIndex() {
67         return errorIndex;
68     }
69 
70     /**
71      * Returns the current parse position.
72      *
73      * @return the current position.
74      */
getIndex()75     public int getIndex() {
76         return currentPosition;
77     }
78 
79     @Override
hashCode()80     public int hashCode() {
81         return currentPosition + errorIndex;
82     }
83 
84     /**
85      * Sets the index at which the parse could not continue.
86      *
87      * @param index
88      *            the index of the parse error.
89      */
setErrorIndex(int index)90     public void setErrorIndex(int index) {
91         errorIndex = index;
92     }
93 
94     /**
95      * Sets the current parse position.
96      *
97      * @param index
98      *            the current parse position.
99      */
setIndex(int index)100     public void setIndex(int index) {
101         currentPosition = index;
102     }
103 
104     /**
105      * Returns the string representation of this parse position.
106      *
107      * @return the string representation of this parse position.
108      */
109     @Override
toString()110     public String toString() {
111         return getClass().getName() + "[index=" + currentPosition
112                 + ", errorIndex=" + errorIndex + "]";
113     }
114 }
115