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