summaryrefslogtreecommitdiffstats
path: root/luni/src/main/java/java/text/ParsePosition.java
blob: 71ce866a0408ad24ecc1af85b8b776e02814ca5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package java.text;

/**
 * Tracks the current position in a parsed string. In case of an error the error
 * index can be set to the position where the error occurred without having to
 * change the parse position.
 */
public class ParsePosition {

    private int currentPosition, errorIndex = -1;

    /**
     * Constructs a new {@code ParsePosition} with the specified index.
     *
     * @param index
     *            the index to begin parsing.
     */
    public ParsePosition(int index) {
        currentPosition = index;
    }

    /**
     * Compares the specified object to this {@code ParsePosition} and indicates
     * if they are equal. In order to be equal, {@code object} must be an
     * instance of {@code ParsePosition} and it must have the same index and
     * error index.
     *
     * @param object
     *            the object to compare with this object.
     * @return {@code true} if the specified object is equal to this
     *         {@code ParsePosition}; {@code false} otherwise.
     * @see #hashCode
     */
    @Override
    public boolean equals(Object object) {
        if (!(object instanceof ParsePosition)) {
            return false;
        }
        ParsePosition pos = (ParsePosition) object;
        return currentPosition == pos.currentPosition
                && errorIndex == pos.errorIndex;
    }

    /**
     * Returns the index at which the parse could not continue.
     *
     * @return the index of the parse error or -1 if there is no error.
     */
    public int getErrorIndex() {
        return errorIndex;
    }

    /**
     * Returns the current parse position.
     *
     * @return the current position.
     */
    public int getIndex() {
        return currentPosition;
    }

    @Override
    public int hashCode() {
        return currentPosition + errorIndex;
    }

    /**
     * Sets the index at which the parse could not continue.
     *
     * @param index
     *            the index of the parse error.
     */
    public void setErrorIndex(int index) {
        errorIndex = index;
    }

    /**
     * Sets the current parse position.
     *
     * @param index
     *            the current parse position.
     */
    public void setIndex(int index) {
        currentPosition = index;
    }

    /**
     * Returns the string representation of this parse position.
     *
     * @return the string representation of this parse position.
     */
    @Override
    public String toString() {
        return getClass().getName() + "[index=" + currentPosition
                + ", errorIndex=" + errorIndex + "]";
    }
}