001: /*
002: *
003: * @(#)FieldPosition.java 1.23 06/10/10
004: *
005: * Portions Copyright 2000-2006 Sun Microsystems, Inc. All Rights
006: * Reserved. Use is subject to license terms.
007: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
008: *
009: * This program is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU General Public License version
011: * 2 only, as published by the Free Software Foundation.
012: *
013: * This program is distributed in the hope that it will be useful, but
014: * WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License version 2 for more details (a copy is
017: * included at /legal/license.txt).
018: *
019: * You should have received a copy of the GNU General Public License
020: * version 2 along with this work; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022: * 02110-1301 USA
023: *
024: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
025: * Clara, CA 95054 or visit www.sun.com if you need additional
026: * information or have any questions.
027: */
028:
029: /*
030: * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
031: * (C) Copyright IBM Corp. 1996 - All Rights Reserved
032: *
033: * The original version of this source code and documentation is copyrighted
034: * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
035: * materials are provided under terms of a License Agreement between Taligent
036: * and Sun. This technology is protected by multiple US and International
037: * patents. This notice and attribution to Taligent may not be removed.
038: * Taligent is a registered trademark of Taligent, Inc.
039: *
040: */
041:
042: package java.text;
043:
044: /**
045: * <code>FieldPosition</code> is a simple class used by <code>Format</code>
046: * and its subclasses to identify fields in formatted output. Fields can
047: * be identified in two ways:
048: * <ul>
049: * <li>By an integer constant, whose names typically end with
050: * <code>_FIELD</code>. The constants are defined in the various
051: * subclasses of <code>Format</code>.
052: * <li>By a <code>Format.Field</code> constant, see <code>ERA_FIELD</code>
053: * and its friends in <code>DateFormat</code> for an example.
054: * </ul>
055: * <p>
056: * <code>FieldPosition</code> keeps track of the position of the
057: * field within the formatted output with two indices: the index
058: * of the first character of the field and the index of the last
059: * character of the field.
060: *
061: * <p>
062: * One version of the <code>format</code> method in the various
063: * <code>Format</code> classes requires a <code>FieldPosition</code>
064: * object as an argument. You use this <code>format</code> method
065: * to perform partial formatting or to get information about the
066: * formatted output (such as the position of a field).
067: *
068: * <p>
069: * If you are interested in the positions of all attributes in the
070: * formatted string use the <code>Format</code> method
071: * <code>formatToCharacterIterator</code>.
072: *
073: * @version 1.23 10/10/06
074: * @author Mark Davis
075: * @see java.text.Format
076: */
077: public class FieldPosition {
078:
079: /**
080: * Input: Desired field to determine start and end offsets for.
081: * The meaning depends on the subclass of Format.
082: */
083: int field = 0;
084:
085: /**
086: * Output: End offset of field in text.
087: * If the field does not occur in the text, 0 is returned.
088: */
089: int endIndex = 0;
090:
091: /**
092: * Output: Start offset of field in text.
093: * If the field does not occur in the text, 0 is returned.
094: */
095: int beginIndex = 0;
096:
097: /**
098: * Desired field this FieldPosition is for.
099: */
100: private Format.Field attribute;
101:
102: /**
103: * Creates a FieldPosition object for the given field. Fields are
104: * identified by constants, whose names typically end with _FIELD,
105: * in the various subclasses of Format.
106: *
107: * @see java.text.NumberFormat#INTEGER_FIELD
108: * @see java.text.NumberFormat#FRACTION_FIELD
109: * @see java.text.DateFormat#YEAR_FIELD
110: * @see java.text.DateFormat#MONTH_FIELD
111: */
112: public FieldPosition(int field) {
113: this .field = field;
114: }
115:
116: /**
117: * Creates a FieldPosition object for the given field constant. Fields are
118: * identified by constants defined in the various <code>Format</code>
119: * subclasses. This is equivalent to calling
120: * <code>new FieldPosition(attribute, -1)</code>.
121: *
122: * @param attribute Format.Field constant identifying a field
123: * @since 1.4
124: */
125: public FieldPosition(Format.Field attribute) {
126: this (attribute, -1);
127: }
128:
129: /**
130: * Creates a <code>FieldPosition</code> object for the given field.
131: * The field is identified by an attribute constant from one of the
132: * <code>Field</code> subclasses as well as an integer field ID
133: * defined by the <code>Format</code> subclasses. <code>Format</code>
134: * subclasses that are aware of <code>Field</code> should give precedence
135: * to <code>attribute</code> and ignore <code>fieldID</code> if
136: * <code>attribute</code> is not null. However, older <code>Format</code>
137: * subclasses may not be aware of <code>Field</code> and rely on
138: * <code>fieldID</code>. If the field has no corresponding integer
139: * constant, <code>fieldID</code> should be -1.
140: *
141: * @param attribute Format.Field constant identifying a field
142: * @param fieldID integer constantce identifying a field
143: * @since 1.4
144: */
145: public FieldPosition(Format.Field attribute, int fieldID) {
146: this .attribute = attribute;
147: this .field = fieldID;
148: }
149:
150: /**
151: * Returns the field identifier as an attribute constant
152: * from one of the <code>Field</code> subclasses. May return null if
153: * the field is specified only by an integer field ID.
154: *
155: * @return Identifier for the field
156: * @since 1.4
157: */
158: public Format.Field getFieldAttribute() {
159: return attribute;
160: }
161:
162: /**
163: * Retrieves the field identifier.
164: */
165: public int getField() {
166: return field;
167: }
168:
169: /**
170: * Retrieves the index of the first character in the requested field.
171: */
172: public int getBeginIndex() {
173: return beginIndex;
174: }
175:
176: /**
177: * Retrieves the index of the character following the last character in the
178: * requested field.
179: */
180: public int getEndIndex() {
181: return endIndex;
182: }
183:
184: /**
185: * Sets the begin index. For use by subclasses of Format.
186: * @since 1.2
187: */
188: public void setBeginIndex(int bi) {
189: beginIndex = bi;
190: }
191:
192: /**
193: * Sets the end index. For use by subclasses of Format.
194: * @since 1.2
195: */
196: public void setEndIndex(int ei) {
197: endIndex = ei;
198: }
199:
200: /**
201: * Returns a <code>Format.FieldDelegate</code> instance that is associated
202: * with the FieldPosition. When the delegate is notified of the same
203: * field the FieldPosition is associated with, the begin/end will be
204: * adjusted.
205: */
206: Format.FieldDelegate getFieldDelegate() {
207: return new Delegate();
208: }
209:
210: /**
211: * Overrides equals
212: */
213: public boolean equals(Object obj) {
214: if (obj == null)
215: return false;
216: if (!(obj instanceof FieldPosition))
217: return false;
218: FieldPosition other = (FieldPosition) obj;
219: if (attribute == null) {
220: if (other.attribute != null) {
221: return false;
222: }
223: } else if (!attribute.equals(other.attribute)) {
224: return false;
225: }
226: return (beginIndex == other.beginIndex
227: && endIndex == other.endIndex && field == other.field);
228: }
229:
230: /**
231: * Returns a hash code for this FieldPosition.
232: * @return a hash code value for this object
233: */
234: public int hashCode() {
235: return (field << 24) | (beginIndex << 16) | endIndex;
236: }
237:
238: /**
239: * Return a string representation of this FieldPosition.
240: * @return a string representation of this object
241: */
242: public String toString() {
243: return getClass().getName() + "[field=" + field + ",attribute="
244: + attribute + ",beginIndex=" + beginIndex
245: + ",endIndex=" + endIndex + ']';
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: */
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: */
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: * An implementation of FieldDelegate that will adjust the begin/end
273: * of the FieldPosition if the arguments match the field of
274: * the FieldPosition.
275: */
276: private class Delegate implements Format.FieldDelegate {
277: /**
278: * Indicates whether the field has been encountered before. If this
279: * is true, and <code>formatted</code> is invoked, the begin/end
280: * are not updated.
281: */
282: private boolean encounteredField;
283:
284: public void formatted(Format.Field attr, Object value,
285: int start, int end, StringBuffer buffer) {
286: if (!encounteredField && matchesField(attr)) {
287: setBeginIndex(start);
288: setEndIndex(end);
289: encounteredField = (start != end);
290: }
291: }
292:
293: public void formatted(int fieldID, Format.Field attr,
294: Object value, int start, int end, StringBuffer buffer) {
295: if (!encounteredField && matchesField(attr, fieldID)) {
296: setBeginIndex(start);
297: setEndIndex(end);
298: encounteredField = (start != end);
299: }
300: }
301: }
302: }
|