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