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