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