2 ******************************************************************************
3 * Copyright (C) 2007-2010, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 ******************************************************************************
8 package com.ibm.icu.impl.duration;
10 import java.util.Locale;
12 import com.ibm.icu.impl.duration.impl.DataRecord.ECountVariant;
13 import com.ibm.icu.impl.duration.impl.DataRecord.ESeparatorVariant;
14 import com.ibm.icu.impl.duration.impl.DataRecord.EUnitVariant;
15 import com.ibm.icu.impl.duration.impl.PeriodFormatterData;
16 import com.ibm.icu.impl.duration.impl.PeriodFormatterDataService;
19 * An implementation of PeriodFormatterFactory that provides customization of
20 * formatting behavior. Instances of this factory are created by
21 * BasicPeriodFormatterService.
23 * The settings on BasicPeriodFormatterFactory are:
26 * <li><b>setDisplayLimit</b> controls whether phrases like 'more than'
27 * or 'less than' will be displayed when the Period has a defined
28 * limit. Default is to display them.</li>
30 * <li><b>setDisplayPastFuture</b> controls whether phrases like 'ago'
31 * or 'from now' will be displayed to indicate past or future
32 * time. Default is to display them.</li>
34 * <li><b>setSeparatorVariant</b> controls how separators (between
35 * count and period, and multiple periods) will be displayed, when
36 * appropriate for the language. Default is to use full
39 * <li><b>setUnitVariant</b> controls which of various types of
40 * unit names to use. PLURALIZED indicates that full names will be
41 * used. MEDIUM indicates that medium-length (usually 2-3 character)
42 * names will be used. SHORT indicates that short (usually single
43 * character) names will be used. If there is no localization data
44 * available for either the SHORT or MEDIUM names, the other will be
45 * used, if neither is available, the PLURALIZED names will be used.
46 * Default is PLURALIZED.</li>
48 * <li><b>setCountVariant</b> controls how the count for the smallest
49 * unit will be formatted: either as an integer, a fraction to the
50 * smallest half, or as a decimal with 1, 2, or 3 decimal points.</li>
51 * Counts for higher units will be formatted as integers.
55 public class BasicPeriodFormatterFactory implements PeriodFormatterFactory {
56 private final PeriodFormatterDataService ds;
57 private PeriodFormatterData data;
58 private Customizations customizations;
59 private boolean customizationsInUse;
60 private String localeName;
62 // package-only constructor
63 BasicPeriodFormatterFactory(PeriodFormatterDataService ds) {
65 this.customizations = new Customizations();
66 this.localeName = Locale.getDefault().toString();
70 * Return the default rdf factory as a BasicPeriodFormatterFactory.
72 * @return a default BasicPeriodFormatterFactory
74 public static BasicPeriodFormatterFactory getDefault() {
75 return (BasicPeriodFormatterFactory)
76 BasicPeriodFormatterService.getInstance().newPeriodFormatterFactory();
80 * Set the locale for this factory.
82 public PeriodFormatterFactory setLocale(String localeName) {
84 this.localeName = localeName;
89 * Set whether limits will be displayed.
91 * @param display true if limits will be displayed
92 * @return this PeriodFormatterFactory
94 public PeriodFormatterFactory setDisplayLimit(boolean display) {
95 updateCustomizations().displayLimit = display;
100 * Return true if limits will be displayed.
102 * @return true if limits will be displayed
104 public boolean getDisplayLimit() {
105 return customizations.displayLimit;
109 * Set whether past and future will be displayed.
111 * @param display true if past and future will be displayed
112 * @return this PeriodFormatterFactory
114 public PeriodFormatterFactory setDisplayPastFuture(boolean display) {
115 updateCustomizations().displayDirection = display;
120 * Return true if past and future will be displayed.
122 * @return true if past and future will be displayed
124 public boolean getDisplayPastFuture() {
125 return customizations.displayDirection;
129 * Set how separators will be displayed.
131 * @param variant the variant indicating separators will be displayed
132 * @return this PeriodFormatterFactory
134 public PeriodFormatterFactory setSeparatorVariant(int variant) {
135 updateCustomizations().separatorVariant = (byte) variant;
140 * Return the variant indicating how separators will be displayed.
142 * @return the variant
144 public int getSeparatorVariant() {
145 return customizations.separatorVariant;
149 * Set the variant of the time unit names to use.
151 * @param variant the variant to use
152 * @return this PeriodFormatterFactory
154 public PeriodFormatterFactory setUnitVariant(int variant) {
155 updateCustomizations().unitVariant = (byte) variant;
160 * Return the unit variant.
162 * @return the unit variant
164 public int getUnitVariant() {
165 return customizations.unitVariant;
169 * Set the variant of the count to use.
171 * @param variant the variant to use
172 * @return this PeriodFormatterFactory
174 public PeriodFormatterFactory setCountVariant(int variant) {
175 updateCustomizations().countVariant = (byte) variant;
180 * Return the count variant.
182 * @return the count variant
184 public int getCountVariant() {
185 return customizations.countVariant;
188 public PeriodFormatter getFormatter() {
189 customizationsInUse = true;
190 return new BasicPeriodFormatter(this, localeName, getData(),
194 private Customizations updateCustomizations() {
195 if (customizationsInUse) {
196 customizations = customizations.copy();
197 customizationsInUse = false;
199 return customizations;
202 // package access only
203 PeriodFormatterData getData() {
205 data = ds.get(localeName);
210 // package access for use by BasicPeriodFormatter
211 PeriodFormatterData getData(String locName) {
212 return ds.get(locName);
215 // package access for use by BasicPeriodFormatter
216 static class Customizations {
217 boolean displayLimit = true;
218 boolean displayDirection = true;
219 byte separatorVariant = ESeparatorVariant.FULL;
220 byte unitVariant = EUnitVariant.PLURALIZED;
221 byte countVariant = ECountVariant.INTEGER;
223 public Customizations copy() {
224 Customizations result = new Customizations();
225 result.displayLimit = displayLimit;
226 result.displayDirection = displayDirection;
227 result.separatorVariant = separatorVariant;
228 result.unitVariant = unitVariant;
229 result.countVariant = countVariant;