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.
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.
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).
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.
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
27 * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
28 * (C) Copyright IBM Corp. 1996 - All Rights Reserved
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.
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:
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.
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.
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).
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>.
71 * @see java.text.Format
73 public class FieldPosition {
76 * Input: Desired field to determine start and end offsets for.
77 * The meaning depends on the subclass of Format.
82 * Output: End offset of field in text.
83 * If the field does not occur in the text, 0 is returned.
88 * Output: Start offset of field in text.
89 * If the field does not occur in the text, 0 is returned.
94 * Desired field this FieldPosition is for.
96 private Format.Field attribute;
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.
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
108 public FieldPosition(int field) {
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>.
118 * @param attribute Format.Field constant identifying a field
121 public FieldPosition(Format.Field attribute) {
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.
137 * @param attribute Format.Field constant identifying a field
138 * @param fieldID integer constantce identifying a field
141 public FieldPosition(Format.Field attribute, int fieldID) {
142 this.attribute = attribute;
143 this.field = fieldID;
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.
151 * @return Identifier for the field
154 public Format.Field getFieldAttribute() {
159 * Retrieves the field identifier.
161 public int getField() {
166 * Retrieves the index of the first character in the requested field.
168 public int getBeginIndex() {
173 * Retrieves the index of the character following the last character in the
176 public int getEndIndex() {
181 * Sets the begin index. For use by subclasses of Format.
184 public void setBeginIndex(int bi) {
189 * Sets the end index. For use by subclasses of Format.
192 public void setEndIndex(int ei) {
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
202 Format.FieldDelegate getFieldDelegate() {
203 return new Delegate();
209 public boolean equals(Object obj)
211 if (obj == null) return false;
212 if (!(obj instanceof FieldPosition))
214 FieldPosition other = (FieldPosition) obj;
215 if (attribute == null) {
216 if (other.attribute != null) {
220 else if (!attribute.equals(other.attribute)) {
223 return (beginIndex == other.beginIndex
224 && endIndex == other.endIndex
225 && field == other.field);
229 * Returns a hash code for this FieldPosition.
230 * @return a hash code value for this object
232 public int hashCode() {
233 return (field << 24) | (beginIndex << 16) | endIndex;
237 * Return a string representation of this FieldPosition.
238 * @return a string representation of this object
240 public String toString() {
241 return getClass().getName() +
242 "[field=" + field + ",attribute=" + attribute +
243 ",beginIndex=" + beginIndex +
244 ",endIndex=" + endIndex + ']';
249 * Return true if the receiver wants a <code>Format.Field</code> value and
250 * <code>attribute</code> is equal to it.
252 private boolean matchesField(Format.Field attribute) {
253 if (this.attribute != null) {
254 return this.attribute.equals(attribute);
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.
264 private boolean matchesField(Format.Field attribute, int field) {
265 if (this.attribute != null) {
266 return this.attribute.equals(attribute);
268 return (field == this.field);
273 * An implementation of FieldDelegate that will adjust the begin/end
274 * of the FieldPosition if the arguments match the field of
277 private class Delegate implements Format.FieldDelegate {
279 * Indicates whether the field has been encountered before. If this
280 * is true, and <code>formatted</code> is invoked, the begin/end
283 private boolean encounteredField;
285 public void formatted(Format.Field attr, Object value, int start,
286 int end, StringBuffer buffer) {
287 if (!encounteredField && matchesField(attr)) {
288 setBeginIndex(start);
290 encounteredField = (start != end);
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);
299 encounteredField = (start != end);