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