1. /*
  2. * @(#)FieldPosition.java 1.16 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. * @(#)FieldPosition.java 1.16 01/11/29
  9. *
  10. * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
  11. * (C) Copyright IBM Corp. 1996 - All Rights Reserved
  12. *
  13. * Portions copyright (c) 1996-1998 Sun Microsystems, Inc. All Rights Reserved.
  14. *
  15. * The original version of this source code and documentation is copyrighted
  16. * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  17. * materials are provided under terms of a License Agreement between Taligent
  18. * and Sun. This technology is protected by multiple US and International
  19. * patents. This notice and attribution to Taligent may not be removed.
  20. * Taligent is a registered trademark of Taligent, Inc.
  21. *
  22. * Permission to use, copy, modify, and distribute this software
  23. * and its documentation for NON-COMMERCIAL purposes and without
  24. * fee is hereby granted provided that this copyright notice
  25. * appears in all copies. Please refer to the file "copyright.html"
  26. * for further important copyright and licensing information.
  27. *
  28. * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  29. * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  30. * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  31. * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  32. * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  33. * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  34. *
  35. */
  36. package java.text;
  37. /**
  38. * <code>FieldPosition</code> is a simple class used by <code>Format</code>
  39. * and its subclasses to identify fields in formatted output. Fields are
  40. * identified by constants, whose names typically end with <code>_FIELD</code>,
  41. * defined in the various subclasses of <code>Format</code>. See
  42. * <code>ERA_FIELD</code> and its friends in <code>DateFormat</code> for
  43. * an example.
  44. *
  45. * <p>
  46. * <code>FieldPosition</code> keeps track of the position of the
  47. * field within the formatted output with two indices: the index
  48. * of the first character of the field and the index of the last
  49. * character of the field.
  50. *
  51. * <p>
  52. * One version of the <code>format</code> method in the various
  53. * <code>Format</code> classes requires a <code>FieldPosition</code>
  54. * object as an argument. You use this <code>format</code> method
  55. * to perform partial formatting or to get information about the
  56. * formatted output (such as the position of a field).
  57. *
  58. * @version 1.16 11/29/01
  59. * @author Mark Davis
  60. * @see java.text.Format
  61. */
  62. public class FieldPosition {
  63. /**
  64. * Input: Desired field to determine start and end offsets for.
  65. * The meaning depends on the subclass of Format.
  66. */
  67. int field = 0;
  68. /**
  69. * Output: End offset of field in text.
  70. * If the field does not occur in the text, 0 is returned.
  71. */
  72. int endIndex = 0;
  73. /**
  74. * Output: Start offset of field in text.
  75. * If the field does not occur in the text, 0 is returned.
  76. */
  77. int beginIndex = 0;
  78. /**
  79. * Creates a FieldPosition object for the given field. Fields are
  80. * identified by constants, whose names typically end with _FIELD,
  81. * in the various subclasses of Format.
  82. *
  83. * @see java.text.NumberFormat#INTEGER_FIELD
  84. * @see java.text.NumberFormat#FRACTION_FIELD
  85. * @see java.text.DateFormat#YEAR_FIELD
  86. * @see java.text.DateFormat#MONTH_FIELD
  87. */
  88. public FieldPosition(int field) {
  89. this.field = field;
  90. }
  91. /**
  92. * Retrieves the field identifier.
  93. */
  94. public int getField() {
  95. return field;
  96. }
  97. /**
  98. * Retrieves the index of the first character in the requested field.
  99. */
  100. public int getBeginIndex() {
  101. return beginIndex;
  102. }
  103. /**
  104. * Retrieves the index of the character following the last character in the
  105. * requested field.
  106. */
  107. public int getEndIndex() {
  108. return endIndex;
  109. }
  110. /**
  111. * Sets the begin index. For use by subclasses of Format.
  112. */
  113. public void setBeginIndex(int bi) {
  114. beginIndex = bi;
  115. }
  116. /**
  117. * Sets the end index. For use by subclasses of Format.
  118. */
  119. public void setEndIndex(int ei) {
  120. endIndex = ei;
  121. }
  122. /**
  123. * Overrides equals
  124. */
  125. public boolean equals(Object obj)
  126. {
  127. if (obj == null) return false;
  128. if (!(obj instanceof FieldPosition))
  129. return false;
  130. FieldPosition other = (FieldPosition) obj;
  131. return (beginIndex == other.beginIndex
  132. && endIndex == other.endIndex
  133. && field == other.field);
  134. }
  135. /**
  136. * Returns a hash code for this FieldPosition.
  137. * @return a hash code value for this object
  138. */
  139. public int hashCode() {
  140. return (field << 24) | (beginIndex << 16) | endIndex;
  141. }
  142. /**
  143. * Return a string representation of this FieldPosition.
  144. * @return a string representation of this object
  145. */
  146. public String toString() {
  147. return getClass().getName() +
  148. "[field=" + field +
  149. ",beginIndex=" + beginIndex +
  150. ",endIndex=" + endIndex + ']';
  151. }
  152. }