• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GENERATED SOURCE. DO NOT MODIFY. */
2 // © 2016 and later: Unicode, Inc. and others.
3 // License & terms of use: http://www.unicode.org/copyright.html#License
4 /*
5  *******************************************************************************
6  * Copyright (C) 2014-2016, International Business Machines Corporation and
7  * others. All Rights Reserved.
8  *******************************************************************************
9  */
10 package ohos.global.icu.text;
11 
12 import ohos.global.icu.impl.SimpleFormatterImpl;
13 
14 /**
15  * Formats simple patterns like "{1} was born in {0}".
16  * Minimal subset of MessageFormat; fast, simple, minimal dependencies.
17  * Supports only numbered arguments with no type nor style parameters,
18  * and formats only string values.
19  * Quoting via ASCII apostrophe compatible with ICU MessageFormat default behavior.
20  *
21  * <p>Factory methods throw exceptions for syntax errors
22  * and for too few or too many arguments/placeholders.
23  *
24  * <p>SimpleFormatter objects are immutable and can be safely cached like strings.
25  *
26  * <p>Example:
27  * <pre>
28  * SimpleFormatter fmt = SimpleFormatter.compile("{1} '{born}' in {0}");
29  *
30  * // Output: "paul {born} in england"
31  * System.out.println(fmt.format("england", "paul"));
32  * </pre>
33  *
34  * @see MessageFormat
35  * @see MessagePattern.ApostropheMode
36  * @hide exposed on OHOS
37  */
38 public final class SimpleFormatter {
39     // For internal use in Java, use SimpleFormatterImpl directly instead:
40     // It is most efficient to compile patterns to compiled-pattern strings
41     // and use them with static methods.
42     // (Avoids allocating SimpleFormatter wrapper objects.)
43 
44     /**
45      * Binary representation of the compiled pattern.
46      * @see SimpleFormatterImpl
47      */
48     private final String compiledPattern;
49 
SimpleFormatter(String compiledPattern)50     private SimpleFormatter(String compiledPattern) {
51         this.compiledPattern = compiledPattern;
52     }
53 
54     /**
55      * Creates a formatter from the pattern string.
56      *
57      * @param pattern The pattern string.
58      * @return The new SimpleFormatter object.
59      * @throws IllegalArgumentException for bad argument syntax.
60      */
compile(CharSequence pattern)61     public static SimpleFormatter compile(CharSequence pattern) {
62         return compileMinMaxArguments(pattern, 0, Integer.MAX_VALUE);
63     }
64 
65     /**
66      * Creates a formatter from the pattern string.
67      * The number of arguments checked against the given limits is the
68      * highest argument number plus one, not the number of occurrences of arguments.
69      *
70      * @param pattern The pattern string.
71      * @param min The pattern must have at least this many arguments.
72      * @param max The pattern must have at most this many arguments.
73      * @return The new SimpleFormatter object.
74      * @throws IllegalArgumentException for bad argument syntax and too few or too many arguments.
75      */
compileMinMaxArguments(CharSequence pattern, int min, int max)76     public static SimpleFormatter compileMinMaxArguments(CharSequence pattern, int min, int max) {
77         StringBuilder sb = new StringBuilder();
78         String compiledPattern = SimpleFormatterImpl.compileToStringMinMaxArguments(pattern, sb, min, max);
79         return new SimpleFormatter(compiledPattern);
80     }
81 
82     /**
83      * @return The max argument number + 1.
84      */
getArgumentLimit()85     public int getArgumentLimit() {
86         return SimpleFormatterImpl.getArgumentLimit(compiledPattern);
87     }
88 
89     /**
90      * Formats the given values.
91      */
format(CharSequence... values)92     public String format(CharSequence... values) {
93         return SimpleFormatterImpl.formatCompiledPattern(compiledPattern, values);
94     }
95 
96     /**
97      * Formats the given values, appending to the appendTo builder.
98      *
99      * @param appendTo Gets the formatted pattern and values appended.
100      * @param offsets offsets[i] receives the offset of where
101      *                values[i] replaced pattern argument {i}.
102      *                Can be null, or can be shorter or longer than values.
103      *                If there is no {i} in the pattern, then offsets[i] is set to -1.
104      * @param values The argument values.
105      *               An argument value must not be the same object as appendTo.
106      *               values.length must be at least getArgumentLimit().
107      *               Can be null if getArgumentLimit()==0.
108      * @return appendTo
109      */
formatAndAppend( StringBuilder appendTo, int[] offsets, CharSequence... values)110     public StringBuilder formatAndAppend(
111             StringBuilder appendTo, int[] offsets, CharSequence... values) {
112         return SimpleFormatterImpl.formatAndAppend(compiledPattern, appendTo, offsets, values);
113     }
114 
115     /**
116      * Formats the given values, replacing the contents of the result builder.
117      * May optimize by actually appending to the result if it is the same object
118      * as the value corresponding to the initial argument in the pattern.
119      *
120      * @param result Gets its contents replaced by the formatted pattern and values.
121      * @param offsets offsets[i] receives the offset of where
122      *                values[i] replaced pattern argument {i}.
123      *                Can be null, or can be shorter or longer than values.
124      *                If there is no {i} in the pattern, then offsets[i] is set to -1.
125      * @param values The argument values.
126      *               An argument value may be the same object as result.
127      *               values.length must be at least getArgumentLimit().
128      * @return result
129      */
formatAndReplace( StringBuilder result, int[] offsets, CharSequence... values)130     public StringBuilder formatAndReplace(
131             StringBuilder result, int[] offsets, CharSequence... values) {
132         return SimpleFormatterImpl.formatAndReplace(compiledPattern, result, offsets, values);
133     }
134 
135     /**
136      * Returns a string similar to the original pattern, only for debugging.
137      */
138     @Override
toString()139     public String toString() {
140         String[] values = new String[getArgumentLimit()];
141         for (int i = 0; i < values.length; i++) {
142             values[i] = "{" + i + '}';
143         }
144         return formatAndAppend(new StringBuilder(), null, values).toString();
145     }
146 
147     /**
148      * Returns the pattern text with none of the arguments.
149      * Like formatting with all-empty string values.
150      */
getTextWithNoArguments()151     public String getTextWithNoArguments() {
152         return SimpleFormatterImpl.getTextWithNoArguments(compiledPattern);
153     }
154 }
155