2 ******************************************************************************
3 * Copyright (C) 2007-2008, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 ******************************************************************************
8 package com.ibm.icu.impl.duration;
10 import java.util.Date;
11 import java.util.TimeZone;
14 * Core implementation class for DurationFormatter.
16 class BasicDurationFormatter implements DurationFormatter {
17 private PeriodFormatter formatter;
18 private PeriodBuilder builder;
19 private DateFormatter fallback;
20 private long fallbackLimit;
21 private String localeName;
22 private TimeZone timeZone;
25 * Creates a basic duration formatter with the given formatter,
26 * builder, and fallback. It's up to the caller to ensure that
27 * the locales and timezones of these are in sync.
29 public BasicDurationFormatter(PeriodFormatter formatter,
30 PeriodBuilder builder,
31 DateFormatter fallback,
33 this.formatter = formatter;
34 this.builder = builder;
35 this.fallback = fallback;
36 this.fallbackLimit = fallbackLimit < 0 ? 0 : fallbackLimit;
39 protected BasicDurationFormatter(PeriodFormatter formatter,
40 PeriodBuilder builder,
41 DateFormatter fallback,
45 this.formatter = formatter;
46 this.builder = builder;
47 this.fallback = fallback;
48 this.fallbackLimit = fallbackLimit;
49 this.localeName = localeName;
50 this.timeZone = timeZone;
53 public String formatDurationFromNowTo(Date targetDate) {
54 long now = System.currentTimeMillis();
55 long duration = now - targetDate.getTime();
56 return formatDurationFrom(duration, now);
59 public String formatDurationFromNow(long duration) {
60 return formatDurationFrom(duration, System.currentTimeMillis());
63 public String formatDurationFrom(long duration, long referenceDate) {
64 String s = doFallback(duration, referenceDate);
66 Period p = doBuild(duration, referenceDate);
72 public DurationFormatter withLocale(String locName) {
73 if (!locName.equals(localeName)) {
74 PeriodFormatter newFormatter = formatter.withLocale(locName);
75 PeriodBuilder newBuilder = builder.withLocale(locName);
76 DateFormatter newFallback = fallback == null
78 : fallback.withLocale(locName);
79 return new BasicDurationFormatter(newFormatter, newBuilder,
80 newFallback, fallbackLimit,
86 public DurationFormatter withTimeZone(TimeZone tz) {
87 if (!tz.equals(timeZone)) {
88 PeriodBuilder newBuilder = builder.withTimeZone(tz);
89 DateFormatter newFallback = fallback == null
91 : fallback.withTimeZone(tz);
92 return new BasicDurationFormatter(formatter, newBuilder,
93 newFallback, fallbackLimit,
99 protected String doFallback(long duration, long referenceDate) {
102 && Math.abs(duration) >= fallbackLimit) {
103 return fallback.format(referenceDate + duration);
108 protected Period doBuild(long duration, long referenceDate) {
109 return builder.createWithReferenceDate(duration, referenceDate);
112 protected String doFormat(Period period) {
113 if (!period.isSet()) {
114 throw new IllegalArgumentException("period is not set");
116 return formatter.format(period);