1 /* 2 * Copyright (c) 1996, 2002, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* 27 * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved 28 * (C) Copyright IBM Corp. 1996 - All Rights Reserved 29 * 30 * The original version of this source code and documentation is copyrighted 31 * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These 32 * materials are provided under terms of a License Agreement between Taligent 33 * and Sun. This technology is protected by multiple US and International 34 * patents. This notice and attribution to Taligent may not be removed. 35 * Taligent is a registered trademark of Taligent, Inc. 36 * 37 */ 38 39 package java.text; 40 41 /** 42 * <code>FieldPosition</code> is a simple class used by <code>Format</code> 43 * and its subclasses to identify fields in formatted output. Fields can 44 * be identified in two ways: 45 * <ul> 46 * <li>By an integer constant, whose names typically end with 47 * <code>_FIELD</code>. The constants are defined in the various 48 * subclasses of <code>Format</code>. 49 * <li>By a <code>Format.Field</code> constant, see <code>ERA_FIELD</code> 50 * and its friends in <code>DateFormat</code> for an example. 51 * </ul> 52 * <p> 53 * <code>FieldPosition</code> keeps track of the position of the 54 * field within the formatted output with two indices: the index 55 * of the first character of the field and the index of the last 56 * character of the field. 57 * 58 * <p> 59 * One version of the <code>format</code> method in the various 60 * <code>Format</code> classes requires a <code>FieldPosition</code> 61 * object as an argument. You use this <code>format</code> method 62 * to perform partial formatting or to get information about the 63 * formatted output (such as the position of a field). 64 * 65 * <p> 66 * If you are interested in the positions of all attributes in the 67 * formatted string use the <code>Format</code> method 68 * <code>formatToCharacterIterator</code>. 69 * 70 * @author Mark Davis 71 * @see java.text.Format 72 */ 73 public class FieldPosition { 74 75 /** 76 * Input: Desired field to determine start and end offsets for. 77 * The meaning depends on the subclass of Format. 78 */ 79 int field = 0; 80 81 /** 82 * Output: End offset of field in text. 83 * If the field does not occur in the text, 0 is returned. 84 */ 85 int endIndex = 0; 86 87 /** 88 * Output: Start offset of field in text. 89 * If the field does not occur in the text, 0 is returned. 90 */ 91 int beginIndex = 0; 92 93 /** 94 * Desired field this FieldPosition is for. 95 */ 96 private Format.Field attribute; 97 98 /** 99 * Creates a FieldPosition object for the given field. Fields are 100 * identified by constants, whose names typically end with _FIELD, 101 * in the various subclasses of Format. 102 * 103 * @see java.text.NumberFormat#INTEGER_FIELD 104 * @see java.text.NumberFormat#FRACTION_FIELD 105 * @see java.text.DateFormat#YEAR_FIELD 106 * @see java.text.DateFormat#MONTH_FIELD 107 */ FieldPosition(int field)108 public FieldPosition(int field) { 109 this.field = field; 110 } 111 112 /** 113 * Creates a FieldPosition object for the given field constant. Fields are 114 * identified by constants defined in the various <code>Format</code> 115 * subclasses. This is equivalent to calling 116 * <code>new FieldPosition(attribute, -1)</code>. 117 * 118 * @param attribute Format.Field constant identifying a field 119 * @since 1.4 120 */ FieldPosition(Format.Field attribute)121 public FieldPosition(Format.Field attribute) { 122 this(attribute, -1); 123 } 124 125 /** 126 * Creates a <code>FieldPosition</code> object for the given field. 127 * The field is identified by an attribute constant from one of the 128 * <code>Field</code> subclasses as well as an integer field ID 129 * defined by the <code>Format</code> subclasses. <code>Format</code> 130 * subclasses that are aware of <code>Field</code> should give precedence 131 * to <code>attribute</code> and ignore <code>fieldID</code> if 132 * <code>attribute</code> is not null. However, older <code>Format</code> 133 * subclasses may not be aware of <code>Field</code> and rely on 134 * <code>fieldID</code>. If the field has no corresponding integer 135 * constant, <code>fieldID</code> should be -1. 136 * 137 * @param attribute Format.Field constant identifying a field 138 * @param fieldID integer constantce identifying a field 139 * @since 1.4 140 */ FieldPosition(Format.Field attribute, int fieldID)141 public FieldPosition(Format.Field attribute, int fieldID) { 142 this.attribute = attribute; 143 this.field = fieldID; 144 } 145 146 /** 147 * Returns the field identifier as an attribute constant 148 * from one of the <code>Field</code> subclasses. May return null if 149 * the field is specified only by an integer field ID. 150 * 151 * @return Identifier for the field 152 * @since 1.4 153 */ getFieldAttribute()154 public Format.Field getFieldAttribute() { 155 return attribute; 156 } 157 158 /** 159 * Retrieves the field identifier. 160 */ getField()161 public int getField() { 162 return field; 163 } 164 165 /** 166 * Retrieves the index of the first character in the requested field. 167 */ getBeginIndex()168 public int getBeginIndex() { 169 return beginIndex; 170 } 171 172 /** 173 * Retrieves the index of the character following the last character in the 174 * requested field. 175 */ getEndIndex()176 public int getEndIndex() { 177 return endIndex; 178 } 179 180 /** 181 * Sets the begin index. For use by subclasses of Format. 182 * @since 1.2 183 */ setBeginIndex(int bi)184 public void setBeginIndex(int bi) { 185 beginIndex = bi; 186 } 187 188 /** 189 * Sets the end index. For use by subclasses of Format. 190 * @since 1.2 191 */ setEndIndex(int ei)192 public void setEndIndex(int ei) { 193 endIndex = ei; 194 } 195 196 /** 197 * Returns a <code>Format.FieldDelegate</code> instance that is associated 198 * with the FieldPosition. When the delegate is notified of the same 199 * field the FieldPosition is associated with, the begin/end will be 200 * adjusted. 201 */ getFieldDelegate()202 Format.FieldDelegate getFieldDelegate() { 203 return new Delegate(); 204 } 205 206 /** 207 * Overrides equals 208 */ equals(Object obj)209 public boolean equals(Object obj) 210 { 211 if (obj == null) return false; 212 if (!(obj instanceof FieldPosition)) 213 return false; 214 FieldPosition other = (FieldPosition) obj; 215 if (attribute == null) { 216 if (other.attribute != null) { 217 return false; 218 } 219 } 220 else if (!attribute.equals(other.attribute)) { 221 return false; 222 } 223 return (beginIndex == other.beginIndex 224 && endIndex == other.endIndex 225 && field == other.field); 226 } 227 228 /** 229 * Returns a hash code for this FieldPosition. 230 * @return a hash code value for this object 231 */ hashCode()232 public int hashCode() { 233 return (field << 24) | (beginIndex << 16) | endIndex; 234 } 235 236 /** 237 * Return a string representation of this FieldPosition. 238 * @return a string representation of this object 239 */ toString()240 public String toString() { 241 return getClass().getName() + 242 "[field=" + field + ",attribute=" + attribute + 243 ",beginIndex=" + beginIndex + 244 ",endIndex=" + endIndex + ']'; 245 } 246 247 248 /** 249 * Return true if the receiver wants a <code>Format.Field</code> value and 250 * <code>attribute</code> is equal to it. 251 */ matchesField(Format.Field attribute)252 private boolean matchesField(Format.Field attribute) { 253 if (this.attribute != null) { 254 return this.attribute.equals(attribute); 255 } 256 return false; 257 } 258 259 /** 260 * Return true if the receiver wants a <code>Format.Field</code> value and 261 * <code>attribute</code> is equal to it, or true if the receiver 262 * represents an inteter constant and <code>field</code> equals it. 263 */ matchesField(Format.Field attribute, int field)264 private boolean matchesField(Format.Field attribute, int field) { 265 if (this.attribute != null) { 266 return this.attribute.equals(attribute); 267 } 268 return (field == this.field); 269 } 270 271 272 /** 273 * An implementation of FieldDelegate that will adjust the begin/end 274 * of the FieldPosition if the arguments match the field of 275 * the FieldPosition. 276 */ 277 private class Delegate implements Format.FieldDelegate { 278 /** 279 * Indicates whether the field has been encountered before. If this 280 * is true, and <code>formatted</code> is invoked, the begin/end 281 * are not updated. 282 */ 283 private boolean encounteredField; 284 formatted(Format.Field attr, Object value, int start, int end, StringBuffer buffer)285 public void formatted(Format.Field attr, Object value, int start, 286 int end, StringBuffer buffer) { 287 if (!encounteredField && matchesField(attr)) { 288 setBeginIndex(start); 289 setEndIndex(end); 290 encounteredField = (start != end); 291 } 292 } 293 formatted(int fieldID, Format.Field attr, Object value, int start, int end, StringBuffer buffer)294 public void formatted(int fieldID, Format.Field attr, Object value, 295 int start, int end, StringBuffer buffer) { 296 if (!encounteredField && matchesField(attr, fieldID)) { 297 setBeginIndex(start); 298 setEndIndex(end); 299 encounteredField = (start != end); 300 } 301 } 302 } 303 } 304