1. /*
  2. * @(#)ParsePosition.java 1.15 00/01/19
  3. *
  4. * Copyright 1996-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. /*
  11. * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
  12. * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
  13. *
  14. * The original version of this source code and documentation is copyrighted
  15. * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  16. * materials are provided under terms of a License Agreement between Taligent
  17. * and Sun. This technology is protected by multiple US and International
  18. * patents. This notice and attribution to Taligent may not be removed.
  19. * Taligent is a registered trademark of Taligent, Inc.
  20. *
  21. */
  22. package java.text;
  23. /**
  24. * <code>ParsePosition</code> is a simple class used by <code>Format</code>
  25. * and its subclasses to keep track of the current position during parsing.
  26. * The <code>parseObject</code> method in the various <code>Format</code>
  27. * classes requires a <code>ParsePosition</code> object as an argument.
  28. *
  29. * <p>
  30. * By design, as you parse through a string with different formats,
  31. * you can use the same <code>ParsePosition</code>, since the index parameter
  32. * records the current position.
  33. *
  34. * @version 1.15 01/19/00
  35. * @author Mark Davis
  36. * @see java.text.Format
  37. */
  38. public class ParsePosition {
  39. /**
  40. * Input: the place you start parsing.
  41. * <br>Output: position where the parse stopped.
  42. * This is designed to be used serially,
  43. * with each call setting index up for the next one.
  44. */
  45. int index = 0;
  46. int errorIndex = -1;
  47. /**
  48. * Retrieve the current parse position. On input to a parse method, this
  49. * is the index of the character at which parsing will begin; on output, it
  50. * is the index of the character following the last character parsed.
  51. */
  52. public int getIndex() {
  53. return index;
  54. }
  55. /**
  56. * Set the current parse position.
  57. */
  58. public void setIndex(int index) {
  59. this.index = index;
  60. }
  61. /**
  62. * Create a new ParsePosition with the given initial index.
  63. */
  64. public ParsePosition(int index) {
  65. this.index = index;
  66. }
  67. /**
  68. * Set the index at which a parse error occurred. Formatters
  69. * should set this before returning an error code from their
  70. * parseObject method. The default value is -1 if this is not set.
  71. */
  72. public void setErrorIndex(int ei)
  73. {
  74. errorIndex = ei;
  75. }
  76. /**
  77. * Retrieve the index at which an error occurred, or -1 if the
  78. * error index has not been set.
  79. */
  80. public int getErrorIndex()
  81. {
  82. return errorIndex;
  83. }
  84. /**
  85. * Overrides equals
  86. */
  87. public boolean equals(Object obj)
  88. {
  89. if (obj == null) return false;
  90. if (!(obj instanceof ParsePosition))
  91. return false;
  92. ParsePosition other = (ParsePosition) obj;
  93. return (index == other.index && errorIndex == other.errorIndex);
  94. }
  95. /**
  96. * Returns a hash code for this ParsePosition.
  97. * @return a hash code value for this object
  98. */
  99. public int hashCode() {
  100. return (errorIndex << 16) | index;
  101. }
  102. /**
  103. * Return a string representation of this ParsePosition.
  104. * @return a string representation of this object
  105. */
  106. public String toString() {
  107. return getClass().getName() +
  108. "[index=" + index +
  109. ",errorIndex=" + errorIndex + ']';
  110. }
  111. }