Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/text/FieldPosition.java
38829 views
/*1* Copyright (c) 1996, 2013, 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</code> is a simple class used by <code>Format</code>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</code>. The constants are defined in the various47* subclasses of <code>Format</code>.48* <li>By a <code>Format.Field</code> constant, see <code>ERA_FIELD</code>49* and its friends in <code>DateFormat</code> for an example.50* </ul>51* <p>52* <code>FieldPosition</code> 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</code> method in the various59* <code>Format</code> classes requires a <code>FieldPosition</code>60* object as an argument. You use this <code>format</code> 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</code> method67* <code>formatToCharacterIterator</code>.68*69* @author Mark Davis70* @see java.text.Format71*/72public class FieldPosition {7374/**75* Input: Desired field to determine start and end offsets for.76* The meaning depends on the subclass of Format.77*/78int field = 0;7980/**81* Output: End offset of field in text.82* If the field does not occur in the text, 0 is returned.83*/84int endIndex = 0;8586/**87* Output: Start offset of field in text.88* If the field does not occur in the text, 0 is returned.89*/90int beginIndex = 0;9192/**93* Desired field this FieldPosition is for.94*/95private Format.Field attribute;9697/**98* Creates a FieldPosition object for the given field. Fields are99* identified by constants, whose names typically end with _FIELD,100* in the various subclasses of Format.101*102* @param field the field identifier103* @see java.text.NumberFormat#INTEGER_FIELD104* @see java.text.NumberFormat#FRACTION_FIELD105* @see java.text.DateFormat#YEAR_FIELD106* @see java.text.DateFormat#MONTH_FIELD107*/108public FieldPosition(int field) {109this.field = field;110}111112/**113* Creates a FieldPosition object for the given field constant. Fields are114* identified by constants defined in the various <code>Format</code>115* subclasses. This is equivalent to calling116* <code>new FieldPosition(attribute, -1)</code>.117*118* @param attribute Format.Field constant identifying a field119* @since 1.4120*/121public FieldPosition(Format.Field attribute) {122this(attribute, -1);123}124125/**126* Creates a <code>FieldPosition</code> object for the given field.127* The field is identified by an attribute constant from one of the128* <code>Field</code> subclasses as well as an integer field ID129* defined by the <code>Format</code> subclasses. <code>Format</code>130* subclasses that are aware of <code>Field</code> should give precedence131* to <code>attribute</code> and ignore <code>fieldID</code> if132* <code>attribute</code> is not null. However, older <code>Format</code>133* subclasses may not be aware of <code>Field</code> and rely on134* <code>fieldID</code>. If the field has no corresponding integer135* constant, <code>fieldID</code> should be -1.136*137* @param attribute Format.Field constant identifying a field138* @param fieldID integer constant identifying a field139* @since 1.4140*/141public FieldPosition(Format.Field attribute, int fieldID) {142this.attribute = attribute;143this.field = fieldID;144}145146/**147* Returns the field identifier as an attribute constant148* from one of the <code>Field</code> subclasses. May return null if149* the field is specified only by an integer field ID.150*151* @return Identifier for the field152* @since 1.4153*/154public Format.Field getFieldAttribute() {155return attribute;156}157158/**159* Retrieves the field identifier.160*161* @return the field identifier162*/163public int getField() {164return field;165}166167/**168* Retrieves the index of the first character in the requested field.169*170* @return the begin index171*/172public int getBeginIndex() {173return beginIndex;174}175176/**177* Retrieves the index of the character following the last character in the178* requested field.179*180* @return the end index181*/182public int getEndIndex() {183return endIndex;184}185186/**187* Sets the begin index. For use by subclasses of Format.188*189* @param bi the begin index190* @since 1.2191*/192public void setBeginIndex(int bi) {193beginIndex = bi;194}195196/**197* Sets the end index. For use by subclasses of Format.198*199* @param ei the end index200* @since 1.2201*/202public void setEndIndex(int ei) {203endIndex = ei;204}205206/**207* Returns a <code>Format.FieldDelegate</code> instance that is associated208* with the FieldPosition. When the delegate is notified of the same209* field the FieldPosition is associated with, the begin/end will be210* adjusted.211*/212Format.FieldDelegate getFieldDelegate() {213return new Delegate();214}215216/**217* Overrides equals218*/219public boolean equals(Object obj)220{221if (obj == null) return false;222if (!(obj instanceof FieldPosition))223return false;224FieldPosition other = (FieldPosition) obj;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</code> value and260* <code>attribute</code> 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</code> value and271* <code>attribute</code> is equal to it, or true if the receiver272* represents an inteter constant and <code>field</code> 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</code> 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