]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_8_1_1/main/classes/core/src/com/ibm/icu/impl/duration/BasicDurationFormatter.java
Added flags.
[Dictionary.git] / jars / icu4j-4_8_1_1 / main / classes / core / src / com / ibm / icu / impl / duration / BasicDurationFormatter.java
1 /*
2 ******************************************************************************
3 * Copyright (C) 2007-2008, International Business Machines Corporation and   *
4 * others. All Rights Reserved.                                               *
5 ******************************************************************************
6 */
7
8 package com.ibm.icu.impl.duration;
9
10 import java.util.Date;
11 import java.util.TimeZone;
12
13 /**
14  * Core implementation class for DurationFormatter.
15  */
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;
23
24   /**
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.
28    */
29   public BasicDurationFormatter(PeriodFormatter formatter,
30                                 PeriodBuilder builder, 
31                                 DateFormatter fallback,
32                                 long fallbackLimit) {
33     this.formatter = formatter;
34     this.builder = builder;
35     this.fallback = fallback;
36     this.fallbackLimit = fallbackLimit < 0 ? 0 : fallbackLimit;
37   }
38
39   protected BasicDurationFormatter(PeriodFormatter formatter,
40                                    PeriodBuilder builder, 
41                                    DateFormatter fallback,
42                                    long fallbackLimit,
43                                    String localeName,
44                                    TimeZone timeZone) {
45     this.formatter = formatter;
46     this.builder = builder;
47     this.fallback = fallback;
48     this.fallbackLimit = fallbackLimit;
49     this.localeName = localeName;
50     this.timeZone = timeZone;
51   }
52
53   public String formatDurationFromNowTo(Date targetDate) {
54     long now = System.currentTimeMillis();
55     long duration = now - targetDate.getTime();
56     return formatDurationFrom(duration, now);
57   }
58
59   public String formatDurationFromNow(long duration) {
60     return formatDurationFrom(duration, System.currentTimeMillis());
61   }
62
63   public String formatDurationFrom(long duration, long referenceDate) {
64     String s = doFallback(duration, referenceDate);
65     if (s == null) {
66       Period p = doBuild(duration, referenceDate);
67       s = doFormat(p);
68     }
69     return s;
70   }
71
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 
77           ? null 
78           : fallback.withLocale(locName);
79       return new BasicDurationFormatter(newFormatter, newBuilder,
80                                         newFallback, fallbackLimit,
81                                         locName, timeZone);
82     }
83     return this;
84   }
85
86   public DurationFormatter withTimeZone(TimeZone tz) {
87     if (!tz.equals(timeZone)) {
88       PeriodBuilder newBuilder = builder.withTimeZone(tz);
89       DateFormatter newFallback = fallback == null 
90           ? null 
91           : fallback.withTimeZone(tz);
92       return new BasicDurationFormatter(formatter, newBuilder,
93                                         newFallback, fallbackLimit,
94                                         localeName, tz);
95     }
96     return this;
97   }
98
99   protected String doFallback(long duration, long referenceDate) {
100     if (fallback != null 
101         && fallbackLimit > 0
102         && Math.abs(duration) >= fallbackLimit) {
103       return fallback.format(referenceDate + duration);
104     }
105     return null;
106   }
107
108   protected Period doBuild(long duration, long referenceDate) {
109     return builder.createWithReferenceDate(duration, referenceDate);
110   }
111
112   protected String doFormat(Period period) {
113     if (!period.isSet()) {
114       throw new IllegalArgumentException("period is not set");
115     }
116     return formatter.format(period);
117   }
118 }