• 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) 2007-2010, International Business Machines Corporation and   *
7 * others. All Rights Reserved.                                               *
8 ******************************************************************************
9 */
10 
11 package ohos.global.icu.impl.duration;
12 
13 import java.util.Locale;
14 
15 import ohos.global.icu.impl.duration.impl.DataRecord.ECountVariant;
16 import ohos.global.icu.impl.duration.impl.DataRecord.ESeparatorVariant;
17 import ohos.global.icu.impl.duration.impl.DataRecord.EUnitVariant;
18 import ohos.global.icu.impl.duration.impl.PeriodFormatterData;
19 import ohos.global.icu.impl.duration.impl.PeriodFormatterDataService;
20 
21 /**
22  * An implementation of PeriodFormatterFactory that provides customization of
23  * formatting behavior. Instances of this factory are created by
24  * BasicPeriodFormatterService.
25  *
26  * The settings on BasicPeriodFormatterFactory are:
27  * <ul>
28  *
29  * <li><b>setDisplayLimit</b> controls whether phrases like 'more than'
30  * or 'less than' will be displayed when the Period has a defined
31  * limit.  Default is to display them.</li>
32  *
33  * <li><b>setDisplayPastFuture</b> controls whether phrases like 'ago'
34  * or 'from now' will be displayed to indicate past or future
35  * time. Default is to display them.</li>
36  *
37  * <li><b>setSeparatorVariant</b> controls how separators (between
38  * count and period, and multiple periods) will be displayed, when
39  * appropriate for the language. Default is to use full
40  * separators.</li>
41  *
42  * <li><b>setUnitVariant</b> controls which of various types of
43  * unit names to use.  PLURALIZED indicates that full names will be
44  * used.  MEDIUM indicates that medium-length (usually 2-3 character)
45  * names will be used.  SHORT indicates that short (usually single
46  * character) names will be used.  If there is no localization data
47  * available for either the SHORT or MEDIUM names, the other will be
48  * used, if neither is available, the PLURALIZED names will be used.
49  * Default is PLURALIZED.</li>
50  *
51  * <li><b>setCountVariant</b> controls how the count for the smallest
52  * unit will be formatted: either as an integer, a fraction to the
53  * smallest half, or as a decimal with 1, 2, or 3 decimal points.</li>
54  * Counts for higher units will be formatted as integers.
55  *
56  * </ul>
57  * @hide exposed on OHOS
58  */
59 public class BasicPeriodFormatterFactory implements PeriodFormatterFactory {
60   private final PeriodFormatterDataService ds;
61   private PeriodFormatterData data;
62   private Customizations customizations;
63   private boolean customizationsInUse;
64   private String localeName;
65 
66   // package-only constructor
BasicPeriodFormatterFactory(PeriodFormatterDataService ds)67   BasicPeriodFormatterFactory(PeriodFormatterDataService ds) {
68     this.ds = ds;
69     this.customizations = new Customizations();
70     this.localeName = Locale.getDefault().toString();
71   }
72 
73   /**
74    * Return the default rdf factory as a BasicPeriodFormatterFactory.
75    *
76    * @return a default BasicPeriodFormatterFactory
77    */
getDefault()78   public static BasicPeriodFormatterFactory getDefault() {
79       return (BasicPeriodFormatterFactory)
80         BasicPeriodFormatterService.getInstance().newPeriodFormatterFactory();
81   }
82 
83   /**
84    * Set the locale for this factory.
85    */
86   @Override
setLocale(String localeName)87   public PeriodFormatterFactory setLocale(String localeName) {
88     data = null;
89     this.localeName = localeName;
90     return this;
91   }
92 
93   /**
94    * Set whether limits will be displayed.
95    *
96    * @param display true if limits will be displayed
97    * @return this PeriodFormatterFactory
98    */
99   @Override
setDisplayLimit(boolean display)100   public PeriodFormatterFactory setDisplayLimit(boolean display) {
101     updateCustomizations().displayLimit = display;
102     return this;
103   }
104 
105   /**
106    * Return true if limits will be displayed.
107    *
108    * @return true if limits will be displayed
109    */
getDisplayLimit()110   public boolean getDisplayLimit() {
111     return customizations.displayLimit;
112   }
113 
114   /**
115    * Set whether past and future will be displayed.
116    *
117    * @param display true if past and future will be displayed
118    * @return this PeriodFormatterFactory
119    */
120   @Override
setDisplayPastFuture(boolean display)121   public PeriodFormatterFactory setDisplayPastFuture(boolean display) {
122     updateCustomizations().displayDirection = display;
123     return this;
124   }
125 
126   /**
127    * Return true if past and future will be displayed.
128    *
129    * @return true if past and future will be displayed
130    */
getDisplayPastFuture()131   public boolean getDisplayPastFuture() {
132     return customizations.displayDirection;
133   }
134 
135   /**
136    * Set how separators will be displayed.
137    *
138    * @param variant the variant indicating separators will be displayed
139    * @return this PeriodFormatterFactory
140    */
141   @Override
setSeparatorVariant(int variant)142   public PeriodFormatterFactory setSeparatorVariant(int variant) {
143     updateCustomizations().separatorVariant = (byte) variant;
144     return this;
145   }
146 
147   /**
148    * Return the variant indicating how separators will be displayed.
149    *
150    * @return the variant
151    */
getSeparatorVariant()152   public int getSeparatorVariant() {
153     return customizations.separatorVariant;
154   }
155 
156   /**
157    * Set the variant of the time unit names to use.
158    *
159    * @param variant the variant to use
160    * @return this PeriodFormatterFactory
161    */
162   @Override
setUnitVariant(int variant)163   public PeriodFormatterFactory setUnitVariant(int variant) {
164     updateCustomizations().unitVariant = (byte) variant;
165     return this;
166   }
167 
168   /**
169    * Return the unit variant.
170    *
171    * @return the unit variant
172    */
getUnitVariant()173   public int getUnitVariant() {
174     return customizations.unitVariant;
175   }
176 
177   /**
178    * Set the variant of the count to use.
179    *
180    * @param variant the variant to use
181    * @return this PeriodFormatterFactory
182    */
183   @Override
setCountVariant(int variant)184   public PeriodFormatterFactory setCountVariant(int variant) {
185     updateCustomizations().countVariant = (byte) variant;
186     return this;
187   }
188 
189   /**
190    * Return the count variant.
191    *
192    * @return the count variant
193    */
getCountVariant()194   public int getCountVariant() {
195     return customizations.countVariant;
196   }
197 
198   @Override
getFormatter()199   public PeriodFormatter getFormatter() {
200     customizationsInUse = true;
201     return new BasicPeriodFormatter(this, localeName, getData(),
202                                     customizations);
203   }
204 
updateCustomizations()205   private Customizations updateCustomizations() {
206     if (customizationsInUse) {
207       customizations = customizations.copy();
208       customizationsInUse = false;
209     }
210     return customizations;
211   }
212 
213   // package access only
getData()214   PeriodFormatterData getData() {
215     if (data == null) {
216       data = ds.get(localeName);
217     }
218     return data;
219   }
220 
221   // package access for use by BasicPeriodFormatter
getData(String locName)222   PeriodFormatterData getData(String locName) {
223     return ds.get(locName);
224   }
225 
226   // package access for use by BasicPeriodFormatter
227   static class Customizations {
228     boolean displayLimit = true;
229     boolean displayDirection = true;
230     byte separatorVariant = ESeparatorVariant.FULL;
231     byte unitVariant = EUnitVariant.PLURALIZED;
232     byte countVariant = ECountVariant.INTEGER;
233 
copy()234     public Customizations copy() {
235         Customizations result = new Customizations();
236         result.displayLimit = displayLimit;
237         result.displayDirection = displayDirection;
238         result.separatorVariant = separatorVariant;
239         result.unitVariant = unitVariant;
240         result.countVariant = countVariant;
241         return result;
242     }
243   }
244 }
245