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 com.ibm.icu.impl.duration.impl.PeriodFormatterData;
\r
11 import com.ibm.icu.impl.duration.impl.PeriodFormatterDataService;
\r
13 import com.ibm.icu.impl.duration.impl.DataRecord.EUnitVariant;
\r
14 import com.ibm.icu.impl.duration.impl.DataRecord.ECountVariant;
\r
15 import com.ibm.icu.impl.duration.impl.DataRecord.ESeparatorVariant;
\r
17 import java.util.Locale;
\r
20 * An implementation of PeriodFormatterFactory that provides customization of
\r
21 * formatting behavior. Instances of this factory are created by
\r
22 * BasicPeriodFormatterService.
\r
24 * The settings on BasicPeriodFormatterFactory are:
\r
27 * <li><b>setDisplayLimit</b> controls whether phrases like 'more than'
\r
28 * or 'less than' will be displayed when the Period has a defined
\r
29 * limit. Default is to display them.</li>
\r
31 * <li><b>setDisplayPastFuture</b> controls whether phrases like 'ago'
\r
32 * or 'from now' will be displayed to indicate past or future
\r
33 * time. Default is to display them.</li>
\r
35 * <li><b>setSeparatorVariant</b> controls how separators (between
\r
36 * count and period, and multiple periods) will be displayed, when
\r
37 * appropriate for the language. Default is to use full
\r
40 * <li><b>setUnitVariant</b> controls which of various types of
\r
41 * unit names to use. PLURALIZED indicates that full names will be
\r
42 * used. MEDIUM indicates that medium-length (usually 2-3 character)
\r
43 * names will be used. SHORT indicates that short (usually single
\r
44 * character) names will be used. If there is no localization data
\r
45 * available for either the SHORT or MEDIUM names, the other will be
\r
46 * used, if neither is available, the PLURALIZED names will be used.
\r
47 * Default is PLURALIZED.</li>
\r
49 * <li><b>setCountVariant</b> controls how the count for the smallest
\r
50 * unit will be formatted: either as an integer, a fraction to the
\r
51 * smallest half, or as a decimal with 1, 2, or 3 decimal points.</li>
\r
52 * Counts for higher units will be formatted as integers.
\r
56 public class BasicPeriodFormatterFactory implements PeriodFormatterFactory {
\r
57 private final PeriodFormatterDataService ds;
\r
58 private PeriodFormatterData data;
\r
59 private Customizations customizations;
\r
60 private boolean customizationsInUse;
\r
61 private String localeName;
\r
63 // package-only constructor
\r
64 BasicPeriodFormatterFactory(PeriodFormatterDataService ds) {
\r
66 this.customizations = new Customizations();
\r
67 this.localeName = Locale.getDefault().toString();
\r
71 * Return the default rdf factory as a BasicPeriodFormatterFactory.
\r
73 * @return a default BasicPeriodFormatterFactory
\r
75 public static BasicPeriodFormatterFactory getDefault() {
\r
76 return (BasicPeriodFormatterFactory)
\r
77 BasicPeriodFormatterService.getInstance().newPeriodFormatterFactory();
\r
81 * Set the locale for this factory.
\r
83 public PeriodFormatterFactory setLocale(String localeName) {
\r
85 this.localeName = localeName;
\r
90 * Set whether limits will be displayed.
\r
92 * @param display true if limits will be displayed
\r
93 * @return this PeriodFormatterFactory
\r
95 public PeriodFormatterFactory setDisplayLimit(boolean display) {
\r
96 updateCustomizations().displayLimit = display;
\r
101 * Return true if limits will be displayed.
\r
103 * @return true if limits will be displayed
\r
105 public boolean getDisplayLimit() {
\r
106 return customizations.displayLimit;
\r
110 * Set whether past and future will be displayed.
\r
112 * @param display true if past and future will be displayed
\r
113 * @return this PeriodFormatterFactory
\r
115 public PeriodFormatterFactory setDisplayPastFuture(boolean display) {
\r
116 updateCustomizations().displayDirection = display;
\r
121 * Return true if past and future will be displayed.
\r
123 * @return true if past and future will be displayed
\r
125 public boolean getDisplayPastFuture() {
\r
126 return customizations.displayDirection;
\r
130 * Set how separators will be displayed.
\r
132 * @param variant the variant indicating separators will be displayed
\r
133 * @return this PeriodFormatterFactory
\r
135 public PeriodFormatterFactory setSeparatorVariant(int variant) {
\r
136 updateCustomizations().separatorVariant = (byte) variant;
\r
141 * Return the variant indicating how separators will be displayed.
\r
143 * @return the variant
\r
145 public int getSeparatorVariant() {
\r
146 return customizations.separatorVariant;
\r
150 * Set the variant of the time unit names to use.
\r
152 * @param variant the variant to use
\r
153 * @return this PeriodFormatterFactory
\r
155 public PeriodFormatterFactory setUnitVariant(int variant) {
\r
156 updateCustomizations().unitVariant = (byte) variant;
\r
161 * Return the unit variant.
\r
163 * @return the unit variant
\r
165 public int getUnitVariant() {
\r
166 return customizations.unitVariant;
\r
170 * Set the variant of the count to use.
\r
172 * @param variant the variant to use
\r
173 * @return this PeriodFormatterFactory
\r
175 public PeriodFormatterFactory setCountVariant(int variant) {
\r
176 updateCustomizations().countVariant = (byte) variant;
\r
181 * Return the count variant.
\r
183 * @return the count variant
\r
185 public int getCountVariant() {
\r
186 return customizations.countVariant;
\r
189 public PeriodFormatter getFormatter() {
\r
190 customizationsInUse = true;
\r
191 return new BasicPeriodFormatter(this, localeName, getData(),
\r
195 private Customizations updateCustomizations() {
\r
196 if (customizationsInUse) {
\r
197 customizations = customizations.copy();
\r
198 customizationsInUse = false;
\r
200 return customizations;
\r
203 // package access only
\r
204 PeriodFormatterData getData() {
\r
205 if (data == null) {
\r
206 data = ds.get(localeName);
\r
211 // package access for use by BasicPeriodFormatter
\r
212 PeriodFormatterData getData(String locName) {
\r
213 return ds.get(locName);
\r
216 // package access for use by BasicPeriodFormatter
\r
217 static class Customizations {
\r
218 boolean displayLimit = true;
\r
219 boolean displayDirection = true;
\r
220 byte separatorVariant = ESeparatorVariant.FULL;
\r
221 byte unitVariant = EUnitVariant.PLURALIZED;
\r
222 byte countVariant = ECountVariant.INTEGER;
\r
224 public Customizations copy() {
\r
225 Customizations result = new Customizations();
\r
226 result.displayLimit = displayLimit;
\r
227 result.displayDirection = displayDirection;
\r
228 result.separatorVariant = separatorVariant;
\r
229 result.unitVariant = unitVariant;
\r
230 result.countVariant = countVariant;
\r