0N/A/*
2362N/A * Copyright (c) 1996, 2002, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
0N/A * (C) Copyright IBM Corp. 1996 - All Rights Reserved
0N/A *
0N/A * The original version of this source code and documentation is copyrighted
0N/A * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
0N/A * materials are provided under terms of a License Agreement between Taligent
0N/A * and Sun. This technology is protected by multiple US and International
0N/A * patents. This notice and attribution to Taligent may not be removed.
0N/A * Taligent is a registered trademark of Taligent, Inc.
0N/A *
0N/A */
0N/A
0N/Apackage java.text;
0N/A
0N/A/**
0N/A * <code>FieldPosition</code> is a simple class used by <code>Format</code>
0N/A * and its subclasses to identify fields in formatted output. Fields can
0N/A * be identified in two ways:
0N/A * <ul>
0N/A * <li>By an integer constant, whose names typically end with
0N/A * <code>_FIELD</code>. The constants are defined in the various
0N/A * subclasses of <code>Format</code>.
0N/A * <li>By a <code>Format.Field</code> constant, see <code>ERA_FIELD</code>
0N/A * and its friends in <code>DateFormat</code> for an example.
0N/A * </ul>
0N/A * <p>
0N/A * <code>FieldPosition</code> keeps track of the position of the
0N/A * field within the formatted output with two indices: the index
0N/A * of the first character of the field and the index of the last
0N/A * character of the field.
0N/A *
0N/A * <p>
0N/A * One version of the <code>format</code> method in the various
0N/A * <code>Format</code> classes requires a <code>FieldPosition</code>
0N/A * object as an argument. You use this <code>format</code> method
0N/A * to perform partial formatting or to get information about the
0N/A * formatted output (such as the position of a field).
0N/A *
0N/A * <p>
0N/A * If you are interested in the positions of all attributes in the
0N/A * formatted string use the <code>Format</code> method
0N/A * <code>formatToCharacterIterator</code>.
0N/A *
0N/A * @author Mark Davis
0N/A * @see java.text.Format
0N/A */
0N/Apublic class FieldPosition {
0N/A
0N/A /**
0N/A * Input: Desired field to determine start and end offsets for.
0N/A * The meaning depends on the subclass of Format.
0N/A */
0N/A int field = 0;
0N/A
0N/A /**
0N/A * Output: End offset of field in text.
0N/A * If the field does not occur in the text, 0 is returned.
0N/A */
0N/A int endIndex = 0;
0N/A
0N/A /**
0N/A * Output: Start offset of field in text.
0N/A * If the field does not occur in the text, 0 is returned.
0N/A */
0N/A int beginIndex = 0;
0N/A
0N/A /**
0N/A * Desired field this FieldPosition is for.
0N/A */
0N/A private Format.Field attribute;
0N/A
0N/A /**
0N/A * Creates a FieldPosition object for the given field. Fields are
0N/A * identified by constants, whose names typically end with _FIELD,
0N/A * in the various subclasses of Format.
0N/A *
0N/A * @see java.text.NumberFormat#INTEGER_FIELD
0N/A * @see java.text.NumberFormat#FRACTION_FIELD
0N/A * @see java.text.DateFormat#YEAR_FIELD
0N/A * @see java.text.DateFormat#MONTH_FIELD
0N/A */
0N/A public FieldPosition(int field) {
0N/A this.field = field;
0N/A }
0N/A
0N/A /**
0N/A * Creates a FieldPosition object for the given field constant. Fields are
0N/A * identified by constants defined in the various <code>Format</code>
0N/A * subclasses. This is equivalent to calling
0N/A * <code>new FieldPosition(attribute, -1)</code>.
0N/A *
0N/A * @param attribute Format.Field constant identifying a field
0N/A * @since 1.4
0N/A */
0N/A public FieldPosition(Format.Field attribute) {
0N/A this(attribute, -1);
0N/A }
0N/A
0N/A /**
0N/A * Creates a <code>FieldPosition</code> object for the given field.
0N/A * The field is identified by an attribute constant from one of the
0N/A * <code>Field</code> subclasses as well as an integer field ID
0N/A * defined by the <code>Format</code> subclasses. <code>Format</code>
0N/A * subclasses that are aware of <code>Field</code> should give precedence
0N/A * to <code>attribute</code> and ignore <code>fieldID</code> if
0N/A * <code>attribute</code> is not null. However, older <code>Format</code>
0N/A * subclasses may not be aware of <code>Field</code> and rely on
0N/A * <code>fieldID</code>. If the field has no corresponding integer
0N/A * constant, <code>fieldID</code> should be -1.
0N/A *
0N/A * @param attribute Format.Field constant identifying a field
0N/A * @param fieldID integer constantce identifying a field
0N/A * @since 1.4
0N/A */
0N/A public FieldPosition(Format.Field attribute, int fieldID) {
0N/A this.attribute = attribute;
0N/A this.field = fieldID;
0N/A }
0N/A
0N/A /**
0N/A * Returns the field identifier as an attribute constant
0N/A * from one of the <code>Field</code> subclasses. May return null if
0N/A * the field is specified only by an integer field ID.
0N/A *
0N/A * @return Identifier for the field
0N/A * @since 1.4
0N/A */
0N/A public Format.Field getFieldAttribute() {
0N/A return attribute;
0N/A }
0N/A
0N/A /**
0N/A * Retrieves the field identifier.
0N/A */
0N/A public int getField() {
0N/A return field;
0N/A }
0N/A
0N/A /**
0N/A * Retrieves the index of the first character in the requested field.
0N/A */
0N/A public int getBeginIndex() {
0N/A return beginIndex;
0N/A }
0N/A
0N/A /**
0N/A * Retrieves the index of the character following the last character in the
0N/A * requested field.
0N/A */
0N/A public int getEndIndex() {
0N/A return endIndex;
0N/A }
0N/A
0N/A /**
0N/A * Sets the begin index. For use by subclasses of Format.
0N/A * @since 1.2
0N/A */
0N/A public void setBeginIndex(int bi) {
0N/A beginIndex = bi;
0N/A }
0N/A
0N/A /**
0N/A * Sets the end index. For use by subclasses of Format.
0N/A * @since 1.2
0N/A */
0N/A public void setEndIndex(int ei) {
0N/A endIndex = ei;
0N/A }
0N/A
0N/A /**
0N/A * Returns a <code>Format.FieldDelegate</code> instance that is associated
0N/A * with the FieldPosition. When the delegate is notified of the same
0N/A * field the FieldPosition is associated with, the begin/end will be
0N/A * adjusted.
0N/A */
0N/A Format.FieldDelegate getFieldDelegate() {
0N/A return new Delegate();
0N/A }
0N/A
0N/A /**
0N/A * Overrides equals
0N/A */
0N/A public boolean equals(Object obj)
0N/A {
0N/A if (obj == null) return false;
0N/A if (!(obj instanceof FieldPosition))
0N/A return false;
0N/A FieldPosition other = (FieldPosition) obj;
0N/A if (attribute == null) {
0N/A if (other.attribute != null) {
0N/A return false;
0N/A }
0N/A }
0N/A else if (!attribute.equals(other.attribute)) {
0N/A return false;
0N/A }
0N/A return (beginIndex == other.beginIndex
0N/A && endIndex == other.endIndex
0N/A && field == other.field);
0N/A }
0N/A
0N/A /**
0N/A * Returns a hash code for this FieldPosition.
0N/A * @return a hash code value for this object
0N/A */
0N/A public int hashCode() {
0N/A return (field << 24) | (beginIndex << 16) | endIndex;
0N/A }
0N/A
0N/A /**
0N/A * Return a string representation of this FieldPosition.
0N/A * @return a string representation of this object
0N/A */
0N/A public String toString() {
0N/A return getClass().getName() +
0N/A "[field=" + field + ",attribute=" + attribute +
0N/A ",beginIndex=" + beginIndex +
0N/A ",endIndex=" + endIndex + ']';
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Return true if the receiver wants a <code>Format.Field</code> value and
0N/A * <code>attribute</code> is equal to it.
0N/A */
0N/A private boolean matchesField(Format.Field attribute) {
0N/A if (this.attribute != null) {
0N/A return this.attribute.equals(attribute);
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Return true if the receiver wants a <code>Format.Field</code> value and
0N/A * <code>attribute</code> is equal to it, or true if the receiver
0N/A * represents an inteter constant and <code>field</code> equals it.
0N/A */
0N/A private boolean matchesField(Format.Field attribute, int field) {
0N/A if (this.attribute != null) {
0N/A return this.attribute.equals(attribute);
0N/A }
0N/A return (field == this.field);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * An implementation of FieldDelegate that will adjust the begin/end
0N/A * of the FieldPosition if the arguments match the field of
0N/A * the FieldPosition.
0N/A */
0N/A private class Delegate implements Format.FieldDelegate {
0N/A /**
0N/A * Indicates whether the field has been encountered before. If this
0N/A * is true, and <code>formatted</code> is invoked, the begin/end
0N/A * are not updated.
0N/A */
0N/A private boolean encounteredField;
0N/A
0N/A public void formatted(Format.Field attr, Object value, int start,
0N/A int end, StringBuffer buffer) {
0N/A if (!encounteredField && matchesField(attr)) {
0N/A setBeginIndex(start);
0N/A setEndIndex(end);
0N/A encounteredField = (start != end);
0N/A }
0N/A }
0N/A
0N/A public void formatted(int fieldID, Format.Field attr, Object value,
0N/A int start, int end, StringBuffer buffer) {
0N/A if (!encounteredField && matchesField(attr, fieldID)) {
0N/A setBeginIndex(start);
0N/A setEndIndex(end);
0N/A encounteredField = (start != end);
0N/A }
0N/A }
0N/A }
0N/A}