1 /* GENERATED SOURCE. DO NOT MODIFY. */ 2 // © 2016 and later: Unicode, Inc. and others. 3 // License & terms of use: http://www.unicode.org/copyright.html#License 4 /* 5 ****************************************************************************** 6 * Copyright (C) 2007-2014, International Business Machines Corporation and * 7 * others. All Rights Reserved. * 8 ****************************************************************************** 9 */ 10 11 package ohos.global.icu.impl.duration; 12 13 import java.util.Date; 14 import java.util.TimeZone; 15 16 /** 17 * Core implementation class for DurationFormatter. 18 */ 19 class BasicDurationFormatter implements DurationFormatter { 20 private PeriodFormatter formatter; 21 private PeriodBuilder builder; 22 private DateFormatter fallback; 23 private long fallbackLimit; 24 private String localeName; 25 private TimeZone timeZone; 26 27 /** 28 * Creates a basic duration formatter with the given formatter, 29 * builder, and fallback. It's up to the caller to ensure that 30 * the locales and timezones of these are in sync. 31 */ BasicDurationFormatter(PeriodFormatter formatter, PeriodBuilder builder, DateFormatter fallback, long fallbackLimit)32 public BasicDurationFormatter(PeriodFormatter formatter, 33 PeriodBuilder builder, 34 DateFormatter fallback, 35 long fallbackLimit) { 36 this.formatter = formatter; 37 this.builder = builder; 38 this.fallback = fallback; 39 this.fallbackLimit = fallbackLimit < 0 ? 0 : fallbackLimit; 40 } 41 42 protected BasicDurationFormatter(PeriodFormatter formatter, 43 PeriodBuilder builder, 44 DateFormatter fallback, 45 long fallbackLimit, 46 String localeName, 47 TimeZone timeZone) { 48 this.formatter = formatter; 49 this.builder = builder; 50 this.fallback = fallback; 51 this.fallbackLimit = fallbackLimit; 52 this.localeName = localeName; 53 this.timeZone = timeZone; 54 } 55 56 @Override 57 public String formatDurationFromNowTo(Date targetDate) { 58 long now = System.currentTimeMillis(); 59 long duration = targetDate.getTime() - now; 60 return formatDurationFrom(duration, now); 61 } 62 63 @Override 64 public String formatDurationFromNow(long duration) { 65 return formatDurationFrom(duration, System.currentTimeMillis()); 66 } 67 68 @Override 69 public String formatDurationFrom(long duration, long referenceDate) { 70 String s = doFallback(duration, referenceDate); 71 if (s == null) { 72 Period p = doBuild(duration, referenceDate); 73 s = doFormat(p); 74 } 75 return s; 76 } 77 78 @Override 79 public DurationFormatter withLocale(String locName) { 80 if (!locName.equals(localeName)) { 81 PeriodFormatter newFormatter = formatter.withLocale(locName); 82 PeriodBuilder newBuilder = builder.withLocale(locName); 83 DateFormatter newFallback = fallback == null 84 ? null 85 : fallback.withLocale(locName); 86 return new BasicDurationFormatter(newFormatter, newBuilder, 87 newFallback, fallbackLimit, 88 locName, timeZone); 89 } 90 return this; 91 } 92 93 @Override 94 public DurationFormatter withTimeZone(TimeZone tz) { 95 if (!tz.equals(timeZone)) { 96 PeriodBuilder newBuilder = builder.withTimeZone(tz); 97 DateFormatter newFallback = fallback == null 98 ? null 99 : fallback.withTimeZone(tz); 100 return new BasicDurationFormatter(formatter, newBuilder, 101 newFallback, fallbackLimit, 102 localeName, tz); 103 } 104 return this; 105 } 106 107 protected String doFallback(long duration, long referenceDate) { 108 if (fallback != null 109 && fallbackLimit > 0 110 && Math.abs(duration) >= fallbackLimit) { 111 return fallback.format(referenceDate + duration); 112 } 113 return null; 114 } 115 doBuild(long duration, long referenceDate)116 protected Period doBuild(long duration, long referenceDate) { 117 return builder.createWithReferenceDate(duration, referenceDate); 118 } 119 doFormat(Period period)120 protected String doFormat(Period period) { 121 if (!period.isSet()) { 122 throw new IllegalArgumentException("period is not set"); 123 } 124 return formatter.format(period); 125 } 126 } 127