2 ******************************************************************************
3 * Copyright (C) 2007-2009, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 ******************************************************************************
8 package com.ibm.icu.impl.duration;
10 import java.util.Locale;
11 import java.util.TimeZone;
14 * Abstract factory object used to create DurationFormatters.
15 * DurationFormatters are immutable once created.
17 * Setters on the factory mutate the factory and return it,
20 * Subclasses override getFormatter to return a custom
23 class BasicDurationFormatterFactory implements DurationFormatterFactory {
24 private BasicPeriodFormatterService ps;
25 private PeriodFormatter formatter;
26 private PeriodBuilder builder;
27 private DateFormatter fallback;
28 private long fallbackLimit;
29 private String localeName;
30 private TimeZone timeZone;
31 private BasicDurationFormatter f; // cache
34 * Create a default formatter for the current locale and time zone.
36 BasicDurationFormatterFactory(BasicPeriodFormatterService ps) {
38 this.localeName = Locale.getDefault().toString();
39 this.timeZone = TimeZone.getDefault();
43 * Set the period formatter used by the factory. New formatters created
44 * with this factory will use the given period formatter.
46 * @return this BasicDurationFormatterFactory
48 public DurationFormatterFactory setPeriodFormatter(
49 PeriodFormatter formatter) {
50 if (formatter != this.formatter) {
51 this.formatter = formatter;
58 * Set the builder used by the factory. New formatters created
59 * with this factory will use the given locale.
61 * @param builder the builder to use
62 * @return this BasicDurationFormatterFactory
64 public DurationFormatterFactory setPeriodBuilder(PeriodBuilder builder) {
65 if (builder != this.builder) {
66 this.builder = builder;
73 * Set a fallback formatter for durations over a given limit.
75 * @param fallback the fallback formatter to use, or null
76 * @return this BasicDurationFormatterFactory
78 public DurationFormatterFactory setFallback(DateFormatter fallback) {
79 boolean doReset = fallback == null
80 ? this.fallback != null
81 : !fallback.equals(this.fallback);
83 this.fallback = fallback;
90 * Set a fallback limit for durations over a given limit.
92 * @param fallbackLimit the fallback limit to use, or 0 if none is desired.
93 * @return this BasicDurationFormatterFactory
95 public DurationFormatterFactory setFallbackLimit(long fallbackLimit) {
96 if (fallbackLimit < 0) {
99 if (fallbackLimit != this.fallbackLimit) {
100 this.fallbackLimit = fallbackLimit;
107 * Set the name of the locale that will be used when
108 * creating new formatters.
110 * @param localeName the name of the Locale
111 * @return this BasicDurationFormatterFactory
113 public DurationFormatterFactory setLocale(String localeName) {
114 if (!localeName.equals(this.localeName)) {
115 this.localeName = localeName;
116 if (builder != null) {
117 builder = builder.withLocale(localeName);
119 if (formatter != null) {
120 formatter = formatter.withLocale(localeName);
128 * Set the name of the locale that will be used when
129 * creating new formatters.
131 * @param timeZone The time zone to use.
132 * @return this BasicDurationFormatterFactory
134 public DurationFormatterFactory setTimeZone(TimeZone timeZone) {
135 if (!timeZone.equals(this.timeZone)) {
136 this.timeZone = timeZone;
137 if (builder != null) {
138 builder = builder.withTimeZone(timeZone);
146 * Return a formatter based on this factory's current settings.
148 * @return a BasicDurationFormatter
150 public DurationFormatter getFormatter() {
152 if (fallback != null) {
153 fallback = fallback.withLocale(localeName).withTimeZone(timeZone);
155 formatter = getPeriodFormatter();
156 builder = getPeriodBuilder();
158 f = createFormatter();
164 * Return the current period formatter.
166 * @return the current period formatter
168 public PeriodFormatter getPeriodFormatter() {
169 if (formatter == null) {
170 formatter = ps.newPeriodFormatterFactory()
171 .setLocale(localeName)
178 * Return the current builder.
180 * @return the current builder
182 public PeriodBuilder getPeriodBuilder() {
183 if (builder == null) {
184 builder = ps.newPeriodBuilderFactory()
185 .setLocale(localeName)
186 .setTimeZone(timeZone)
187 .getSingleUnitBuilder();
193 * Return the current fallback formatter.
195 * @return the fallback formatter, or null if there is no fallback
198 public DateFormatter getFallback() {
203 * Return the current fallback formatter limit
205 * @return the limit, or 0 if there is no fallback.
207 public long getFallbackLimit() {
208 return fallback == null ? 0 : fallbackLimit;
212 * Return the current locale name.
214 * @return the current locale name
216 public String getLocaleName() {
221 * Return the current locale name.
223 * @return the current locale name
225 public TimeZone getTimeZone() {
230 * Create the formatter. All local fields are already initialized.
232 protected BasicDurationFormatter createFormatter() {
233 return new BasicDurationFormatter(formatter, builder, fallback,
234 fallbackLimit, localeName,
239 * Clear the cached formatter. Subclasses must call this if their
240 * state has changed. This is automatically invoked by setBuilder,
241 * setFormatter, setFallback, setLocaleName, and setTimeZone
243 protected void reset() {