summaryrefslogtreecommitdiffstats
path: root/android_icu4j/src/main/java/android/icu/impl/duration/BasicDurationFormatterFactory.java
blob: 5c6939858ef7965411f5cf5f5a0ad821ebeca5f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* GENERATED SOURCE. DO NOT MODIFY. */
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html#License
/*
******************************************************************************
* Copyright (C) 2007-2009, International Business Machines Corporation and   *
* others. All Rights Reserved.                                               *
******************************************************************************
*/

package android.icu.impl.duration;

import java.util.Locale;
import java.util.TimeZone;

/**
 * Abstract factory object used to create DurationFormatters.
 * DurationFormatters are immutable once created.
 * <p>
 * Setters on the factory mutate the factory and return it,
 * for chaining.
 * <p>
 * Subclasses override getFormatter to return a custom
 * DurationFormatter.
 */
class BasicDurationFormatterFactory implements DurationFormatterFactory {
  private BasicPeriodFormatterService ps;
  private PeriodFormatter formatter;
  private PeriodBuilder builder;
  private DateFormatter fallback;
  private long fallbackLimit;
  private String localeName;
  private TimeZone timeZone;
  private BasicDurationFormatter f; // cache

  /**
   * Create a default formatter for the current locale and time zone.
   */
  BasicDurationFormatterFactory(BasicPeriodFormatterService ps) {
    this.ps = ps;
    this.localeName = Locale.getDefault().toString();
    this.timeZone = TimeZone.getDefault();
  }

  /**
   * Set the period formatter used by the factory.  New formatters created
   * with this factory will use the given period formatter.
   *
   * @return this BasicDurationFormatterFactory
   */
  @Override
  public DurationFormatterFactory setPeriodFormatter(
      PeriodFormatter formatter) {
    if (formatter != this.formatter) {
      this.formatter = formatter;
      reset();
    }
    return this;
  }

  /**
   * Set the builder used by the factory.  New formatters created
   * with this factory will use the given locale.
   *
   * @param builder the builder to use
   * @return this BasicDurationFormatterFactory
   */
  @Override
  public DurationFormatterFactory setPeriodBuilder(PeriodBuilder builder) {
    if (builder != this.builder) {
      this.builder = builder;
      reset();
    }
    return this;
  }

  /**
   * Set a fallback formatter for durations over a given limit.
   *
   * @param fallback the fallback formatter to use, or null
   * @return this BasicDurationFormatterFactory
   */
  @Override
  public DurationFormatterFactory setFallback(DateFormatter fallback) {
    boolean doReset = fallback == null
        ? this.fallback != null
        : !fallback.equals(this.fallback);
    if (doReset) {
      this.fallback = fallback;
      reset();
    }
    return this;
  }

  /**
   * Set a fallback limit for durations over a given limit.
   *
   * @param fallbackLimit the fallback limit to use, or 0 if none is desired.
   * @return this BasicDurationFormatterFactory
   */
  @Override
  public DurationFormatterFactory setFallbackLimit(long fallbackLimit) {
    if (fallbackLimit < 0) {
      fallbackLimit = 0;
    }
    if (fallbackLimit != this.fallbackLimit) {
      this.fallbackLimit = fallbackLimit;
      reset();
    }
    return this;
  }

  /**
   * Set the name of the locale that will be used when
   * creating new formatters.
   *
   * @param localeName the name of the Locale
   * @return this BasicDurationFormatterFactory
   */
  @Override
  public DurationFormatterFactory setLocale(String localeName) {
    if (!localeName.equals(this.localeName)) {
      this.localeName = localeName;
      if (builder != null) {
          builder = builder.withLocale(localeName);
      }
      if (formatter != null) {
          formatter = formatter.withLocale(localeName);
      }
      reset();
    }
    return this;
  }

  /**
   * Set the name of the locale that will be used when
   * creating new formatters.
   *
   * @param timeZone The time zone to use.
   * @return this BasicDurationFormatterFactory
   */
  @Override
  public DurationFormatterFactory setTimeZone(TimeZone timeZone) {
    if (!timeZone.equals(this.timeZone)) {
      this.timeZone = timeZone;
      if (builder != null) {
          builder = builder.withTimeZone(timeZone);
      }
      reset();
    }
    return this;
  }

  /**
   * Return a formatter based on this factory's current settings.
   *
   * @return a BasicDurationFormatter
   */
  @Override
  public DurationFormatter getFormatter() {
    if (f == null) {
      if (fallback != null) {
        fallback = fallback.withLocale(localeName).withTimeZone(timeZone);
      }
      formatter = getPeriodFormatter();
      builder = getPeriodBuilder();

      f = createFormatter();
    }
    return f;
  }

  /**
   * Return the current period formatter.
   *
   * @return the current period formatter
   */
  public PeriodFormatter getPeriodFormatter() {
    if (formatter == null) {
      formatter = ps.newPeriodFormatterFactory()
          .setLocale(localeName)
          .getFormatter();
    }
    return formatter;
  }

  /**
   * Return the current builder.
   *
   * @return the current builder
   */
  public PeriodBuilder getPeriodBuilder() {
    if (builder == null) {
      builder = ps.newPeriodBuilderFactory()
          .setLocale(localeName)
          .setTimeZone(timeZone)
          .getSingleUnitBuilder();
    }
    return builder;
  }

  /**
   * Return the current fallback formatter.
   *
   * @return the fallback formatter, or null if there is no fallback
   * formatter
   */
  public DateFormatter getFallback() {
    return fallback;
  }

  /**
   * Return the current fallback formatter limit
   *
   * @return the limit, or 0 if there is no fallback.
   */
  public long getFallbackLimit() {
    return fallback == null ? 0 : fallbackLimit;
  }

  /**
   * Return the current locale name.
   *
   * @return the current locale name
   */
  public String getLocaleName() {
    return localeName;
  }

  /**
   * Return the current locale name.
   *
   * @return the current locale name
   */
  public TimeZone getTimeZone() {
    return timeZone;
  }

  /**
   * Create the formatter.  All local fields are already initialized.
   */
  protected BasicDurationFormatter createFormatter() {
    return new BasicDurationFormatter(formatter, builder, fallback,
                                      fallbackLimit, localeName,
                                      timeZone);
  }

  /**
   * Clear the cached formatter.  Subclasses must call this if their
   * state has changed. This is automatically invoked by setBuilder,
   * setFormatter, setFallback, setLocaleName, and setTimeZone
   */
  protected void reset() {
    f = null;
  }
}