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