/*
*******************************************************************************
* Copyright (C) 2010-2014, International Business Machines
* Corporation and others. All Rights Reserved.
*******************************************************************************
* created on: 2010aug21
* created by: Markus W. Scherer
*/
package androidx.core.i18n.messageformat_icu.text;
import androidx.annotation.RestrictTo;
import androidx.core.i18n.messageformat_icu.impl.PatternProps;
import androidx.core.i18n.messageformat_icu.util.Freezable;
import androidx.core.i18n.messageformat_icu.util.ICUCloneNotSupportedException;
import java.util.ArrayList;
import java.util.Locale;
import androidx.core.i18n.messageformat_icu.impl.ICUConfig;
//Note: Minimize ICU dependencies, only use a very small part of the ICU core.
//In particular, do not depend on *Format classes.
/**
* Parses and represents ICU MessageFormat patterns.
* Also handles patterns for ChoiceFormat, PluralFormat and SelectFormat.
* Used in the implementations of those classes as well as in tools
* for message validation, translation and format conversion.
* <p>
* The parser handles all syntax relevant for identifying message arguments.
* This includes "complex" arguments whose style strings contain
* nested MessageFormat pattern substrings.
* For "simple" arguments (with no nested MessageFormat pattern substrings),
* the argument style is not parsed any further.
* <p>
* The parser handles named and numbered message arguments and allows both in one message.
* <p>
* Once a pattern has been parsed successfully, iterate through the parsed data
* with countParts(), getPart() and related methods.
* <p>
* The data logically represents a parse tree, but is stored and accessed
* as a list of "parts" for fast and simple parsing and to minimize object allocations.
* Arguments and nested messages are best handled via recursion.
* For every _START "part", {@link #getLimitPartIndex(int)} efficiently returns
* the index of the corresponding _LIMIT "part".
* <p>
* List of "parts":
* <pre>
* message = MSG_START (SKIP_SYNTAX | INSERT_CHAR | REPLACE_NUMBER | argument)* MSG_LIMIT
* argument = noneArg | simpleArg | complexArg
* complexArg = choiceArg | pluralArg | selectArg
*
* noneArg = ARG_START.NONE (ARG_NAME | ARG_NUMBER) ARG_LIMIT.NONE
* simpleArg = ARG_START.SIMPLE (ARG_NAME | ARG_NUMBER) ARG_TYPE [ARG_STYLE] ARG_LIMIT.SIMPLE
* choiceArg = ARG_START.CHOICE (ARG_NAME | ARG_NUMBER) choiceStyle ARG_LIMIT.CHOICE
* pluralArg = ARG_START.PLURAL (ARG_NAME | ARG_NUMBER) pluralStyle ARG_LIMIT.PLURAL
* selectArg = ARG_START.SELECT (ARG_NAME | ARG_NUMBER) selectStyle ARG_LIMIT.SELECT
*
* choiceStyle = ((ARG_INT | ARG_DOUBLE) ARG_SELECTOR message)+
* pluralStyle = [ARG_INT | ARG_DOUBLE] (ARG_SELECTOR [ARG_INT | ARG_DOUBLE] message)+
* selectStyle = (ARG_SELECTOR message)+
* </pre>
* <ul>
* <li>Literal output text is not represented directly by "parts" but accessed
* between parts of a message, from one part's getLimit() to the next part's getIndex().
* <li><code>ARG_START.CHOICE</code> stands for an ARG_START Part with ArgType CHOICE.
* <li>In the choiceStyle, the ARG_SELECTOR has the '<', the '#' or
* the less-than-or-equal-to sign (U+2264).
* <li>In the pluralStyle, the first, optional numeric Part has the "offset:" value.
* The optional numeric Part between each (ARG_SELECTOR, message) pair
* is the value of an explicit-number selector like "=2",
* otherwise the selector is a non-numeric identifier.
* <li>The REPLACE_NUMBER Part can occur only in an immediate sub-message of the pluralStyle.
* <p>
* This class is not intended for public subclassing.
*
* icu_annot::stable ICU 4.8
* @author Markus Scherer
*/
@RestrictTo(RestrictTo.Scope.LIBRARY)
public final class MessagePattern implements Cloneable, Freezable<MessagePattern> {
/**
* Mode for when an apostrophe starts quoted literal text for MessageFormat output.
* The default is DOUBLE_OPTIONAL unless overridden via ICUConfig
* (/com/ibm/icu/ICUConfig.properties).
* <p>
* A pair of adjacent apostrophes always results in a single apostrophe in the output,
* even when the pair is between two single, text-quoting apostrophes.
* <p>
* The following table shows examples of desired MessageFormat.format() output
* with the pattern strings that yield that output.
* <p>
* <table>
* <tr>
* <th>Desired output</th>
* <th>DOUBLE_OPTIONAL</th>
* <th>DOUBLE_REQUIRED</th>
* </tr>
* <tr>
* <td>I see {many}</td>
* <td>I see '{many}'</td>
* <td>(same)</td>
* </tr>
* <tr>
* <td>I said {'Wow!'}</td>
* <td>I said '{''Wow!''}'</td>
* <td>(same)</td>
* </tr>
* <tr>
* <td>I don't know</td>
* <td>I don't know OR<br> I don''t know</td>
* <td>I don''t know</td>
* </tr>
* </table>
* icu_annot::stable ICU 4.8
*/
public enum ApostropheMode {
/**
* A literal apostrophe is represented by
* either a single or a double apostrophe pattern character.
* Within a MessageFormat pattern, a single apostrophe only starts quoted literal text
* if it immediately precedes a curly brace {},
* or a pipe symbol | if inside a choice format,
* or a pound symbol # if inside a plural format.
* <p>
* This is the default behavior starting with ICU 4.8.
* icu_annot::stable ICU 4.8
*/
DOUBLE_OPTIONAL,
/**
* A literal apostrophe must be represented by
* a double apostrophe pattern character.
* A single apostrophe always starts quoted literal text.
* <p>
* This is the behavior of ICU 4.6 and earlier, and of the JDK.
* icu_annot::stable ICU 4.8
*/
DOUBLE_REQUIRED
}
/**
* Constructs an empty MessagePattern with default ApostropheMode.
* icu_annot::stable ICU 4.8
*/
public MessagePattern() {
aposMode=defaultAposMode;
}
/**
* Constructs an empty MessagePattern.
* @param mode Explicit ApostropheMode.
* icu_annot::stable ICU 4.8
*/
public MessagePattern(ApostropheMode mode) {
aposMode=mode;
}
/**
* Constructs a MessagePattern with default ApostropheMode and
* parses the MessageFormat pattern string.
* @param pattern a MessageFormat pattern string
* @throws IllegalArgumentException for syntax errors in the pattern string
* @throws IndexOutOfBoundsException if certain limits are exceeded
* (e.g., argument number too high, argument name too long, etc.)
* @throws NumberFormatException if a number could not be parsed
* icu_annot::stable ICU 4.8
*/
public MessagePattern(String pattern) {
aposMode=defaultAposMode;
parse(pattern);
}
/**
* Parses a MessageFormat pattern string.
* @param pattern a MessageFormat pattern string
* @return this
* @throws IllegalArgumentException for syntax errors in the pattern string
* @throws IndexOutOfBoundsException if certain limits are exceeded
* (e.g., argument number too high, argument name too long, etc.)
* @throws NumberFormatException if a number could not be parsed
* icu_annot::stable ICU 4.8
*/
public MessagePattern parse(String pattern) {
preParse(pattern);
parseMessage(0, 0, 0, ArgType.NONE);
postParse();
return this;
}
/**
* Parses a ChoiceFormat pattern string.
* @param pattern a ChoiceFormat pattern string
* @return this
* @throws IllegalArgumentException for syntax errors in the pattern string
* @throws IndexOutOfBoundsException if certain limits are exceeded
* (e.g., argument number too high, argument name too long, etc.)
* @throws NumberFormatException if a number could not be parsed
* icu_annot::stable ICU 4.8
*/
public MessagePattern parseChoiceStyle(String pattern) {
preParse(pattern);
parseChoiceStyle(0, 0);
postParse();
return this;
}
/**
* Parses a PluralFormat pattern string.
* @param pattern a PluralFormat pattern string
* @return this
* @throws IllegalArgumentException for syntax errors in the pattern string
* @throws IndexOutOfBoundsException if certain limits are exceeded
* (e.g., argument number too high, argument name too long, etc.)
* @throws NumberFormatException if a number could not be parsed
* icu_annot::stable ICU 4.8
*/
public MessagePattern parsePluralStyle(String pattern) {
preParse(pattern);
parsePluralOrSelectStyle(ArgType.PLURAL, 0, 0);
postParse();
return this;
}
/**
* Parses a SelectFormat pattern string.
* @param pattern a SelectFormat pattern string
* @return this
* @throws IllegalArgumentException for syntax errors in the pattern string
* @throws IndexOutOfBoundsException if certain limits are exceeded
* (e.g., argument number too high, argument name too long, etc.)
* @throws NumberFormatException if a number could not be parsed
* icu_annot::stable ICU 4.8
*/
public MessagePattern parseSelectStyle(String pattern) {
preParse(pattern);
parsePluralOrSelectStyle(ArgType.SELECT, 0, 0);
postParse();
return this;
}
/**
* Clears this MessagePattern.
* countParts() will return 0.
* icu_annot::stable ICU 4.8
*/
public void clear() {
// Mostly the same as preParse().
if(isFrozen()) {
throw new UnsupportedOperationException(
"Attempt to clear() a frozen MessagePattern instance.");
}
msg=null;
hasArgNames=hasArgNumbers=false;
needsAutoQuoting=false;
parts.clear();
if(numericValues!=null) {
numericValues.clear();
}
}
/**
* Clears this MessagePattern and sets the ApostropheMode.
* countParts() will return 0.
* @param mode The new ApostropheMode.
* icu_annot::stable ICU 4.8
*/
public void clearPatternAndSetApostropheMode(ApostropheMode mode) {
clear();
aposMode=mode;
}
/**
* @param other another object to compare with.
* @return true if this object is equivalent to the other one.
* icu_annot::stable ICU 4.8
*/
@Override
public boolean equals(Object other) {
if(this==other) {
return true;
}
if(other==null || getClass()!=other.getClass()) {
return false;
}
MessagePattern o=(MessagePattern)other;
return
aposMode.equals(o.aposMode) &&
(msg==null ? o.msg==null : msg.equals(o.msg)) &&
parts.equals(o.parts);
// No need to compare numericValues if msg and parts are the same.
}
/**
* {@inheritDoc}
* icu_annot::stable ICU 4.8
*/
@Override
public int hashCode() {
return (aposMode.hashCode()*37+(msg!=null ? msg.hashCode() : 0))*37+parts.hashCode();
}
/**
* @return this instance's ApostropheMode.
* icu_annot::stable ICU 4.8
*/
public ApostropheMode getApostropheMode() {
return aposMode;
}
/**
* @return true if getApostropheMode() == ApostropheMode.DOUBLE_REQUIRED
* icu_annot::internal
*/
public boolean jdkAposMode() {
return aposMode == ApostropheMode.DOUBLE_REQUIRED;
}
/**
* @return the parsed pattern string (null if none was parsed).
* icu_annot::stable ICU 4.8
*/
public String getPatternString() {
return msg;
}
/**
* Does the parsed pattern have named arguments like {first_name}?
* @return true if the parsed pattern has at least one named argument.
* icu_annot::stable ICU 4.8
*/
public boolean hasNamedArguments() {
return hasArgNames;
}
/**
* Does the parsed pattern have numbered arguments like {2}?
* @return true if the parsed pattern has at least one numbered argument.
* icu_annot::stable ICU 4.8
*/
public boolean hasNumberedArguments() {
return hasArgNumbers;
}
/**
* {@inheritDoc}
* icu_annot::stable ICU 4.8
*/
@Override
public String toString() {
return msg;
}
/**
* Validates and parses an argument name or argument number string.
* An argument name must be a "pattern identifier", that is, it must contain
* no Unicode Pattern_Syntax or Pattern_White_Space characters.
* If it only contains ASCII digits, then it must be a small integer with no leading zero.
* @param name Input string.
* @return >=0 if the name is a valid number,
* ARG_NAME_NOT_NUMBER (-1) if it is a "pattern identifier" but not all ASCII digits,
* ARG_NAME_NOT_VALID (-2) if it is neither.
* icu_annot::stable ICU 4.8
*/
public static int validateArgumentName(String name) {
if(!PatternProps.isIdentifier(name)) {
return ARG_NAME_NOT_VALID;
}
return parseArgNumber(name, 0, name.length());
}
/**
* Return value from {@link #validateArgumentName(String)} for when
* the string is a valid "pattern identifier" but not a number.
* icu_annot::stable ICU 4.8
*/
public static final int ARG_NAME_NOT_NUMBER=-1;
/**
* Return value from {@link #validateArgumentName(String)} for when
* the string is invalid.
* It might not be a valid "pattern identifier",
* or it have only ASCII digits but there is a leading zero or the number is too large.
* icu_annot::stable ICU 4.8
*/
public static final int ARG_NAME_NOT_VALID=-2;
/**
* Returns a version of the parsed pattern string where each ASCII apostrophe
* is doubled (escaped) if it is not already, and if it is not interpreted as quoting syntax.
* <p>
* For example, this turns "I don't '{know}' {gender,select,female{h''er}other{h'im}}."
* into "I don''t '{know}' {gender,select,female{h''er}other{h''im}}."
* @return the deep-auto-quoted version of the parsed pattern string.
* @see MessageFormat#autoQuoteApostrophe(String)
* icu_annot::stable ICU 4.8
*/
public String autoQuoteApostropheDeep() {
if(!needsAutoQuoting) {
return msg;
}
StringBuilder modified=null;
// Iterate backward so that the insertion indexes do not change.
int count=countParts();
for(int i=count; i>0;) {
Part part;
if((part=getPart(--i)).getType()==Part.Type.INSERT_CHAR) {
if(modified==null) {
modified=new StringBuilder(msg.length()+10).append(msg);
}
modified.insert(part.index, (char)part.value);
}
}
if(modified==null) {
return msg;
} else {
return modified.toString();
}
}
/**
* Returns the number of "parts" created by parsing the pattern string.
* Returns 0 if no pattern has been parsed or clear() was called.
* @return the number of pattern parts.
* icu_annot::stable ICU 4.8
*/
public int countParts() {
return parts.size();
}
/**
* Gets the i-th pattern "part".
* @param i The index of the Part data. (0..countParts()-1)
* @return the i-th pattern "part".
* @throws IndexOutOfBoundsException if i is outside the (0..countParts()-1) range
* icu_annot::stable ICU 4.8
*/
public Part getPart(int i) {
return parts.get(i);
}
/**
* Returns the Part.Type of the i-th pattern "part".
* Convenience method for getPart(i).getType().
* @param i The index of the Part data. (0..countParts()-1)
* @return The Part.Type of the i-th Part.
* @throws IndexOutOfBoundsException if i is outside the (0..countParts()-1) range
* icu_annot::stable ICU 4.8
*/
public Part.Type getPartType(int i) {
return parts.get(i).type;
}
/**
* Returns the pattern index of the specified pattern "part".
* Convenience method for getPart(partIndex).getIndex().
* @param partIndex The index of the Part data. (0..countParts()-1)
* @return The pattern index of this Part.
* @throws IndexOutOfBoundsException if partIndex is outside the (0..countParts()-1) range
* icu_annot::stable ICU 4.8
*/
public int getPatternIndex(int partIndex) {
return parts.get(partIndex).index;
}
/**
* Returns the substring of the pattern string indicated by the Part.
* Convenience method for getPatternString().substring(part.getIndex(), part.getLimit()).
* @param part a part of this MessagePattern.
* @return the substring associated with part.
* icu_annot::stable ICU 4.8
*/
public String getSubstring(Part part) {
int index=part.index;
return msg.substring(index, index+part.length);
}
/**
* Compares the part's substring with the input string s.
* @param part a part of this MessagePattern.
* @param s a string.
* @return true if getSubstring(part).equals(s).
* icu_annot::stable ICU 4.8
*/
public boolean partSubstringMatches(Part part, String s) {
return msg.regionMatches(part.index, s, 0, part.length);
}
/**
* Returns the numeric value associated with an ARG_INT or ARG_DOUBLE.
* @param part a part of this MessagePattern.
* @return the part's numeric value, or NO_NUMERIC_VALUE if this is not a numeric part.
* icu_annot::stable ICU 4.8
*/
public double getNumericValue(Part part) {
Part.Type type=part.type;
if(type==Part.Type.ARG_INT) {
return part.value;
} else if(type==Part.Type.ARG_DOUBLE) {
return numericValues.get(part.value);
} else {
return NO_NUMERIC_VALUE;
}
}
/**
* Special value that is returned by getNumericValue(Part) when no
* numeric value is defined for a part.
* @see #getNumericValue
* icu_annot::stable ICU 4.8
*/
public static final double NO_NUMERIC_VALUE=-123456789;
/**
* Returns the "offset:" value of a PluralFormat argument, or 0 if none is specified.
* @param pluralStart the index of the first PluralFormat argument style part. (0..countParts()-1)
* @return the "offset:" value.
* @throws IndexOutOfBoundsException if pluralStart is outside the (0..countParts()-1) range
* icu_annot::stable ICU 4.8
*/
public double getPluralOffset(int pluralStart) {
Part part=parts.get(pluralStart);
if(part.type.hasNumericValue()) {
return getNumericValue(part);
} else {
return 0;
}
}
/**
* Returns the index of the ARG|MSG_LIMIT part corresponding to the ARG|MSG_START at start.
* @param start The index of some Part data (0..countParts()-1);
* this Part should be of Type ARG_START or MSG_START.
* @return The first i>start where getPart(i).getType()==ARG|MSG_LIMIT at the same nesting level,
* or start itself if getPartType(msgStart)!=ARG|MSG_START.
* @throws IndexOutOfBoundsException if start is outside the (0..countParts()-1) range
* icu_annot::stable ICU 4.8
*/
public int getLimitPartIndex(int start) {
int limit=parts.get(start).limitPartIndex;
if(limit<start) {
return start;
}
return limit;
}
/**
* A message pattern "part", representing a pattern parsing event.
* There is a part for the start and end of a message or argument,
* for quoting and escaping of and with ASCII apostrophes,
* and for syntax elements of "complex" arguments.
* icu_annot::stable ICU 4.8
*/
public static final class Part {
private Part(Type t, int i, int l, int v) {
type=t;
index=i;
length=(char)l;
value=(short)v;
}
/**
* Returns the type of this part.
* @return the part type.
* icu_annot::stable ICU 4.8
*/
public Type getType() {
return type;
}
/**
* Returns the pattern string index associated with this Part.
* @return this part's pattern string index.
* icu_annot::stable ICU 4.8
*/
public int getIndex() {
return index;
}
/**
* Returns the length of the pattern substring associated with this Part.
* This is 0 for some parts.
* @return this part's pattern substring length.
* icu_annot::stable ICU 4.8
*/
public int getLength() {
return length;
}
/**
* Returns the pattern string limit (exclusive-end) index associated with this Part.
* Convenience method for getIndex()+getLength().
* @return this part's pattern string limit index, same as getIndex()+getLength().
* icu_annot::stable ICU 4.8
*/
public int getLimit() {
return index+length;
}
/**
* Returns a value associated with this part.
* See the documentation of each part type for details.
* @return the part value.
* icu_annot::stable ICU 4.8
*/
public int getValue() {
return value;
}
/**
* Returns the argument type if this part is of type ARG_START or ARG_LIMIT,
* otherwise ArgType.NONE.
* @return the argument type for this part.
* icu_annot::stable ICU 4.8
*/
public ArgType getArgType() {
Type type=getType();
if(type==Type.ARG_START || type==Type.ARG_LIMIT) {
return argTypes[value];
} else {
return ArgType.NONE;
}
}
/**
* Part type constants.
* icu_annot::stable ICU 4.8
*/
public enum Type {
/**
* Start of a message pattern (main or nested).
* The length is 0 for the top-level message
* and for a choice argument sub-message, otherwise 1 for the '{'.
* The value indicates the nesting level, starting with 0 for the main message.
* <p>
* There is always a later MSG_LIMIT part.
* icu_annot::stable ICU 4.8
*/
MSG_START,
/**
* End of a message pattern (main or nested).
* The length is 0 for the top-level message and
* the last sub-message of a choice argument,
* otherwise 1 for the '}' or (in a choice argument style) the '|'.
* The value indicates the nesting level, starting with 0 for the main message.
* icu_annot::stable ICU 4.8
*/
MSG_LIMIT,
/**
* Indicates a substring of the pattern string which is to be skipped when formatting.
* For example, an apostrophe that begins or ends quoted text
* would be indicated with such a part.
* The value is undefined and currently always 0.
* icu_annot::stable ICU 4.8
*/
SKIP_SYNTAX,
/**
* Indicates that a syntax character needs to be inserted for auto-quoting.
* The length is 0.
* The value is the character code of the insertion character. (U+0027=APOSTROPHE)
* icu_annot::stable ICU 4.8
*/
INSERT_CHAR,
/**
* Indicates a syntactic (non-escaped) # symbol in a plural variant.
* When formatting, replace this part's substring with the
* (value-offset) for the plural argument value.
* The value is undefined and currently always 0.
* icu_annot::stable ICU 4.8
*/
REPLACE_NUMBER,
/**
* Start of an argument.
* The length is 1 for the '{'.
* The value is the ordinal value of the ArgType. Use getArgType().
* <p>
* This part is followed by either an ARG_NUMBER or ARG_NAME,
* followed by optional argument sub-parts (see ArgType constants)
* and finally an ARG_LIMIT part.
* icu_annot::stable ICU 4.8
*/
ARG_START,
/**
* End of an argument.
* The length is 1 for the '}'.
* The value is the ordinal value of the ArgType. Use getArgType().
* icu_annot::stable ICU 4.8
*/
ARG_LIMIT,
/**
* The argument number, provided by the value.
* icu_annot::stable ICU 4.8
*/
ARG_NUMBER,
/**
* The argument name.
* The value is undefined and currently always 0.
* icu_annot::stable ICU 4.8
*/
ARG_NAME,
/**
* The argument type.
* The value is undefined and currently always 0.
* icu_annot::stable ICU 4.8
*/
ARG_TYPE,
/**
* The argument style text.
* The value is undefined and currently always 0.
* icu_annot::stable ICU 4.8
*/
ARG_STYLE,
/**
* A selector substring in a "complex" argument style.
* The value is undefined and currently always 0.
* icu_annot::stable ICU 4.8
*/
ARG_SELECTOR,
/**
* An integer value, for example the offset or an explicit selector value
* in a PluralFormat style.
* The part value is the integer value.
* icu_annot::stable ICU 4.8
*/
ARG_INT,
/**
* A numeric value, for example the offset or an explicit selector value
* in a PluralFormat style.
* The part value is an index into an internal array of numeric values;
* use getNumericValue().
* icu_annot::stable ICU 4.8
*/
ARG_DOUBLE;
/**
* Indicates whether this part has a numeric value.
* If so, then that numeric value can be retrieved via {@link MessagePattern#getNumericValue(Part)}.
* @return true if this part has a numeric value.
* icu_annot::stable ICU 4.8
*/
public boolean hasNumericValue() {
return this==ARG_INT || this==ARG_DOUBLE;
}
}
/**
* @return a string representation of this part.
* icu_annot::stable ICU 4.8
*/
@Override
public String toString() {
String valueString=(type==Type.ARG_START || type==Type.ARG_LIMIT) ?
getArgType().name() : Integer.toString(value);
return type.name()+"("+valueString+")@"+index;
}
/**
* @param other another object to compare with.
* @return true if this object is equivalent to the other one.
* icu_annot::stable ICU 4.8
*/
@Override
public boolean equals(Object other) {
if(this==other) {
return true;
}
if(other==null || getClass()!=other.getClass()) {
return false;
}
Part o=(Part)other;
return
type.equals(o.type) &&
index==o.index &&
length==o.length &&
value==o.value &&
limitPartIndex==o.limitPartIndex;
}
/**
* {@inheritDoc}
* icu_annot::stable ICU 4.8
*/
@Override
public int hashCode() {
return ((type.hashCode()*37+index)*37+length)*37+value;
}
private static final int MAX_LENGTH=0xffff;
private static final int MAX_VALUE=Short.MAX_VALUE;
// Some fields are not final because they are modified during pattern parsing.
// After pattern parsing, the parts are effectively immutable.
private final Type type;
private final int index;
private final char length;
private short value;
private int limitPartIndex;
}
/**
* Argument type constants.
* Returned by Part.getArgType() for ARG_START and ARG_LIMIT parts.
*
* Messages nested inside an argument are each delimited by MSG_START and MSG_LIMIT,
* with a nesting level one greater than the surrounding message.
* icu_annot::stable ICU 4.8
*/
public enum ArgType {
/**
* The argument has no specified type.
* icu_annot::stable ICU 4.8
*/
NONE,
/**
* The argument has a "simple" type which is provided by the ARG_TYPE part.
* An ARG_STYLE part might follow that.
* icu_annot::stable ICU 4.8
*/
SIMPLE,
/**
* The argument is a ChoiceFormat with one or more
* ((ARG_INT | ARG_DOUBLE), ARG_SELECTOR, message) tuples.
* icu_annot::stable ICU 4.8
*/
CHOICE,
/**
* The argument is a cardinal-number PluralFormat with an optional ARG_INT or ARG_DOUBLE offset
* (e.g., offset:1)
* and one or more (ARG_SELECTOR [explicit-value] message) tuples.
* If the selector has an explicit value (e.g., =2), then
* that value is provided by the ARG_INT or ARG_DOUBLE part preceding the message.
* Otherwise the message immediately follows the ARG_SELECTOR.
* icu_annot::stable ICU 4.8
*/
PLURAL,
/**
* The argument is a SelectFormat with one or more (ARG_SELECTOR, message) pairs.
* icu_annot::stable ICU 4.8
*/
SELECT,
/**
* The argument is an ordinal-number PluralFormat
* with the same style parts sequence and semantics as {@link ArgType#PLURAL}.
* icu_annot::stable ICU 50
*/
SELECTORDINAL;
/**
* @return true if the argument type has a plural style part sequence and semantics,
* for example {@link ArgType#PLURAL} and {@link ArgType#SELECTORDINAL}.
* icu_annot::stable ICU 50
*/
public boolean hasPluralStyle() {
return this == PLURAL || this == SELECTORDINAL;
}
}
/**
* Creates and returns a copy of this object.
* @return a copy of this object (or itself if frozen).
* icu_annot::stable ICU 4.8
*/
@Override
public Object clone() {
if(isFrozen()) {
return this;
} else {
return cloneAsThawed();
}
}
/**
* Creates and returns an unfrozen copy of this object.
* @return a copy of this object.
* icu_annot::stable ICU 4.8
*/
@SuppressWarnings("unchecked")
@Override
public MessagePattern cloneAsThawed() {
MessagePattern newMsg;
try {
newMsg=(MessagePattern)super.clone();
} catch (CloneNotSupportedException e) {
throw new ICUCloneNotSupportedException(e);
}
newMsg.parts=(ArrayList<Part>)parts.clone();
if(numericValues!=null) {
newMsg.numericValues=(ArrayList<Double>)numericValues.clone();
}
newMsg.frozen=false;
return newMsg;
}
/**
* Freezes this object, making it immutable and thread-safe.
* @return this
* icu_annot::stable ICU 4.8
*/
@Override
public MessagePattern freeze() {
frozen=true;
return this;
}
/**
* Determines whether this object is frozen (immutable) or not.
* @return true if this object is frozen.
* icu_annot::stable ICU 4.8
*/
@Override
public boolean isFrozen() {
return frozen;
}
private void preParse(String pattern) {
if(isFrozen()) {
throw new UnsupportedOperationException(
"Attempt to parse("+prefix(pattern)+") on frozen MessagePattern instance.");
}
msg=pattern;
hasArgNames=hasArgNumbers=false;
needsAutoQuoting=false;
parts.clear();
if(numericValues!=null) {
numericValues.clear();
}
}
private void postParse() {
// Nothing to be done currently.
}
private int parseMessage(int index, int msgStartLength, int nestingLevel, ArgType parentType) {
if(nestingLevel>Part.MAX_VALUE) {
throw new IndexOutOfBoundsException();
}
int msgStart=parts.size();
addPart(Part.Type.MSG_START, index, msgStartLength, nestingLevel);
index+=msgStartLength;
while(index<msg.length()) {
char c=msg.charAt(index++);
if(c=='