rt/emul/compact/src/main/java/java/text/Format.java
branchjdk7-b147
changeset 1334 588d5bf7a560
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/java/text/Format.java	Thu Oct 03 15:40:35 2013 +0200
     1.3 @@ -0,0 +1,406 @@
     1.4 +/*
     1.5 + * Copyright (c) 1996, 2005, 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, 1997 - All Rights Reserved
    1.31 + * (C) Copyright IBM Corp. 1996 - 1998 - 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 +import java.io.Serializable;
    1.45 +
    1.46 +/**
    1.47 + * <code>Format</code> is an abstract base class for formatting locale-sensitive
    1.48 + * information such as dates, messages, and numbers.
    1.49 + *
    1.50 + * <p>
    1.51 + * <code>Format</code> defines the programming interface for formatting
    1.52 + * locale-sensitive objects into <code>String</code>s (the
    1.53 + * <code>format</code> method) and for parsing <code>String</code>s back
    1.54 + * into objects (the <code>parseObject</code> method).
    1.55 + *
    1.56 + * <p>
    1.57 + * Generally, a format's <code>parseObject</code> method must be able to parse
    1.58 + * any string formatted by its <code>format</code> method. However, there may
    1.59 + * be exceptional cases where this is not possible. For example, a
    1.60 + * <code>format</code> method might create two adjacent integer numbers with
    1.61 + * no separator in between, and in this case the <code>parseObject</code> could
    1.62 + * not tell which digits belong to which number.
    1.63 + *
    1.64 + * <h4>Subclassing</h4>
    1.65 + *
    1.66 + * <p>
    1.67 + * The Java Platform provides three specialized subclasses of <code>Format</code>--
    1.68 + * <code>DateFormat</code>, <code>MessageFormat</code>, and
    1.69 + * <code>NumberFormat</code>--for formatting dates, messages, and numbers,
    1.70 + * respectively.
    1.71 + * <p>
    1.72 + * Concrete subclasses must implement three methods:
    1.73 + * <ol>
    1.74 + * <li> <code>format(Object obj, StringBuffer toAppendTo, FieldPosition pos)</code>
    1.75 + * <li> <code>formatToCharacterIterator(Object obj)</code>
    1.76 + * <li> <code>parseObject(String source, ParsePosition pos)</code>
    1.77 + * </ol>
    1.78 + * These general methods allow polymorphic parsing and formatting of objects
    1.79 + * and are used, for example, by <code>MessageFormat</code>.
    1.80 + * Subclasses often also provide additional <code>format</code> methods for
    1.81 + * specific input types as well as <code>parse</code> methods for specific
    1.82 + * result types. Any <code>parse</code> method that does not take a
    1.83 + * <code>ParsePosition</code> argument should throw <code>ParseException</code>
    1.84 + * when no text in the required format is at the beginning of the input text.
    1.85 + *
    1.86 + * <p>
    1.87 + * Most subclasses will also implement the following factory methods:
    1.88 + * <ol>
    1.89 + * <li>
    1.90 + * <code>getInstance</code> for getting a useful format object appropriate
    1.91 + * for the current locale
    1.92 + * <li>
    1.93 + * <code>getInstance(Locale)</code> for getting a useful format
    1.94 + * object appropriate for the specified locale
    1.95 + * </ol>
    1.96 + * In addition, some subclasses may also implement other
    1.97 + * <code>getXxxxInstance</code> methods for more specialized control. For
    1.98 + * example, the <code>NumberFormat</code> class provides
    1.99 + * <code>getPercentInstance</code> and <code>getCurrencyInstance</code>
   1.100 + * methods for getting specialized number formatters.
   1.101 + *
   1.102 + * <p>
   1.103 + * Subclasses of <code>Format</code> that allow programmers to create objects
   1.104 + * for locales (with <code>getInstance(Locale)</code> for example)
   1.105 + * must also implement the following class method:
   1.106 + * <blockquote>
   1.107 + * <pre>
   1.108 + * public static Locale[] getAvailableLocales()
   1.109 + * </pre>
   1.110 + * </blockquote>
   1.111 + *
   1.112 + * <p>
   1.113 + * And finally subclasses may define a set of constants to identify the various
   1.114 + * fields in the formatted output. These constants are used to create a FieldPosition
   1.115 + * object which identifies what information is contained in the field and its
   1.116 + * position in the formatted result. These constants should be named
   1.117 + * <code><em>item</em>_FIELD</code> where <code><em>item</em></code> identifies
   1.118 + * the field. For examples of these constants, see <code>ERA_FIELD</code> and its
   1.119 + * friends in {@link DateFormat}.
   1.120 + *
   1.121 + * <h4><a name="synchronization">Synchronization</a></h4>
   1.122 + *
   1.123 + * <p>
   1.124 + * Formats are generally not synchronized.
   1.125 + * It is recommended to create separate format instances for each thread.
   1.126 + * If multiple threads access a format concurrently, it must be synchronized
   1.127 + * externally.
   1.128 + *
   1.129 + * @see          java.text.ParsePosition
   1.130 + * @see          java.text.FieldPosition
   1.131 + * @see          java.text.NumberFormat
   1.132 + * @see          java.text.DateFormat
   1.133 + * @see          java.text.MessageFormat
   1.134 + * @author       Mark Davis
   1.135 + */
   1.136 +public abstract class Format implements Serializable, Cloneable {
   1.137 +
   1.138 +    private static final long serialVersionUID = -299282585814624189L;
   1.139 +
   1.140 +    /**
   1.141 +     * Sole constructor.  (For invocation by subclass constructors, typically
   1.142 +     * implicit.)
   1.143 +     */
   1.144 +    protected Format() {
   1.145 +    }
   1.146 +
   1.147 +    /**
   1.148 +     * Formats an object to produce a string. This is equivalent to
   1.149 +     * <blockquote>
   1.150 +     * {@link #format(Object, StringBuffer, FieldPosition) format}<code>(obj,
   1.151 +     *         new StringBuffer(), new FieldPosition(0)).toString();</code>
   1.152 +     * </blockquote>
   1.153 +     *
   1.154 +     * @param obj    The object to format
   1.155 +     * @return       Formatted string.
   1.156 +     * @exception IllegalArgumentException if the Format cannot format the given
   1.157 +     *            object
   1.158 +     */
   1.159 +    public final String format (Object obj) {
   1.160 +        return format(obj, new StringBuffer(), new FieldPosition(0)).toString();
   1.161 +    }
   1.162 +
   1.163 +    /**
   1.164 +     * Formats an object and appends the resulting text to a given string
   1.165 +     * buffer.
   1.166 +     * If the <code>pos</code> argument identifies a field used by the format,
   1.167 +     * then its indices are set to the beginning and end of the first such
   1.168 +     * field encountered.
   1.169 +     *
   1.170 +     * @param obj    The object to format
   1.171 +     * @param toAppendTo    where the text is to be appended
   1.172 +     * @param pos    A <code>FieldPosition</code> identifying a field
   1.173 +     *               in the formatted text
   1.174 +     * @return       the string buffer passed in as <code>toAppendTo</code>,
   1.175 +     *               with formatted text appended
   1.176 +     * @exception NullPointerException if <code>toAppendTo</code> or
   1.177 +     *            <code>pos</code> is null
   1.178 +     * @exception IllegalArgumentException if the Format cannot format the given
   1.179 +     *            object
   1.180 +     */
   1.181 +    public abstract StringBuffer format(Object obj,
   1.182 +                    StringBuffer toAppendTo,
   1.183 +                    FieldPosition pos);
   1.184 +
   1.185 +    /**
   1.186 +     * Formats an Object producing an <code>AttributedCharacterIterator</code>.
   1.187 +     * You can use the returned <code>AttributedCharacterIterator</code>
   1.188 +     * to build the resulting String, as well as to determine information
   1.189 +     * about the resulting String.
   1.190 +     * <p>
   1.191 +     * Each attribute key of the AttributedCharacterIterator will be of type
   1.192 +     * <code>Field</code>. It is up to each <code>Format</code> implementation
   1.193 +     * to define what the legal values are for each attribute in the
   1.194 +     * <code>AttributedCharacterIterator</code>, but typically the attribute
   1.195 +     * key is also used as the attribute value.
   1.196 +     * <p>The default implementation creates an
   1.197 +     * <code>AttributedCharacterIterator</code> with no attributes. Subclasses
   1.198 +     * that support fields should override this and create an
   1.199 +     * <code>AttributedCharacterIterator</code> with meaningful attributes.
   1.200 +     *
   1.201 +     * @exception NullPointerException if obj is null.
   1.202 +     * @exception IllegalArgumentException when the Format cannot format the
   1.203 +     *            given object.
   1.204 +     * @param obj The object to format
   1.205 +     * @return AttributedCharacterIterator describing the formatted value.
   1.206 +     * @since 1.4
   1.207 +     */
   1.208 +    public AttributedCharacterIterator formatToCharacterIterator(Object obj) {
   1.209 +        return createAttributedCharacterIterator(format(obj));
   1.210 +    }
   1.211 +
   1.212 +    /**
   1.213 +     * Parses text from a string to produce an object.
   1.214 +     * <p>
   1.215 +     * The method attempts to parse text starting at the index given by
   1.216 +     * <code>pos</code>.
   1.217 +     * If parsing succeeds, then the index of <code>pos</code> is updated
   1.218 +     * to the index after the last character used (parsing does not necessarily
   1.219 +     * use all characters up to the end of the string), and the parsed
   1.220 +     * object is returned. The updated <code>pos</code> can be used to
   1.221 +     * indicate the starting point for the next call to this method.
   1.222 +     * If an error occurs, then the index of <code>pos</code> is not
   1.223 +     * changed, the error index of <code>pos</code> is set to the index of
   1.224 +     * the character where the error occurred, and null is returned.
   1.225 +     *
   1.226 +     * @param source A <code>String</code>, part of which should be parsed.
   1.227 +     * @param pos A <code>ParsePosition</code> object with index and error
   1.228 +     *            index information as described above.
   1.229 +     * @return An <code>Object</code> parsed from the string. In case of
   1.230 +     *         error, returns null.
   1.231 +     * @exception NullPointerException if <code>pos</code> is null.
   1.232 +     */
   1.233 +    public abstract Object parseObject (String source, ParsePosition pos);
   1.234 +
   1.235 +    /**
   1.236 +     * Parses text from the beginning of the given string to produce an object.
   1.237 +     * The method may not use the entire text of the given string.
   1.238 +     *
   1.239 +     * @param source A <code>String</code> whose beginning should be parsed.
   1.240 +     * @return An <code>Object</code> parsed from the string.
   1.241 +     * @exception ParseException if the beginning of the specified string
   1.242 +     *            cannot be parsed.
   1.243 +     */
   1.244 +    public Object parseObject(String source) throws ParseException {
   1.245 +        ParsePosition pos = new ParsePosition(0);
   1.246 +        Object result = parseObject(source, pos);
   1.247 +        if (pos.index == 0) {
   1.248 +            throw new ParseException("Format.parseObject(String) failed",
   1.249 +                pos.errorIndex);
   1.250 +        }
   1.251 +        return result;
   1.252 +    }
   1.253 +
   1.254 +    /**
   1.255 +     * Creates and returns a copy of this object.
   1.256 +     *
   1.257 +     * @return a clone of this instance.
   1.258 +     */
   1.259 +    public Object clone() {
   1.260 +        try {
   1.261 +            return super.clone();
   1.262 +        } catch (CloneNotSupportedException e) {
   1.263 +            // will never happen
   1.264 +            return null;
   1.265 +        }
   1.266 +    }
   1.267 +
   1.268 +    //
   1.269 +    // Convenience methods for creating AttributedCharacterIterators from
   1.270 +    // different parameters.
   1.271 +    //
   1.272 +
   1.273 +    /**
   1.274 +     * Creates an <code>AttributedCharacterIterator</code> for the String
   1.275 +     * <code>s</code>.
   1.276 +     *
   1.277 +     * @param s String to create AttributedCharacterIterator from
   1.278 +     * @return AttributedCharacterIterator wrapping s
   1.279 +     */
   1.280 +    AttributedCharacterIterator createAttributedCharacterIterator(String s) {
   1.281 +        AttributedString as = new AttributedString(s);
   1.282 +
   1.283 +        return as.getIterator();
   1.284 +    }
   1.285 +
   1.286 +    /**
   1.287 +     * Creates an <code>AttributedCharacterIterator</code> containg the
   1.288 +     * concatenated contents of the passed in
   1.289 +     * <code>AttributedCharacterIterator</code>s.
   1.290 +     *
   1.291 +     * @param iterators AttributedCharacterIterators used to create resulting
   1.292 +     *                  AttributedCharacterIterators
   1.293 +     * @return AttributedCharacterIterator wrapping passed in
   1.294 +     *         AttributedCharacterIterators
   1.295 +     */
   1.296 +    AttributedCharacterIterator createAttributedCharacterIterator(
   1.297 +                       AttributedCharacterIterator[] iterators) {
   1.298 +        AttributedString as = new AttributedString(iterators);
   1.299 +
   1.300 +        return as.getIterator();
   1.301 +    }
   1.302 +
   1.303 +    /**
   1.304 +     * Returns an AttributedCharacterIterator with the String
   1.305 +     * <code>string</code> and additional key/value pair <code>key</code>,
   1.306 +     * <code>value</code>.
   1.307 +     *
   1.308 +     * @param string String to create AttributedCharacterIterator from
   1.309 +     * @param key Key for AttributedCharacterIterator
   1.310 +     * @param value Value associated with key in AttributedCharacterIterator
   1.311 +     * @return AttributedCharacterIterator wrapping args
   1.312 +     */
   1.313 +    AttributedCharacterIterator createAttributedCharacterIterator(
   1.314 +                      String string, AttributedCharacterIterator.Attribute key,
   1.315 +                      Object value) {
   1.316 +        AttributedString as = new AttributedString(string);
   1.317 +
   1.318 +        as.addAttribute(key, value);
   1.319 +        return as.getIterator();
   1.320 +    }
   1.321 +
   1.322 +    /**
   1.323 +     * Creates an AttributedCharacterIterator with the contents of
   1.324 +     * <code>iterator</code> and the additional attribute <code>key</code>
   1.325 +     * <code>value</code>.
   1.326 +     *
   1.327 +     * @param iterator Initial AttributedCharacterIterator to add arg to
   1.328 +     * @param key Key for AttributedCharacterIterator
   1.329 +     * @param value Value associated with key in AttributedCharacterIterator
   1.330 +     * @return AttributedCharacterIterator wrapping args
   1.331 +     */
   1.332 +    AttributedCharacterIterator createAttributedCharacterIterator(
   1.333 +              AttributedCharacterIterator iterator,
   1.334 +              AttributedCharacterIterator.Attribute key, Object value) {
   1.335 +        AttributedString as = new AttributedString(iterator);
   1.336 +
   1.337 +        as.addAttribute(key, value);
   1.338 +        return as.getIterator();
   1.339 +    }
   1.340 +
   1.341 +
   1.342 +    /**
   1.343 +     * Defines constants that are used as attribute keys in the
   1.344 +     * <code>AttributedCharacterIterator</code> returned
   1.345 +     * from <code>Format.formatToCharacterIterator</code> and as
   1.346 +     * field identifiers in <code>FieldPosition</code>.
   1.347 +     *
   1.348 +     * @since 1.4
   1.349 +     */
   1.350 +    public static class Field extends AttributedCharacterIterator.Attribute {
   1.351 +
   1.352 +        // Proclaim serial compatibility with 1.4 FCS
   1.353 +        private static final long serialVersionUID = 276966692217360283L;
   1.354 +
   1.355 +        /**
   1.356 +         * Creates a Field with the specified name.
   1.357 +         *
   1.358 +         * @param name Name of the attribute
   1.359 +         */
   1.360 +        protected Field(String name) {
   1.361 +            super(name);
   1.362 +        }
   1.363 +    }
   1.364 +
   1.365 +
   1.366 +    /**
   1.367 +     * FieldDelegate is notified by the various <code>Format</code>
   1.368 +     * implementations as they are formatting the Objects. This allows for
   1.369 +     * storage of the individual sections of the formatted String for
   1.370 +     * later use, such as in a <code>FieldPosition</code> or for an
   1.371 +     * <code>AttributedCharacterIterator</code>.
   1.372 +     * <p>
   1.373 +     * Delegates should NOT assume that the <code>Format</code> will notify
   1.374 +     * the delegate of fields in any particular order.
   1.375 +     *
   1.376 +     * @see FieldPosition.Delegate
   1.377 +     * @see CharacterIteratorFieldDelegate
   1.378 +     */
   1.379 +    interface FieldDelegate {
   1.380 +        /**
   1.381 +         * Notified when a particular region of the String is formatted. This
   1.382 +         * method will be invoked if there is no corresponding integer field id
   1.383 +         * matching <code>attr</code>.
   1.384 +         *
   1.385 +         * @param attr Identifies the field matched
   1.386 +         * @param value Value associated with the field
   1.387 +         * @param start Beginning location of the field, will be >= 0
   1.388 +         * @param end End of the field, will be >= start and <= buffer.length()
   1.389 +         * @param buffer Contains current formatted value, receiver should
   1.390 +         *        NOT modify it.
   1.391 +         */
   1.392 +        public void formatted(Format.Field attr, Object value, int start,
   1.393 +                              int end, StringBuffer buffer);
   1.394 +
   1.395 +        /**
   1.396 +         * Notified when a particular region of the String is formatted.
   1.397 +         *
   1.398 +         * @param fieldID Identifies the field by integer
   1.399 +         * @param attr Identifies the field matched
   1.400 +         * @param value Value associated with the field
   1.401 +         * @param start Beginning location of the field, will be >= 0
   1.402 +         * @param end End of the field, will be >= start and <= buffer.length()
   1.403 +         * @param buffer Contains current formatted value, receiver should
   1.404 +         *        NOT modify it.
   1.405 +         */
   1.406 +        public void formatted(int fieldID, Format.Field attr, Object value,
   1.407 +                              int start, int end, StringBuffer buffer);
   1.408 +    }
   1.409 +}