1. /*
  2. * @(#)FieldPosition.java 1.16 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 - All Rights Reserved
  12. * (C) Copyright IBM Corp. 1996 - 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>FieldPosition</code> is a simple class used by <code>Format</code>
  25. * and its subclasses to identify fields in formatted output. Fields are
  26. * identified by constants, whose names typically end with <code>_FIELD</code>,
  27. * defined in the various subclasses of <code>Format</code>. See
  28. * <code>ERA_FIELD</code> and its friends in <code>DateFormat</code> for
  29. * an example.
  30. *
  31. * <p>
  32. * <code>FieldPosition</code> keeps track of the position of the
  33. * field within the formatted output with two indices: the index
  34. * of the first character of the field and the index of the last
  35. * character of the field.
  36. *
  37. * <p>
  38. * One version of the <code>format</code> method in the various
  39. * <code>Format</code> classes requires a <code>FieldPosition</code>
  40. * object as an argument. You use this <code>format</code> method
  41. * to perform partial formatting or to get information about the
  42. * formatted output (such as the position of a field).
  43. *
  44. * @version 1.16 01/19/00
  45. * @author Mark Davis
  46. * @see java.text.Format
  47. */
  48. public class FieldPosition {
  49. /**
  50. * Input: Desired field to determine start and end offsets for.
  51. * The meaning depends on the subclass of Format.
  52. */
  53. int field = 0;
  54. /**
  55. * Output: End offset of field in text.
  56. * If the field does not occur in the text, 0 is returned.
  57. */
  58. int endIndex = 0;
  59. /**
  60. * Output: Start offset of field in text.
  61. * If the field does not occur in the text, 0 is returned.
  62. */
  63. int beginIndex = 0;
  64. /**
  65. * Creates a FieldPosition object for the given field. Fields are
  66. * identified by constants, whose names typically end with _FIELD,
  67. * in the various subclasses of Format.
  68. *
  69. * @see java.text.NumberFormat#INTEGER_FIELD
  70. * @see java.text.NumberFormat#FRACTION_FIELD
  71. * @see java.text.DateFormat#YEAR_FIELD
  72. * @see java.text.DateFormat#MONTH_FIELD
  73. */
  74. public FieldPosition(int field) {
  75. this.field = field;
  76. }
  77. /**
  78. * Retrieves the field identifier.
  79. */
  80. public int getField() {
  81. return field;
  82. }
  83. /**
  84. * Retrieves the index of the first character in the requested field.
  85. */
  86. public int getBeginIndex() {
  87. return beginIndex;
  88. }
  89. /**
  90. * Retrieves the index of the character following the last character in the
  91. * requested field.
  92. */
  93. public int getEndIndex() {
  94. return endIndex;
  95. }
  96. /**
  97. * Sets the begin index. For use by subclasses of Format.
  98. */
  99. public void setBeginIndex(int bi) {
  100. beginIndex = bi;
  101. }
  102. /**
  103. * Sets the end index. For use by subclasses of Format.
  104. */
  105. public void setEndIndex(int ei) {
  106. endIndex = ei;
  107. }
  108. /**
  109. * Overrides equals
  110. */
  111. public boolean equals(Object obj)
  112. {
  113. if (obj == null) return false;
  114. if (!(obj instanceof FieldPosition))
  115. return false;
  116. FieldPosition other = (FieldPosition) obj;
  117. return (beginIndex == other.beginIndex
  118. && endIndex == other.endIndex
  119. && field == other.field);
  120. }
  121. /**
  122. * Returns a hash code for this FieldPosition.
  123. * @return a hash code value for this object
  124. */
  125. public int hashCode() {
  126. return (field << 24) | (beginIndex << 16) | endIndex;
  127. }
  128. /**
  129. * Return a string representation of this FieldPosition.
  130. * @return a string representation of this object
  131. */
  132. public String toString() {
  133. return getClass().getName() +
  134. "[field=" + field +
  135. ",beginIndex=" + beginIndex +
  136. ",endIndex=" + endIndex + ']';
  137. }
  138. }