001/* FieldPosition.java -- Keeps track of field positions while formatting
002   Copyright (C) 1998, 1999, 2001, 2005  Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038
039package java.text;
040
041/**
042 * This class is used by the java.text formatting classes to track
043 * field positions.  A field position is defined by an identifier value
044 * and begin and end index positions.  The formatting classes in java.text
045 * typically define constant values for the field identifiers.
046 *
047 * @author Aaron M. Renn (arenn@urbanophile.com)
048 * @author Per Bothner (bothner@cygnus.com)
049 */
050public class FieldPosition
051{
052  /**
053   * This is the field identifier value.
054   */
055  private int field_id;
056
057  /**
058   * This is the beginning index of the field.
059   */
060  private int begin;
061
062  /**
063   * This is the ending index of the field.
064   */
065  private int end;
066
067  /**
068   * This is the field attribute value.
069   */
070  private Format.Field field_attribute;
071
072  /**
073   * This method initializes a new instance of <code>FieldPosition</code>
074   * to have the specified field attribute. The attribute will be used as
075   * an id. It is formally equivalent to calling FieldPosition(field, -1).
076   *
077   * @param field The field format attribute.
078   */
079  public FieldPosition (Format.Field field)
080  {
081    this(field, -1);
082  }
083
084  /**
085   * This method initializes a new instance of <code>FieldPosition</code>
086   * to have the specified field attribute. The attribute will be used as
087   * an id is non null. The integer field id is only used if the Format.Field
088   * attribute is not used by the formatter.
089   *
090   * @param field The field format attribute.
091   * @param field_id The field identifier value.
092   */
093  public FieldPosition (Format.Field field, int field_id)
094  {
095    this.field_attribute = field;
096    this.field_id = field_id;
097  }
098
099  /**
100   * This method initializes a new instance of <code>FieldPosition</code> to
101   * have the specified field id.
102   *
103   * @param field_id The field identifier value.
104   */
105  public FieldPosition (int field_id)
106  {
107    this.field_id = field_id;
108  }
109
110  /**
111   * This method returns the field identifier value for this object.
112   *
113   * @return The field identifier.
114   */
115  public int getField ()
116  {
117    return field_id;
118  }
119
120  public Format.Field getFieldAttribute ()
121  {
122    return field_attribute;
123  }
124
125  /**
126   * This method returns the beginning index for this field.
127   *
128   * @return The beginning index.
129   */
130  public int getBeginIndex ()
131  {
132    return begin;
133  }
134
135  /**
136   * This method sets the beginning index of this field to the specified value.
137   *
138   * @param begin The new beginning index.
139   */
140  public void setBeginIndex (int begin)
141  {
142    this.begin = begin;
143  }
144
145  /**
146   * This method returns the ending index for the field.
147   *
148   * @return The ending index.
149   */
150  public int getEndIndex ()
151  {
152    return end;
153  }
154
155  /**
156   * This method sets the ending index of this field to the specified value.
157   *
158   * @param end The new ending index.
159   */
160  public void setEndIndex (int end)
161  {
162    this.end = end;
163  }
164
165  /**
166   * This method tests this object for equality against the specified object.
167   * The objects will be considered equal if and only if:
168   * <p>
169   * <ul>
170   * <li>The specified object is not <code>null</code>.
171   * <li>The specified object has the same class as this object.
172   * <li>The specified object has the same field identifier, field attribute
173   * and beginning and ending index as this object.
174   * </ul>
175   *
176   * @param obj The object to test for equality to this object.
177   *
178   * @return <code>true</code> if the specified object is equal to
179   * this object, <code>false</code> otherwise.
180   */
181  public boolean equals (Object obj)
182  {
183    if (this == obj)
184      return true;
185
186    if (obj == null || obj.getClass() != this.getClass())
187      return false;
188
189    FieldPosition fp = (FieldPosition) obj;
190    return (field_id == fp.field_id
191            && (field_attribute == fp.field_attribute
192                || (field_attribute != null
193                    && field_attribute.equals(fp.field_attribute)))
194            && begin == fp.begin
195            && end == fp.end);
196  }
197
198
199  /**
200   * This method returns a hash value for this object
201   *
202   * @return A hash value for this object.
203   */
204  public int hashCode ()
205  {
206    int hash = 5;
207
208    hash = 31 * hash + field_id;
209    hash = 31 * hash + begin;
210    hash = 31 * hash + end;
211    hash = 31 * hash +
212      (null == field_attribute ? 0 : field_attribute.hashCode());
213
214    return hash;
215  }
216
217  /**
218   * This method returns a <code>String</code> representation of this
219   * object.
220   *
221   * @return A <code>String</code> representation of this object.
222   */
223  public String toString ()
224  {
225    return (getClass ().getName ()
226            + "[field=" + getField ()
227            + ",attribute=" + getFieldAttribute ()
228            + ",beginIndex=" + getBeginIndex ()
229            + ",endIndex=" + getEndIndex ()
230            + "]");
231  }
232}