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