1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/emul/compact/src/main/java/java/text/FieldPosition.java Thu Oct 03 15:40:35 2013 +0200
1.3 @@ -0,0 +1,303 @@
1.4 +/*
1.5 + * Copyright (c) 1996, 2002, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +/*
1.30 + * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
1.31 + * (C) Copyright IBM Corp. 1996 - All Rights Reserved
1.32 + *
1.33 + * The original version of this source code and documentation is copyrighted
1.34 + * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
1.35 + * materials are provided under terms of a License Agreement between Taligent
1.36 + * and Sun. This technology is protected by multiple US and International
1.37 + * patents. This notice and attribution to Taligent may not be removed.
1.38 + * Taligent is a registered trademark of Taligent, Inc.
1.39 + *
1.40 + */
1.41 +
1.42 +package java.text;
1.43 +
1.44 +/**
1.45 + * <code>FieldPosition</code> is a simple class used by <code>Format</code>
1.46 + * and its subclasses to identify fields in formatted output. Fields can
1.47 + * be identified in two ways:
1.48 + * <ul>
1.49 + * <li>By an integer constant, whose names typically end with
1.50 + * <code>_FIELD</code>. The constants are defined in the various
1.51 + * subclasses of <code>Format</code>.
1.52 + * <li>By a <code>Format.Field</code> constant, see <code>ERA_FIELD</code>
1.53 + * and its friends in <code>DateFormat</code> for an example.
1.54 + * </ul>
1.55 + * <p>
1.56 + * <code>FieldPosition</code> keeps track of the position of the
1.57 + * field within the formatted output with two indices: the index
1.58 + * of the first character of the field and the index of the last
1.59 + * character of the field.
1.60 + *
1.61 + * <p>
1.62 + * One version of the <code>format</code> method in the various
1.63 + * <code>Format</code> classes requires a <code>FieldPosition</code>
1.64 + * object as an argument. You use this <code>format</code> method
1.65 + * to perform partial formatting or to get information about the
1.66 + * formatted output (such as the position of a field).
1.67 + *
1.68 + * <p>
1.69 + * If you are interested in the positions of all attributes in the
1.70 + * formatted string use the <code>Format</code> method
1.71 + * <code>formatToCharacterIterator</code>.
1.72 + *
1.73 + * @author Mark Davis
1.74 + * @see java.text.Format
1.75 + */
1.76 +public class FieldPosition {
1.77 +
1.78 + /**
1.79 + * Input: Desired field to determine start and end offsets for.
1.80 + * The meaning depends on the subclass of Format.
1.81 + */
1.82 + int field = 0;
1.83 +
1.84 + /**
1.85 + * Output: End offset of field in text.
1.86 + * If the field does not occur in the text, 0 is returned.
1.87 + */
1.88 + int endIndex = 0;
1.89 +
1.90 + /**
1.91 + * Output: Start offset of field in text.
1.92 + * If the field does not occur in the text, 0 is returned.
1.93 + */
1.94 + int beginIndex = 0;
1.95 +
1.96 + /**
1.97 + * Desired field this FieldPosition is for.
1.98 + */
1.99 + private Format.Field attribute;
1.100 +
1.101 + /**
1.102 + * Creates a FieldPosition object for the given field. Fields are
1.103 + * identified by constants, whose names typically end with _FIELD,
1.104 + * in the various subclasses of Format.
1.105 + *
1.106 + * @see java.text.NumberFormat#INTEGER_FIELD
1.107 + * @see java.text.NumberFormat#FRACTION_FIELD
1.108 + * @see java.text.DateFormat#YEAR_FIELD
1.109 + * @see java.text.DateFormat#MONTH_FIELD
1.110 + */
1.111 + public FieldPosition(int field) {
1.112 + this.field = field;
1.113 + }
1.114 +
1.115 + /**
1.116 + * Creates a FieldPosition object for the given field constant. Fields are
1.117 + * identified by constants defined in the various <code>Format</code>
1.118 + * subclasses. This is equivalent to calling
1.119 + * <code>new FieldPosition(attribute, -1)</code>.
1.120 + *
1.121 + * @param attribute Format.Field constant identifying a field
1.122 + * @since 1.4
1.123 + */
1.124 + public FieldPosition(Format.Field attribute) {
1.125 + this(attribute, -1);
1.126 + }
1.127 +
1.128 + /**
1.129 + * Creates a <code>FieldPosition</code> object for the given field.
1.130 + * The field is identified by an attribute constant from one of the
1.131 + * <code>Field</code> subclasses as well as an integer field ID
1.132 + * defined by the <code>Format</code> subclasses. <code>Format</code>
1.133 + * subclasses that are aware of <code>Field</code> should give precedence
1.134 + * to <code>attribute</code> and ignore <code>fieldID</code> if
1.135 + * <code>attribute</code> is not null. However, older <code>Format</code>
1.136 + * subclasses may not be aware of <code>Field</code> and rely on
1.137 + * <code>fieldID</code>. If the field has no corresponding integer
1.138 + * constant, <code>fieldID</code> should be -1.
1.139 + *
1.140 + * @param attribute Format.Field constant identifying a field
1.141 + * @param fieldID integer constantce identifying a field
1.142 + * @since 1.4
1.143 + */
1.144 + public FieldPosition(Format.Field attribute, int fieldID) {
1.145 + this.attribute = attribute;
1.146 + this.field = fieldID;
1.147 + }
1.148 +
1.149 + /**
1.150 + * Returns the field identifier as an attribute constant
1.151 + * from one of the <code>Field</code> subclasses. May return null if
1.152 + * the field is specified only by an integer field ID.
1.153 + *
1.154 + * @return Identifier for the field
1.155 + * @since 1.4
1.156 + */
1.157 + public Format.Field getFieldAttribute() {
1.158 + return attribute;
1.159 + }
1.160 +
1.161 + /**
1.162 + * Retrieves the field identifier.
1.163 + */
1.164 + public int getField() {
1.165 + return field;
1.166 + }
1.167 +
1.168 + /**
1.169 + * Retrieves the index of the first character in the requested field.
1.170 + */
1.171 + public int getBeginIndex() {
1.172 + return beginIndex;
1.173 + }
1.174 +
1.175 + /**
1.176 + * Retrieves the index of the character following the last character in the
1.177 + * requested field.
1.178 + */
1.179 + public int getEndIndex() {
1.180 + return endIndex;
1.181 + }
1.182 +
1.183 + /**
1.184 + * Sets the begin index. For use by subclasses of Format.
1.185 + * @since 1.2
1.186 + */
1.187 + public void setBeginIndex(int bi) {
1.188 + beginIndex = bi;
1.189 + }
1.190 +
1.191 + /**
1.192 + * Sets the end index. For use by subclasses of Format.
1.193 + * @since 1.2
1.194 + */
1.195 + public void setEndIndex(int ei) {
1.196 + endIndex = ei;
1.197 + }
1.198 +
1.199 + /**
1.200 + * Returns a <code>Format.FieldDelegate</code> instance that is associated
1.201 + * with the FieldPosition. When the delegate is notified of the same
1.202 + * field the FieldPosition is associated with, the begin/end will be
1.203 + * adjusted.
1.204 + */
1.205 + Format.FieldDelegate getFieldDelegate() {
1.206 + return new Delegate();
1.207 + }
1.208 +
1.209 + /**
1.210 + * Overrides equals
1.211 + */
1.212 + public boolean equals(Object obj)
1.213 + {
1.214 + if (obj == null) return false;
1.215 + if (!(obj instanceof FieldPosition))
1.216 + return false;
1.217 + FieldPosition other = (FieldPosition) obj;
1.218 + if (attribute == null) {
1.219 + if (other.attribute != null) {
1.220 + return false;
1.221 + }
1.222 + }
1.223 + else if (!attribute.equals(other.attribute)) {
1.224 + return false;
1.225 + }
1.226 + return (beginIndex == other.beginIndex
1.227 + && endIndex == other.endIndex
1.228 + && field == other.field);
1.229 + }
1.230 +
1.231 + /**
1.232 + * Returns a hash code for this FieldPosition.
1.233 + * @return a hash code value for this object
1.234 + */
1.235 + public int hashCode() {
1.236 + return (field << 24) | (beginIndex << 16) | endIndex;
1.237 + }
1.238 +
1.239 + /**
1.240 + * Return a string representation of this FieldPosition.
1.241 + * @return a string representation of this object
1.242 + */
1.243 + public String toString() {
1.244 + return getClass().getName() +
1.245 + "[field=" + field + ",attribute=" + attribute +
1.246 + ",beginIndex=" + beginIndex +
1.247 + ",endIndex=" + endIndex + ']';
1.248 + }
1.249 +
1.250 +
1.251 + /**
1.252 + * Return true if the receiver wants a <code>Format.Field</code> value and
1.253 + * <code>attribute</code> is equal to it.
1.254 + */
1.255 + private boolean matchesField(Format.Field attribute) {
1.256 + if (this.attribute != null) {
1.257 + return this.attribute.equals(attribute);
1.258 + }
1.259 + return false;
1.260 + }
1.261 +
1.262 + /**
1.263 + * Return true if the receiver wants a <code>Format.Field</code> value and
1.264 + * <code>attribute</code> is equal to it, or true if the receiver
1.265 + * represents an inteter constant and <code>field</code> equals it.
1.266 + */
1.267 + private boolean matchesField(Format.Field attribute, int field) {
1.268 + if (this.attribute != null) {
1.269 + return this.attribute.equals(attribute);
1.270 + }
1.271 + return (field == this.field);
1.272 + }
1.273 +
1.274 +
1.275 + /**
1.276 + * An implementation of FieldDelegate that will adjust the begin/end
1.277 + * of the FieldPosition if the arguments match the field of
1.278 + * the FieldPosition.
1.279 + */
1.280 + private class Delegate implements Format.FieldDelegate {
1.281 + /**
1.282 + * Indicates whether the field has been encountered before. If this
1.283 + * is true, and <code>formatted</code> is invoked, the begin/end
1.284 + * are not updated.
1.285 + */
1.286 + private boolean encounteredField;
1.287 +
1.288 + public void formatted(Format.Field attr, Object value, int start,
1.289 + int end, StringBuffer buffer) {
1.290 + if (!encounteredField && matchesField(attr)) {
1.291 + setBeginIndex(start);
1.292 + setEndIndex(end);
1.293 + encounteredField = (start != end);
1.294 + }
1.295 + }
1.296 +
1.297 + public void formatted(int fieldID, Format.Field attr, Object value,
1.298 + int start, int end, StringBuffer buffer) {
1.299 + if (!encounteredField && matchesField(attr, fieldID)) {
1.300 + setBeginIndex(start);
1.301 + setEndIndex(end);
1.302 + encounteredField = (start != end);
1.303 + }
1.304 + }
1.305 + }
1.306 +}