src/java.base/share/classes/java/text/FieldPosition.java
changeset 58288 48e480e56aad
parent 47216 71c04702a3d5
equal deleted inserted replaced
58287:a7f16447085e 58288:48e480e56aad
     1 /*
     1 /*
     2  * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    37  */
    37  */
    38 
    38 
    39 package java.text;
    39 package java.text;
    40 
    40 
    41 /**
    41 /**
    42  * <code>FieldPosition</code> is a simple class used by <code>Format</code>
    42  * {@code FieldPosition} is a simple class used by {@code Format}
    43  * and its subclasses to identify fields in formatted output. Fields can
    43  * and its subclasses to identify fields in formatted output. Fields can
    44  * be identified in two ways:
    44  * be identified in two ways:
    45  * <ul>
    45  * <ul>
    46  *  <li>By an integer constant, whose names typically end with
    46  *  <li>By an integer constant, whose names typically end with
    47  *      <code>_FIELD</code>. The constants are defined in the various
    47  *      {@code _FIELD}. The constants are defined in the various
    48  *      subclasses of <code>Format</code>.
    48  *      subclasses of {@code Format}.
    49  *  <li>By a <code>Format.Field</code> constant, see <code>ERA_FIELD</code>
    49  *  <li>By a {@code Format.Field} constant, see {@code ERA_FIELD}
    50  *      and its friends in <code>DateFormat</code> for an example.
    50  *      and its friends in {@code DateFormat} for an example.
    51  * </ul>
    51  * </ul>
    52  * <p>
    52  * <p>
    53  * <code>FieldPosition</code> keeps track of the position of the
    53  * {@code FieldPosition} keeps track of the position of the
    54  * field within the formatted output with two indices: the index
    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
    55  * of the first character of the field and the index of the last
    56  * character of the field.
    56  * character of the field.
    57  *
    57  *
    58  * <p>
    58  * <p>
    59  * One version of the <code>format</code> method in the various
    59  * One version of the {@code format} method in the various
    60  * <code>Format</code> classes requires a <code>FieldPosition</code>
    60  * {@code Format} classes requires a {@code FieldPosition}
    61  * object as an argument. You use this <code>format</code> method
    61  * object as an argument. You use this {@code format} method
    62  * to perform partial formatting or to get information about the
    62  * to perform partial formatting or to get information about the
    63  * formatted output (such as the position of a field).
    63  * formatted output (such as the position of a field).
    64  *
    64  *
    65  * <p>
    65  * <p>
    66  * If you are interested in the positions of all attributes in the
    66  * If you are interested in the positions of all attributes in the
    67  * formatted string use the <code>Format</code> method
    67  * formatted string use the {@code Format} method
    68  * <code>formatToCharacterIterator</code>.
    68  * {@code formatToCharacterIterator}.
    69  *
    69  *
    70  * @author      Mark Davis
    70  * @author      Mark Davis
    71  * @since 1.1
    71  * @since 1.1
    72  * @see         java.text.Format
    72  * @see         java.text.Format
    73  */
    73  */
   111         this.field = field;
   111         this.field = field;
   112     }
   112     }
   113 
   113 
   114     /**
   114     /**
   115      * Creates a FieldPosition object for the given field constant. Fields are
   115      * Creates a FieldPosition object for the given field constant. Fields are
   116      * identified by constants defined in the various <code>Format</code>
   116      * identified by constants defined in the various {@code Format}
   117      * subclasses. This is equivalent to calling
   117      * subclasses. This is equivalent to calling
   118      * <code>new FieldPosition(attribute, -1)</code>.
   118      * {@code new FieldPosition(attribute, -1)}.
   119      *
   119      *
   120      * @param attribute Format.Field constant identifying a field
   120      * @param attribute Format.Field constant identifying a field
   121      * @since 1.4
   121      * @since 1.4
   122      */
   122      */
   123     public FieldPosition(Format.Field attribute) {
   123     public FieldPosition(Format.Field attribute) {
   124         this(attribute, -1);
   124         this(attribute, -1);
   125     }
   125     }
   126 
   126 
   127     /**
   127     /**
   128      * Creates a <code>FieldPosition</code> object for the given field.
   128      * Creates a {@code FieldPosition} object for the given field.
   129      * The field is identified by an attribute constant from one of the
   129      * The field is identified by an attribute constant from one of the
   130      * <code>Field</code> subclasses as well as an integer field ID
   130      * {@code Field} subclasses as well as an integer field ID
   131      * defined by the <code>Format</code> subclasses. <code>Format</code>
   131      * defined by the {@code Format} subclasses. {@code Format}
   132      * subclasses that are aware of <code>Field</code> should give precedence
   132      * subclasses that are aware of {@code Field} should give precedence
   133      * to <code>attribute</code> and ignore <code>fieldID</code> if
   133      * to {@code attribute} and ignore {@code fieldID} if
   134      * <code>attribute</code> is not null. However, older <code>Format</code>
   134      * {@code attribute} is not null. However, older {@code Format}
   135      * subclasses may not be aware of <code>Field</code> and rely on
   135      * subclasses may not be aware of {@code Field} and rely on
   136      * <code>fieldID</code>. If the field has no corresponding integer
   136      * {@code fieldID}. If the field has no corresponding integer
   137      * constant, <code>fieldID</code> should be -1.
   137      * constant, {@code fieldID} should be -1.
   138      *
   138      *
   139      * @param attribute Format.Field constant identifying a field
   139      * @param attribute Format.Field constant identifying a field
   140      * @param fieldID integer constant identifying a field
   140      * @param fieldID integer constant identifying a field
   141      * @since 1.4
   141      * @since 1.4
   142      */
   142      */
   145         this.field = fieldID;
   145         this.field = fieldID;
   146     }
   146     }
   147 
   147 
   148     /**
   148     /**
   149      * Returns the field identifier as an attribute constant
   149      * Returns the field identifier as an attribute constant
   150      * from one of the <code>Field</code> subclasses. May return null if
   150      * from one of the {@code Field} subclasses. May return null if
   151      * the field is specified only by an integer field ID.
   151      * the field is specified only by an integer field ID.
   152      *
   152      *
   153      * @return Identifier for the field
   153      * @return Identifier for the field
   154      * @since 1.4
   154      * @since 1.4
   155      */
   155      */
   204     public void setEndIndex(int ei) {
   204     public void setEndIndex(int ei) {
   205         endIndex = ei;
   205         endIndex = ei;
   206     }
   206     }
   207 
   207 
   208     /**
   208     /**
   209      * Returns a <code>Format.FieldDelegate</code> instance that is associated
   209      * Returns a {@code Format.FieldDelegate} instance that is associated
   210      * with the FieldPosition. When the delegate is notified of the same
   210      * with the FieldPosition. When the delegate is notified of the same
   211      * field the FieldPosition is associated with, the begin/end will be
   211      * field the FieldPosition is associated with, the begin/end will be
   212      * adjusted.
   212      * adjusted.
   213      */
   213      */
   214     Format.FieldDelegate getFieldDelegate() {
   214     Format.FieldDelegate getFieldDelegate() {
   256             ",endIndex=" + endIndex + ']';
   256             ",endIndex=" + endIndex + ']';
   257     }
   257     }
   258 
   258 
   259 
   259 
   260     /**
   260     /**
   261      * Return true if the receiver wants a <code>Format.Field</code> value and
   261      * Return true if the receiver wants a {@code Format.Field} value and
   262      * <code>attribute</code> is equal to it.
   262      * {@code attribute} is equal to it.
   263      */
   263      */
   264     private boolean matchesField(Format.Field attribute) {
   264     private boolean matchesField(Format.Field attribute) {
   265         if (this.attribute != null) {
   265         if (this.attribute != null) {
   266             return this.attribute.equals(attribute);
   266             return this.attribute.equals(attribute);
   267         }
   267         }
   268         return false;
   268         return false;
   269     }
   269     }
   270 
   270 
   271     /**
   271     /**
   272      * Return true if the receiver wants a <code>Format.Field</code> value and
   272      * Return true if the receiver wants a {@code Format.Field} value and
   273      * <code>attribute</code> is equal to it, or true if the receiver
   273      * {@code attribute} is equal to it, or true if the receiver
   274      * represents an inteter constant and <code>field</code> equals it.
   274      * represents an inteter constant and {@code field} equals it.
   275      */
   275      */
   276     private boolean matchesField(Format.Field attribute, int field) {
   276     private boolean matchesField(Format.Field attribute, int field) {
   277         if (this.attribute != null) {
   277         if (this.attribute != null) {
   278             return this.attribute.equals(attribute);
   278             return this.attribute.equals(attribute);
   279         }
   279         }
   287      * the FieldPosition.
   287      * the FieldPosition.
   288      */
   288      */
   289     private class Delegate implements Format.FieldDelegate {
   289     private class Delegate implements Format.FieldDelegate {
   290         /**
   290         /**
   291          * Indicates whether the field has been  encountered before. If this
   291          * Indicates whether the field has been  encountered before. If this
   292          * is true, and <code>formatted</code> is invoked, the begin/end
   292          * is true, and {@code formatted} is invoked, the begin/end
   293          * are not updated.
   293          * are not updated.
   294          */
   294          */
   295         private boolean encounteredField;
   295         private boolean encounteredField;
   296 
   296 
   297         public void formatted(Format.Field attr, Object value, int start,
   297         public void formatted(Format.Field attr, Object value, int start,