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