Source for java.text.FieldPosition

   1: /* FieldPosition.java -- Keeps track of field positions while formatting
   2:    Copyright (C) 1998, 1999, 2001, 2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package java.text;
  40: 
  41: /**
  42:  * This class is used by the java.text formatting classes to track
  43:  * field positions.  A field position is defined by an identifier value
  44:  * and begin and end index positions.  The formatting classes in java.text
  45:  * typically define constant values for the field identifiers.
  46:  *
  47:  * @author Aaron M. Renn (arenn@urbanophile.com)
  48:  * @author Per Bothner (bothner@cygnus.com)
  49:  */
  50: public class FieldPosition
  51: {
  52:   /**
  53:    * This is the field identifier value.
  54:    */
  55:   private int field_id;
  56: 
  57:   /**
  58:    * This is the beginning index of the field.
  59:    */
  60:   private int begin;
  61: 
  62:   /**
  63:    * This is the ending index of the field.
  64:    */
  65:   private int end;
  66: 
  67:   /**
  68:    * This is the field attribute value.
  69:    */
  70:   private Format.Field field_attribute;
  71: 
  72:   /**
  73:    * This method initializes a new instance of <code>FieldPosition</code>
  74:    * to have the specified field attribute. The attribute will be used as
  75:    * an id. It is formally equivalent to calling FieldPosition(field, -1).
  76:    *
  77:    * @param field The field format attribute.
  78:    */
  79:   public FieldPosition (Format.Field field)
  80:   {
  81:     this(field, -1);
  82:   }
  83: 
  84:   /**
  85:    * This method initializes a new instance of <code>FieldPosition</code>
  86:    * to have the specified field attribute. The attribute will be used as
  87:    * an id is non null. The integer field id is only used if the Format.Field
  88:    * attribute is not used by the formatter.
  89:    *
  90:    * @param field The field format attribute.
  91:    * @param field_id The field identifier value.
  92:    */
  93:   public FieldPosition (Format.Field field, int field_id)
  94:   {
  95:     this.field_attribute = field;
  96:     this.field_id = field_id;
  97:   }
  98: 
  99:   /**
 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: }