KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > text > ParsePosition


1 /*
2  * @(#)ParsePosition.java 1.19 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 /*
9  * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
10  * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
11  *
12  * The original version of this source code and documentation is copyrighted
13  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
14  * materials are provided under terms of a License Agreement between Taligent
15  * and Sun. This technology is protected by multiple US and International
16  * patents. This notice and attribution to Taligent may not be removed.
17  * Taligent is a registered trademark of Taligent, Inc.
18  *
19  */

20
21 package java.text;
22
23
24 /**
25  * <code>ParsePosition</code> is a simple class used by <code>Format</code>
26  * and its subclasses to keep track of the current position during parsing.
27  * The <code>parseObject</code> method in the various <code>Format</code>
28  * classes requires a <code>ParsePosition</code> object as an argument.
29  *
30  * <p>
31  * By design, as you parse through a string with different formats,
32  * you can use the same <code>ParsePosition</code>, since the index parameter
33  * records the current position.
34  *
35  * @version 1.19 12/19/03
36  * @author Mark Davis
37  * @see java.text.Format
38  */

39
40 public class ParsePosition {
41
42     /**
43      * Input: the place you start parsing.
44      * <br>Output: position where the parse stopped.
45      * This is designed to be used serially,
46      * with each call setting index up for the next one.
47      */

48     int index = 0;
49     int errorIndex = -1;
50
51     /**
52      * Retrieve the current parse position. On input to a parse method, this
53      * is the index of the character at which parsing will begin; on output, it
54      * is the index of the character following the last character parsed.
55      */

56     public int getIndex() {
57         return index;
58     }
59
60     /**
61      * Set the current parse position.
62      */

63     public void setIndex(int index) {
64         this.index = index;
65     }
66
67     /**
68      * Create a new ParsePosition with the given initial index.
69      */

70     public ParsePosition(int index) {
71         this.index = index;
72     }
73     /**
74      * Set the index at which a parse error occurred. Formatters
75      * should set this before returning an error code from their
76      * parseObject method. The default value is -1 if this is not set.
77      * @since 1.2
78      */

79     public void setErrorIndex(int ei)
80     {
81         errorIndex = ei;
82     }
83
84     /**
85      * Retrieve the index at which an error occurred, or -1 if the
86      * error index has not been set.
87      * @since 1.2
88      */

89     public int getErrorIndex()
90     {
91         return errorIndex;
92     }
93     /**
94      * Overrides equals
95      */

96     public boolean equals(Object JavaDoc obj)
97     {
98         if (obj == null) return false;
99         if (!(obj instanceof ParsePosition JavaDoc))
100             return false;
101         ParsePosition JavaDoc other = (ParsePosition JavaDoc) obj;
102         return (index == other.index && errorIndex == other.errorIndex);
103     }
104
105     /**
106      * Returns a hash code for this ParsePosition.
107      * @return a hash code value for this object
108      */

109     public int hashCode() {
110         return (errorIndex << 16) | index;
111     }
112
113     /**
114      * Return a string representation of this ParsePosition.
115      * @return a string representation of this object
116      */

117     public String JavaDoc toString() {
118         return getClass().getName() +
119             "[index=" + index +
120             ",errorIndex=" + errorIndex + ']';
121     }
122 }
123
Popular Tags