]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/impl/duration/BasicDurationFormatterFactory.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / impl / duration / BasicDurationFormatterFactory.java
1 /*\r
2 ******************************************************************************\r
3 * Copyright (C) 2007-2009, 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 java.util.Locale;\r
11 import java.util.TimeZone;\r
12 \r
13 /**\r
14  * Abstract factory object used to create DurationFormatters.\r
15  * DurationFormatters are immutable once created.\r
16  * <p>\r
17  * Setters on the factory mutate the factory and return it,\r
18  * for chaining.\r
19  * <p>\r
20  * Subclasses override getFormatter to return a custom\r
21  * DurationFormatter.\r
22  */\r
23 class BasicDurationFormatterFactory implements DurationFormatterFactory {\r
24   private BasicPeriodFormatterService ps;\r
25   private PeriodFormatter formatter;\r
26   private PeriodBuilder builder;\r
27   private DateFormatter fallback;\r
28   private long fallbackLimit;\r
29   private String localeName;\r
30   private TimeZone timeZone;\r
31   private BasicDurationFormatter f; // cache\r
32 \r
33   /**\r
34    * Create a default formatter for the current locale and time zone.\r
35    */\r
36   BasicDurationFormatterFactory(BasicPeriodFormatterService ps) {\r
37     this.ps = ps;\r
38     this.localeName = Locale.getDefault().toString();\r
39     this.timeZone = TimeZone.getDefault();\r
40   }\r
41 \r
42   /**\r
43    * Set the period formatter used by the factory.  New formatters created\r
44    * with this factory will use the given period formatter.\r
45    *\r
46    * @param builder the builder to use\r
47    * @return this BasicDurationFormatterFactory\r
48    */\r
49   public DurationFormatterFactory setPeriodFormatter(\r
50       PeriodFormatter formatter) {\r
51     if (formatter != this.formatter) {\r
52       this.formatter = formatter;\r
53       reset();\r
54     }\r
55     return this;\r
56   }\r
57 \r
58   /**\r
59    * Set the builder used by the factory.  New formatters created\r
60    * with this factory will use the given locale.\r
61    *\r
62    * @param builder the builder to use\r
63    * @return this BasicDurationFormatterFactory\r
64    */\r
65   public DurationFormatterFactory setPeriodBuilder(PeriodBuilder builder) {\r
66     if (builder != this.builder) {\r
67       this.builder = builder;\r
68       reset();\r
69     }\r
70     return this;\r
71   }\r
72 \r
73   /**\r
74    * Set a fallback formatter for durations over a given limit.\r
75    *\r
76    * @param fallback the fallback formatter to use, or null\r
77    * @return this BasicDurationFormatterFactory\r
78    */\r
79   public DurationFormatterFactory setFallback(DateFormatter fallback) {\r
80     boolean doReset = fallback == null\r
81         ? this.fallback != null\r
82         : !fallback.equals(this.fallback);\r
83     if (doReset) {\r
84       this.fallback = fallback;\r
85       reset();\r
86     }\r
87     return this;\r
88   }\r
89 \r
90   /**\r
91    * Set a fallback limit for durations over a given limit.\r
92    *\r
93    * @param fallbackLimit the fallback limit to use, or 0 if none is desired.\r
94    * @return this BasicDurationFormatterFactory\r
95    */\r
96   public DurationFormatterFactory setFallbackLimit(long fallbackLimit) {\r
97     if (fallbackLimit < 0) {\r
98       fallbackLimit = 0;\r
99     }\r
100     if (fallbackLimit != this.fallbackLimit) {\r
101       this.fallbackLimit = fallbackLimit;\r
102       reset();\r
103     }\r
104     return this;\r
105   }\r
106 \r
107   /**\r
108    * Set the name of the locale that will be used when \r
109    * creating new formatters.\r
110    *\r
111    * @param localeName the name of the Locale\r
112    * @return this BasicDurationFormatterFactory\r
113    */\r
114   public DurationFormatterFactory setLocale(String localeName) {\r
115     if (!localeName.equals(this.localeName)) {\r
116       this.localeName = localeName;\r
117       if (builder != null) {\r
118           builder = builder.withLocale(localeName);\r
119       }\r
120       if (formatter != null) {\r
121           formatter = formatter.withLocale(localeName);\r
122       }\r
123       reset();\r
124     }\r
125     return this;\r
126   }\r
127 \r
128   /**\r
129    * Set the name of the locale that will be used when \r
130    * creating new formatters.\r
131    *\r
132    * @param localeName the name of the Locale\r
133    * @return this BasicDurationFormatterFactory\r
134    */\r
135   public DurationFormatterFactory setTimeZone(TimeZone timeZone) {\r
136     if (!timeZone.equals(this.timeZone)) {\r
137       this.timeZone = timeZone;\r
138       if (builder != null) {\r
139           builder = builder.withTimeZone(timeZone);\r
140       }\r
141       reset();\r
142     }\r
143     return this;\r
144   }\r
145 \r
146   /**\r
147    * Return a formatter based on this factory's current settings.\r
148    *\r
149    * @return a BasicDurationFormatter\r
150    */\r
151   public DurationFormatter getFormatter() {\r
152     if (f == null) {\r
153       if (fallback != null) {\r
154         fallback = fallback.withLocale(localeName).withTimeZone(timeZone);\r
155       }\r
156       formatter = getPeriodFormatter();\r
157       builder = getPeriodBuilder();\r
158 \r
159       f = createFormatter();\r
160     }\r
161     return f;\r
162   }\r
163 \r
164   /**\r
165    * Return the current period formatter.\r
166    *\r
167    * @return the current period formatter\r
168    */\r
169   public PeriodFormatter getPeriodFormatter() {\r
170     if (formatter == null) {\r
171       formatter = ps.newPeriodFormatterFactory()\r
172           .setLocale(localeName)\r
173           .getFormatter();\r
174     }\r
175     return formatter;\r
176   }\r
177 \r
178   /**\r
179    * Return the current builder.\r
180    *\r
181    * @return the current builder\r
182    */\r
183   public PeriodBuilder getPeriodBuilder() {\r
184     if (builder == null) {\r
185       builder = ps.newPeriodBuilderFactory()\r
186           .setLocale(localeName)\r
187           .setTimeZone(timeZone)\r
188           .getSingleUnitBuilder();\r
189     }\r
190     return builder;\r
191   }\r
192 \r
193   /**\r
194    * Return the current fallback formatter.\r
195    *\r
196    * @return the fallback formatter, or null if there is no fallback\r
197    * formatter\r
198    */\r
199   public DateFormatter getFallback() {\r
200     return fallback;\r
201   }\r
202 \r
203   /**\r
204    * Return the current fallback formatter limit\r
205    *\r
206    * @return the limit, or 0 if there is no fallback.\r
207    */\r
208   public long getFallbackLimit() {\r
209     return fallback == null ? 0 : fallbackLimit;\r
210   }\r
211 \r
212   /**\r
213    * Return the current locale name.\r
214    *\r
215    * @return the current locale name\r
216    */\r
217   public String getLocaleName() {\r
218     return localeName;\r
219   }\r
220 \r
221   /**\r
222    * Return the current locale name.\r
223    *\r
224    * @return the current locale name\r
225    */\r
226   public TimeZone getTimeZone() {\r
227     return timeZone;\r
228   }\r
229 \r
230   /**\r
231    * Create the formatter.  All local fields are already initialized.\r
232    */\r
233   protected BasicDurationFormatter createFormatter() {\r
234     return new BasicDurationFormatter(formatter, builder, fallback, \r
235                                       fallbackLimit, localeName,\r
236                                       timeZone);\r
237   }\r
238 \r
239   /**\r
240    * Clear the cached formatter.  Subclasses must call this if their\r
241    * state has changed. This is automatically invoked by setBuilder,\r
242    * setFormatter, setFallback, setLocaleName, and setTimeZone\r
243    */\r
244   protected void reset() {\r
245     f = null;\r
246   }\r
247 }\r