home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / text / ParsePosition.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  4.1 KB  |  132 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)ParsePosition.java    1.14 98/07/24
  3.  *
  4.  * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
  5.  * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
  6.  *
  7.  * Portions copyright (c) 1996-1998 Sun Microsystems, Inc.
  8.  * All Rights Reserved.
  9.  *
  10.  *   The original version of this source code and documentation is copyrighted
  11.  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  12.  * materials are provided under terms of a License Agreement between Taligent
  13.  * and Sun. This technology is protected by multiple US and International
  14.  * patents. This notice and attribution to Taligent may not be removed.
  15.  *   Taligent is a registered trademark of Taligent, Inc.
  16.  *
  17.  * Permission to use, copy, modify, and distribute this software
  18.  * and its documentation for NON-COMMERCIAL purposes and without
  19.  * fee is hereby granted provided that this copyright notice
  20.  * appears in all copies. Please refer to the file "copyright.html"
  21.  * for further important copyright and licensing information.
  22.  *
  23.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  24.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  25.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  26.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  27.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  28.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  29.  *
  30.  */
  31.  
  32. package java.text;
  33.  
  34.  
  35. /**
  36.  * <code>ParsePosition</code> is a simple class used by <code>Format</code>
  37.  * and its subclasses to keep track of the current position during parsing.
  38.  * The <code>parseObject</code> method in the various <code>Format</code>
  39.  * classes requires a <code>ParsePosition</code> object as an argument.
  40.  *
  41.  * <p>
  42.  * By design, as you parse through a string with different formats,
  43.  * you can use the same <code>ParsePosition</code>, since the index parameter
  44.  * records the current position.
  45.  *
  46.  * @version     1.14 07/24/98
  47.  * @author      Mark Davis
  48.  * @see         java.text.Format
  49.  */
  50.  
  51. public class ParsePosition {
  52.  
  53.     /**
  54.      * Input: the place you start parsing.
  55.      * <br>Output: position where the parse stopped.
  56.      * This is designed to be used serially,
  57.      * with each call setting index up for the next one.
  58.      */
  59.     int index = 0;
  60.     int errorIndex = -1;
  61.  
  62.     /**
  63.      * Retrieve the current parse position.  On input to a parse method, this
  64.      * is the index of the character at which parsing will begin; on output, it
  65.      * is the index of the character following the last character parsed.
  66.      */
  67.     public int getIndex() {
  68.         return index;
  69.     }
  70.  
  71.     /**
  72.      * Set the current parse position.
  73.      */
  74.     public void setIndex(int index) {
  75.         this.index = index;
  76.     }
  77.  
  78.     /**
  79.      * Create a new ParsePosition with the given initial index.
  80.      */
  81.     public ParsePosition(int index) {
  82.         this.index = index;
  83.     }
  84.     /**
  85.      * Set the index at which a parse error occurred.  Formatters
  86.      * should set this before returning an error code from their
  87.      * parseObject method.  The default value is -1 if this is not set.
  88.      */
  89.     public void setErrorIndex(int ei)
  90.     {
  91.         errorIndex = ei;
  92.     }
  93.  
  94.     /**
  95.      * Retrieve the index at which an error occurred, or -1 if the
  96.      * error index has not been set.
  97.      */
  98.     public int getErrorIndex()
  99.     {
  100.         return errorIndex;
  101.     }
  102.     /**
  103.      * Overrides equals
  104.      */
  105.     public boolean equals(Object obj)
  106.     {
  107.         if (obj == null) return false;
  108.         if (!(obj instanceof ParsePosition))
  109.             return false;
  110.         ParsePosition other = (ParsePosition) obj;
  111.         return (index == other.index && errorIndex == other.errorIndex);
  112.     }
  113.  
  114.     /**
  115.      * Returns a hash code for this ParsePosition.
  116.      * @return a hash code value for this object
  117.      */
  118.     public int hashCode() {
  119.         return (errorIndex << 16) | index;
  120.     }
  121.  
  122.     /**
  123.      * Return a string representation of this ParsePosition.
  124.      * @return  a string representation of this object
  125.      */
  126.     public String toString() {
  127.         return getClass().getName() +
  128.             "[index=" + index +
  129.             ",errorIndex=" + errorIndex + ']';
  130.     }
  131. }
  132.