2 ******************************************************************************
\r
3 * Copyright (C) 2007-2009, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 ******************************************************************************
\r
8 package com.ibm.icu.impl.duration;
\r
10 import java.util.Locale;
\r
11 import java.util.TimeZone;
\r
14 * Abstract factory object used to create DurationFormatters.
\r
15 * DurationFormatters are immutable once created.
\r
17 * Setters on the factory mutate the factory and return it,
\r
20 * Subclasses override getFormatter to return a custom
\r
21 * DurationFormatter.
\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
34 * Create a default formatter for the current locale and time zone.
\r
36 BasicDurationFormatterFactory(BasicPeriodFormatterService ps) {
\r
38 this.localeName = Locale.getDefault().toString();
\r
39 this.timeZone = TimeZone.getDefault();
\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
46 * @return this BasicDurationFormatterFactory
\r
48 public DurationFormatterFactory setPeriodFormatter(
\r
49 PeriodFormatter formatter) {
\r
50 if (formatter != this.formatter) {
\r
51 this.formatter = formatter;
\r
58 * Set the builder used by the factory. New formatters created
\r
59 * with this factory will use the given locale.
\r
61 * @param builder the builder to use
\r
62 * @return this BasicDurationFormatterFactory
\r
64 public DurationFormatterFactory setPeriodBuilder(PeriodBuilder builder) {
\r
65 if (builder != this.builder) {
\r
66 this.builder = builder;
\r
73 * Set a fallback formatter for durations over a given limit.
\r
75 * @param fallback the fallback formatter to use, or null
\r
76 * @return this BasicDurationFormatterFactory
\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
83 this.fallback = fallback;
\r
90 * Set a fallback limit for durations over a given limit.
\r
92 * @param fallbackLimit the fallback limit to use, or 0 if none is desired.
\r
93 * @return this BasicDurationFormatterFactory
\r
95 public DurationFormatterFactory setFallbackLimit(long fallbackLimit) {
\r
96 if (fallbackLimit < 0) {
\r
99 if (fallbackLimit != this.fallbackLimit) {
\r
100 this.fallbackLimit = fallbackLimit;
\r
107 * Set the name of the locale that will be used when
\r
108 * creating new formatters.
\r
110 * @param localeName the name of the Locale
\r
111 * @return this BasicDurationFormatterFactory
\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
119 if (formatter != null) {
\r
120 formatter = formatter.withLocale(localeName);
\r
128 * Set the name of the locale that will be used when
\r
129 * creating new formatters.
\r
131 * @param timeZone The time zone to use.
\r
132 * @return this BasicDurationFormatterFactory
\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
146 * Return a formatter based on this factory's current settings.
\r
148 * @return a BasicDurationFormatter
\r
150 public DurationFormatter getFormatter() {
\r
152 if (fallback != null) {
\r
153 fallback = fallback.withLocale(localeName).withTimeZone(timeZone);
\r
155 formatter = getPeriodFormatter();
\r
156 builder = getPeriodBuilder();
\r
158 f = createFormatter();
\r
164 * Return the current period formatter.
\r
166 * @return the current period formatter
\r
168 public PeriodFormatter getPeriodFormatter() {
\r
169 if (formatter == null) {
\r
170 formatter = ps.newPeriodFormatterFactory()
\r
171 .setLocale(localeName)
\r
178 * Return the current builder.
\r
180 * @return the current builder
\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
193 * Return the current fallback formatter.
\r
195 * @return the fallback formatter, or null if there is no fallback
\r
198 public DateFormatter getFallback() {
\r
203 * Return the current fallback formatter limit
\r
205 * @return the limit, or 0 if there is no fallback.
\r
207 public long getFallbackLimit() {
\r
208 return fallback == null ? 0 : fallbackLimit;
\r
212 * Return the current locale name.
\r
214 * @return the current locale name
\r
216 public String getLocaleName() {
\r
221 * Return the current locale name.
\r
223 * @return the current locale name
\r
225 public TimeZone getTimeZone() {
\r
230 * Create the formatter. All local fields are already initialized.
\r
232 protected BasicDurationFormatter createFormatter() {
\r
233 return new BasicDurationFormatter(formatter, builder, fallback,
\r
234 fallbackLimit, localeName,
\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
243 protected void reset() {
\r