rt/emul/compact/src/main/java/java/text/ParsePosition.java
branchjdk7-b147
changeset 1334 588d5bf7a560
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/java/text/ParsePosition.java	Thu Oct 03 15:40:35 2013 +0200
     1.3 @@ -0,0 +1,139 @@
     1.4 +/*
     1.5 + * Copyright (c) 1996, 2002, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +/*
    1.30 + * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
    1.31 + * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
    1.32 + *
    1.33 + *   The original version of this source code and documentation is copyrighted
    1.34 + * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
    1.35 + * materials are provided under terms of a License Agreement between Taligent
    1.36 + * and Sun. This technology is protected by multiple US and International
    1.37 + * patents. This notice and attribution to Taligent may not be removed.
    1.38 + *   Taligent is a registered trademark of Taligent, Inc.
    1.39 + *
    1.40 + */
    1.41 +
    1.42 +package java.text;
    1.43 +
    1.44 +
    1.45 +/**
    1.46 + * <code>ParsePosition</code> is a simple class used by <code>Format</code>
    1.47 + * and its subclasses to keep track of the current position during parsing.
    1.48 + * The <code>parseObject</code> method in the various <code>Format</code>
    1.49 + * classes requires a <code>ParsePosition</code> object as an argument.
    1.50 + *
    1.51 + * <p>
    1.52 + * By design, as you parse through a string with different formats,
    1.53 + * you can use the same <code>ParsePosition</code>, since the index parameter
    1.54 + * records the current position.
    1.55 + *
    1.56 + * @author      Mark Davis
    1.57 + * @see         java.text.Format
    1.58 + */
    1.59 +
    1.60 +public class ParsePosition {
    1.61 +
    1.62 +    /**
    1.63 +     * Input: the place you start parsing.
    1.64 +     * <br>Output: position where the parse stopped.
    1.65 +     * This is designed to be used serially,
    1.66 +     * with each call setting index up for the next one.
    1.67 +     */
    1.68 +    int index = 0;
    1.69 +    int errorIndex = -1;
    1.70 +
    1.71 +    /**
    1.72 +     * Retrieve the current parse position.  On input to a parse method, this
    1.73 +     * is the index of the character at which parsing will begin; on output, it
    1.74 +     * is the index of the character following the last character parsed.
    1.75 +     */
    1.76 +    public int getIndex() {
    1.77 +        return index;
    1.78 +    }
    1.79 +
    1.80 +    /**
    1.81 +     * Set the current parse position.
    1.82 +     */
    1.83 +    public void setIndex(int index) {
    1.84 +        this.index = index;
    1.85 +    }
    1.86 +
    1.87 +    /**
    1.88 +     * Create a new ParsePosition with the given initial index.
    1.89 +     */
    1.90 +    public ParsePosition(int index) {
    1.91 +        this.index = index;
    1.92 +    }
    1.93 +    /**
    1.94 +     * Set the index at which a parse error occurred.  Formatters
    1.95 +     * should set this before returning an error code from their
    1.96 +     * parseObject method.  The default value is -1 if this is not set.
    1.97 +     * @since 1.2
    1.98 +     */
    1.99 +    public void setErrorIndex(int ei)
   1.100 +    {
   1.101 +        errorIndex = ei;
   1.102 +    }
   1.103 +
   1.104 +    /**
   1.105 +     * Retrieve the index at which an error occurred, or -1 if the
   1.106 +     * error index has not been set.
   1.107 +     * @since 1.2
   1.108 +     */
   1.109 +    public int getErrorIndex()
   1.110 +    {
   1.111 +        return errorIndex;
   1.112 +    }
   1.113 +    /**
   1.114 +     * Overrides equals
   1.115 +     */
   1.116 +    public boolean equals(Object obj)
   1.117 +    {
   1.118 +        if (obj == null) return false;
   1.119 +        if (!(obj instanceof ParsePosition))
   1.120 +            return false;
   1.121 +        ParsePosition other = (ParsePosition) obj;
   1.122 +        return (index == other.index && errorIndex == other.errorIndex);
   1.123 +    }
   1.124 +
   1.125 +    /**
   1.126 +     * Returns a hash code for this ParsePosition.
   1.127 +     * @return a hash code value for this object
   1.128 +     */
   1.129 +    public int hashCode() {
   1.130 +        return (errorIndex << 16) | index;
   1.131 +    }
   1.132 +
   1.133 +    /**
   1.134 +     * Return a string representation of this ParsePosition.
   1.135 +     * @return  a string representation of this object
   1.136 +     */
   1.137 +    public String toString() {
   1.138 +        return getClass().getName() +
   1.139 +            "[index=" + index +
   1.140 +            ",errorIndex=" + errorIndex + ']';
   1.141 +    }
   1.142 +}