Path: blob/master/src/java.base/share/classes/java/text/FieldPosition.java
41152 views
/*1* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* (C) Copyright Taligent, Inc. 1996 - All Rights Reserved27* (C) Copyright IBM Corp. 1996 - All Rights Reserved28*29* The original version of this source code and documentation is copyrighted30* and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These31* materials are provided under terms of a License Agreement between Taligent32* and Sun. This technology is protected by multiple US and International33* patents. This notice and attribution to Taligent may not be removed.34* Taligent is a registered trademark of Taligent, Inc.35*36*/3738package java.text;3940/**41* {@code FieldPosition} is a simple class used by {@code Format}42* and its subclasses to identify fields in formatted output. Fields can43* be identified in two ways:44* <ul>45* <li>By an integer constant, whose names typically end with46* {@code _FIELD}. The constants are defined in the various47* subclasses of {@code Format}.48* <li>By a {@code Format.Field} constant, see {@code ERA_FIELD}49* and its friends in {@code DateFormat} for an example.50* </ul>51* <p>52* {@code FieldPosition} keeps track of the position of the53* field within the formatted output with two indices: the index54* of the first character of the field and the index of the last55* character of the field.56*57* <p>58* One version of the {@code format} method in the various59* {@code Format} classes requires a {@code FieldPosition}60* object as an argument. You use this {@code format} method61* to perform partial formatting or to get information about the62* formatted output (such as the position of a field).63*64* <p>65* If you are interested in the positions of all attributes in the66* formatted string use the {@code Format} method67* {@code formatToCharacterIterator}.68*69* @author Mark Davis70* @since 1.171* @see java.text.Format72*/73public class FieldPosition {7475/**76* Input: Desired field to determine start and end offsets for.77* The meaning depends on the subclass of Format.78*/79int field = 0;8081/**82* Output: End offset of field in text.83* If the field does not occur in the text, 0 is returned.84*/85int endIndex = 0;8687/**88* Output: Start offset of field in text.89* If the field does not occur in the text, 0 is returned.90*/91int beginIndex = 0;9293/**94* Desired field this FieldPosition is for.95*/96private Format.Field attribute;9798/**99* Creates a FieldPosition object for the given field. Fields are100* identified by constants, whose names typically end with _FIELD,101* in the various subclasses of Format.102*103* @param field the field identifier104* @see java.text.NumberFormat#INTEGER_FIELD105* @see java.text.NumberFormat#FRACTION_FIELD106* @see java.text.DateFormat#YEAR_FIELD107* @see java.text.DateFormat#MONTH_FIELD108*/109public FieldPosition(int field) {110this.field = field;111}112113/**114* Creates a FieldPosition object for the given field constant. Fields are115* identified by constants defined in the various {@code Format}116* subclasses. This is equivalent to calling117* {@code new FieldPosition(attribute, -1)}.118*119* @param attribute Format.Field constant identifying a field120* @since 1.4121*/122public FieldPosition(Format.Field attribute) {123this(attribute, -1);124}125126/**127* Creates a {@code FieldPosition} object for the given field.128* The field is identified by an attribute constant from one of the129* {@code Field} subclasses as well as an integer field ID130* defined by the {@code Format} subclasses. {@code Format}131* subclasses that are aware of {@code Field} should give precedence132* to {@code attribute} and ignore {@code fieldID} if133* {@code attribute} is not null. However, older {@code Format}134* subclasses may not be aware of {@code Field} and rely on135* {@code fieldID}. If the field has no corresponding integer136* constant, {@code fieldID} should be -1.137*138* @param attribute Format.Field constant identifying a field139* @param fieldID integer constant identifying a field140* @since 1.4141*/142public FieldPosition(Format.Field attribute, int fieldID) {143this.attribute = attribute;144this.field = fieldID;145}146147/**148* Returns the field identifier as an attribute constant149* from one of the {@code Field} subclasses. May return null if150* the field is specified only by an integer field ID.151*152* @return Identifier for the field153* @since 1.4154*/155public Format.Field getFieldAttribute() {156return attribute;157}158159/**160* Retrieves the field identifier.161*162* @return the field identifier163*/164public int getField() {165return field;166}167168/**169* Retrieves the index of the first character in the requested field.170*171* @return the begin index172*/173public int getBeginIndex() {174return beginIndex;175}176177/**178* Retrieves the index of the character following the last character in the179* requested field.180*181* @return the end index182*/183public int getEndIndex() {184return endIndex;185}186187/**188* Sets the begin index. For use by subclasses of Format.189*190* @param bi the begin index191* @since 1.2192*/193public void setBeginIndex(int bi) {194beginIndex = bi;195}196197/**198* Sets the end index. For use by subclasses of Format.199*200* @param ei the end index201* @since 1.2202*/203public void setEndIndex(int ei) {204endIndex = ei;205}206207/**208* Returns a {@code Format.FieldDelegate} instance that is associated209* with the FieldPosition. When the delegate is notified of the same210* field the FieldPosition is associated with, the begin/end will be211* adjusted.212*/213Format.FieldDelegate getFieldDelegate() {214return new Delegate();215}216217/**218* Overrides equals219*/220public boolean equals(Object obj)221{222if (obj == null) return false;223if (!(obj instanceof FieldPosition other))224return false;225if (attribute == null) {226if (other.attribute != null) {227return false;228}229}230else if (!attribute.equals(other.attribute)) {231return false;232}233return (beginIndex == other.beginIndex234&& endIndex == other.endIndex235&& field == other.field);236}237238/**239* Returns a hash code for this FieldPosition.240* @return a hash code value for this object241*/242public int hashCode() {243return (field << 24) | (beginIndex << 16) | endIndex;244}245246/**247* Return a string representation of this FieldPosition.248* @return a string representation of this object249*/250public String toString() {251return getClass().getName() +252"[field=" + field + ",attribute=" + attribute +253",beginIndex=" + beginIndex +254",endIndex=" + endIndex + ']';255}256257258/**259* Return true if the receiver wants a {@code Format.Field} value and260* {@code attribute} is equal to it.261*/262private boolean matchesField(Format.Field attribute) {263if (this.attribute != null) {264return this.attribute.equals(attribute);265}266return false;267}268269/**270* Return true if the receiver wants a {@code Format.Field} value and271* {@code attribute} is equal to it, or true if the receiver272* represents an inteter constant and {@code field} equals it.273*/274private boolean matchesField(Format.Field attribute, int field) {275if (this.attribute != null) {276return this.attribute.equals(attribute);277}278return (field == this.field);279}280281282/**283* An implementation of FieldDelegate that will adjust the begin/end284* of the FieldPosition if the arguments match the field of285* the FieldPosition.286*/287private class Delegate implements Format.FieldDelegate {288/**289* Indicates whether the field has been encountered before. If this290* is true, and {@code formatted} is invoked, the begin/end291* are not updated.292*/293private boolean encounteredField;294295public void formatted(Format.Field attr, Object value, int start,296int end, StringBuffer buffer) {297if (!encounteredField && matchesField(attr)) {298setBeginIndex(start);299setEndIndex(end);300encounteredField = (start != end);301}302}303304public void formatted(int fieldID, Format.Field attr, Object value,305int start, int end, StringBuffer buffer) {306if (!encounteredField && matchesField(attr, fieldID)) {307setBeginIndex(start);308setEndIndex(end);309encounteredField = (start != end);310}311}312}313}314315316