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