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