FieldPosition.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* FieldPosition.java -- Keeps track of field positions while formatting
  2. Copyright (C) 1998, 1999, 2001, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.text;
  32. /**
  33. * This class is used by the java.text formatting classes to track
  34. * field positions. A field position is defined by an identifier value
  35. * and begin and end index positions. The formatting classes in java.text
  36. * typically define constant values for the field identifiers.
  37. *
  38. * @author Aaron M. Renn (arenn@urbanophile.com)
  39. * @author Per Bothner (bothner@cygnus.com)
  40. */
  41. public class FieldPosition
  42. {
  43. /**
  44. * This is the field identifier value.
  45. */
  46. private int field_id;
  47. /**
  48. * This is the beginning index of the field.
  49. */
  50. private int begin;
  51. /**
  52. * This is the ending index of the field.
  53. */
  54. private int end;
  55. /**
  56. * This is the field attribute value.
  57. */
  58. private Format.Field field_attribute;
  59. /**
  60. * This method initializes a new instance of <code>FieldPosition</code>
  61. * to have the specified field attribute. The attribute will be used as
  62. * an id. It is formally equivalent to calling FieldPosition(field, -1).
  63. *
  64. * @param field The field format attribute.
  65. */
  66. public FieldPosition (Format.Field field)
  67. {
  68. this(field, -1);
  69. }
  70. /**
  71. * This method initializes a new instance of <code>FieldPosition</code>
  72. * to have the specified field attribute. The attribute will be used as
  73. * an id is non null. The integer field id is only used if the Format.Field
  74. * attribute is not used by the formatter.
  75. *
  76. * @param field The field format attribute.
  77. * @param field_id The field identifier value.
  78. */
  79. public FieldPosition (Format.Field field, int field_id)
  80. {
  81. this.field_attribute = field;
  82. this.field_id = field_id;
  83. }
  84. /**
  85. * This method initializes a new instance of <code>FieldPosition</code> to
  86. * have the specified field id.
  87. *
  88. * @param field_id The field identifier value.
  89. */
  90. public FieldPosition (int field_id)
  91. {
  92. this.field_id = field_id;
  93. }
  94. /**
  95. * This method returns the field identifier value for this object.
  96. *
  97. * @return The field identifier.
  98. */
  99. public int getField ()
  100. {
  101. return field_id;
  102. }
  103. public Format.Field getFieldAttribute ()
  104. {
  105. return field_attribute;
  106. }
  107. /**
  108. * This method returns the beginning index for this field.
  109. *
  110. * @return The beginning index.
  111. */
  112. public int getBeginIndex ()
  113. {
  114. return begin;
  115. }
  116. /**
  117. * This method sets the beginning index of this field to the specified value.
  118. *
  119. * @param begin The new beginning index.
  120. */
  121. public void setBeginIndex (int begin)
  122. {
  123. this.begin = begin;
  124. }
  125. /**
  126. * This method returns the ending index for the field.
  127. *
  128. * @return The ending index.
  129. */
  130. public int getEndIndex ()
  131. {
  132. return end;
  133. }
  134. /**
  135. * This method sets the ending index of this field to the specified value.
  136. *
  137. * @param end The new ending index.
  138. */
  139. public void setEndIndex (int end)
  140. {
  141. this.end = end;
  142. }
  143. /**
  144. * This method tests this object for equality against the specified object.
  145. * The objects will be considered equal if and only if:
  146. * <p>
  147. * <ul>
  148. * <li>The specified object is not <code>null</code>.
  149. * <li>The specified object has the same class as this object.
  150. * <li>The specified object has the same field identifier, field attribute
  151. * and beginning and ending index as this object.
  152. * </ul>
  153. *
  154. * @param obj The object to test for equality to this object.
  155. *
  156. * @return <code>true</code> if the specified object is equal to
  157. * this object, <code>false</code> otherwise.
  158. */
  159. public boolean equals (Object obj)
  160. {
  161. if (this == obj)
  162. return true;
  163. if (obj == null || obj.getClass() != this.getClass())
  164. return false;
  165. FieldPosition fp = (FieldPosition) obj;
  166. return (field_id == fp.field_id
  167. && (field_attribute == fp.field_attribute
  168. || (field_attribute != null
  169. && field_attribute.equals(fp.field_attribute)))
  170. && begin == fp.begin
  171. && end == fp.end);
  172. }
  173. /**
  174. * This method returns a hash value for this object
  175. *
  176. * @return A hash value for this object.
  177. */
  178. public int hashCode ()
  179. {
  180. int hash = 5;
  181. hash = 31 * hash + field_id;
  182. hash = 31 * hash + begin;
  183. hash = 31 * hash + end;
  184. hash = 31 * hash +
  185. (null == field_attribute ? 0 : field_attribute.hashCode());
  186. return hash;
  187. }
  188. /**
  189. * This method returns a <code>String</code> representation of this
  190. * object.
  191. *
  192. * @return A <code>String</code> representation of this object.
  193. */
  194. public String toString ()
  195. {
  196. return (getClass ().getName ()
  197. + "[field=" + getField ()
  198. + ",attribute=" + getFieldAttribute ()
  199. + ",beginIndex=" + getBeginIndex ()
  200. + ",endIndex=" + getEndIndex ()
  201. + "]");
  202. }
  203. }