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 * @param builder the builder to use
\r
47 * @return this BasicDurationFormatterFactory
\r
49 public DurationFormatterFactory setPeriodFormatter(
\r
50 PeriodFormatter formatter) {
\r
51 if (formatter != this.formatter) {
\r
52 this.formatter = formatter;
\r
59 * Set the builder used by the factory. New formatters created
\r
60 * with this factory will use the given locale.
\r
62 * @param builder the builder to use
\r
63 * @return this BasicDurationFormatterFactory
\r
65 public DurationFormatterFactory setPeriodBuilder(PeriodBuilder builder) {
\r
66 if (builder != this.builder) {
\r
67 this.builder = builder;
\r
74 * Set a fallback formatter for durations over a given limit.
\r
76 * @param fallback the fallback formatter to use, or null
\r
77 * @return this BasicDurationFormatterFactory
\r
79 public DurationFormatterFactory setFallback(DateFormatter fallback) {
\r
80 boolean doReset = fallback == null
\r
81 ? this.fallback != null
\r
82 : !fallback.equals(this.fallback);
\r
84 this.fallback = fallback;
\r
91 * Set a fallback limit for durations over a given limit.
\r
93 * @param fallbackLimit the fallback limit to use, or 0 if none is desired.
\r
94 * @return this BasicDurationFormatterFactory
\r
96 public DurationFormatterFactory setFallbackLimit(long fallbackLimit) {
\r
97 if (fallbackLimit < 0) {
\r
100 if (fallbackLimit != this.fallbackLimit) {
\r
101 this.fallbackLimit = fallbackLimit;
\r
108 * Set the name of the locale that will be used when
\r
109 * creating new formatters.
\r
111 * @param localeName the name of the Locale
\r
112 * @return this BasicDurationFormatterFactory
\r
114 public DurationFormatterFactory setLocale(String localeName) {
\r
115 if (!localeName.equals(this.localeName)) {
\r
116 this.localeName = localeName;
\r
117 if (builder != null) {
\r
118 builder = builder.withLocale(localeName);
\r
120 if (formatter != null) {
\r
121 formatter = formatter.withLocale(localeName);
\r
129 * Set the name of the locale that will be used when
\r
130 * creating new formatters.
\r
132 * @param localeName the name of the Locale
\r
133 * @return this BasicDurationFormatterFactory
\r
135 public DurationFormatterFactory setTimeZone(TimeZone timeZone) {
\r
136 if (!timeZone.equals(this.timeZone)) {
\r
137 this.timeZone = timeZone;
\r
138 if (builder != null) {
\r
139 builder = builder.withTimeZone(timeZone);
\r
147 * Return a formatter based on this factory's current settings.
\r
149 * @return a BasicDurationFormatter
\r
151 public DurationFormatter getFormatter() {
\r
153 if (fallback != null) {
\r
154 fallback = fallback.withLocale(localeName).withTimeZone(timeZone);
\r
156 formatter = getPeriodFormatter();
\r
157 builder = getPeriodBuilder();
\r
159 f = createFormatter();
\r
165 * Return the current period formatter.
\r
167 * @return the current period formatter
\r
169 public PeriodFormatter getPeriodFormatter() {
\r
170 if (formatter == null) {
\r
171 formatter = ps.newPeriodFormatterFactory()
\r
172 .setLocale(localeName)
\r
179 * Return the current builder.
\r
181 * @return the current builder
\r
183 public PeriodBuilder getPeriodBuilder() {
\r
184 if (builder == null) {
\r
185 builder = ps.newPeriodBuilderFactory()
\r
186 .setLocale(localeName)
\r
187 .setTimeZone(timeZone)
\r
188 .getSingleUnitBuilder();
\r
194 * Return the current fallback formatter.
\r
196 * @return the fallback formatter, or null if there is no fallback
\r
199 public DateFormatter getFallback() {
\r
204 * Return the current fallback formatter limit
\r
206 * @return the limit, or 0 if there is no fallback.
\r
208 public long getFallbackLimit() {
\r
209 return fallback == null ? 0 : fallbackLimit;
\r
213 * Return the current locale name.
\r
215 * @return the current locale name
\r
217 public String getLocaleName() {
\r
222 * Return the current locale name.
\r
224 * @return the current locale name
\r
226 public TimeZone getTimeZone() {
\r
231 * Create the formatter. All local fields are already initialized.
\r
233 protected BasicDurationFormatter createFormatter() {
\r
234 return new BasicDurationFormatter(formatter, builder, fallback,
\r
235 fallbackLimit, localeName,
\r
240 * Clear the cached formatter. Subclasses must call this if their
\r
241 * state has changed. This is automatically invoked by setBuilder,
\r
242 * setFormatter, setFallback, setLocaleName, and setTimeZone
\r
244 protected void reset() {
\r