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