]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/classes/core/src/com/ibm/icu/impl/duration/BasicPeriodFormatterFactory.java
Upgrade ICU4J.
[Dictionary.git] / jars / icu4j-52_1 / main / classes / core / src / com / ibm / icu / impl / duration / BasicPeriodFormatterFactory.java
1 /*
2 ******************************************************************************
3 * Copyright (C) 2007-2010, 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.Locale;
11
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;
17
18 /**
19  * An implementation of PeriodFormatterFactory that provides customization of
20  * formatting behavior. Instances of this factory are created by
21  * BasicPeriodFormatterService.
22  *
23  * The settings on BasicPeriodFormatterFactory are:
24  * <ul>
25  *
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>
29  *
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>
33  *
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
37  * separators.</li>
38  *
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>
47  *
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.
52  *
53  * </ul>
54  */
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;
61
62   // package-only constructor
63   BasicPeriodFormatterFactory(PeriodFormatterDataService ds) {
64     this.ds = ds;
65     this.customizations = new Customizations();
66     this.localeName = Locale.getDefault().toString();
67   }
68
69   /**
70    * Return the default rdf factory as a BasicPeriodFormatterFactory.
71    *
72    * @return a default BasicPeriodFormatterFactory
73    */
74   public static BasicPeriodFormatterFactory getDefault() {
75       return (BasicPeriodFormatterFactory)
76         BasicPeriodFormatterService.getInstance().newPeriodFormatterFactory();
77   }
78
79   /**
80    * Set the locale for this factory.
81    */
82   public PeriodFormatterFactory setLocale(String localeName) {
83     data = null;
84     this.localeName = localeName;
85     return this;
86   }
87
88   /**
89    * Set whether limits will be displayed.
90    *
91    * @param display true if limits will be displayed
92    * @return this PeriodFormatterFactory
93    */
94   public PeriodFormatterFactory setDisplayLimit(boolean display) {
95     updateCustomizations().displayLimit = display;
96     return this;
97   }
98
99   /**
100    * Return true if limits will be displayed.
101    *
102    * @return true if limits will be displayed
103    */
104   public boolean getDisplayLimit() {
105     return customizations.displayLimit;
106   }
107
108   /**
109    * Set whether past and future will be displayed.
110    *
111    * @param display true if past and future will be displayed
112    * @return this PeriodFormatterFactory
113    */
114   public PeriodFormatterFactory setDisplayPastFuture(boolean display) {
115     updateCustomizations().displayDirection = display;
116     return this;
117   }
118
119   /**
120    * Return true if past and future will be displayed.
121    *
122    * @return true if past and future will be displayed
123    */
124   public boolean getDisplayPastFuture() {
125     return customizations.displayDirection;
126   }
127
128   /**
129    * Set how separators will be displayed.
130    *
131    * @param variant the variant indicating separators will be displayed
132    * @return this PeriodFormatterFactory
133    */
134   public PeriodFormatterFactory setSeparatorVariant(int variant) {
135     updateCustomizations().separatorVariant = (byte) variant;
136     return this;
137   }
138
139   /**
140    * Return the variant indicating how separators will be displayed.
141    *
142    * @return the variant
143    */
144   public int getSeparatorVariant() {
145     return customizations.separatorVariant;
146   }
147
148   /**
149    * Set the variant of the time unit names to use.
150    *
151    * @param variant the variant to use
152    * @return this PeriodFormatterFactory
153    */
154   public PeriodFormatterFactory setUnitVariant(int variant) {
155     updateCustomizations().unitVariant = (byte) variant;
156     return this;
157   }
158
159   /**
160    * Return the unit variant.
161    *
162    * @return the unit variant
163    */
164   public int getUnitVariant() {
165     return customizations.unitVariant;
166   }
167
168   /**
169    * Set the variant of the count to use.
170    *
171    * @param variant the variant to use
172    * @return this PeriodFormatterFactory
173    */
174   public PeriodFormatterFactory setCountVariant(int variant) {
175     updateCustomizations().countVariant = (byte) variant;
176     return this;
177   }
178
179   /**
180    * Return the count variant.
181    *
182    * @return the count variant
183    */
184   public int getCountVariant() {
185     return customizations.countVariant;
186   }
187
188   public PeriodFormatter getFormatter() {
189     customizationsInUse = true;
190     return new BasicPeriodFormatter(this, localeName, getData(), 
191                                     customizations);
192   }
193
194   private Customizations updateCustomizations() {
195     if (customizationsInUse) {
196       customizations = customizations.copy();
197       customizationsInUse = false;
198     }
199     return customizations;
200   }
201
202   // package access only
203   PeriodFormatterData getData() {
204     if (data == null) {
205       data = ds.get(localeName);
206     }
207     return data;
208   }
209
210   // package access for use by BasicPeriodFormatter
211   PeriodFormatterData getData(String locName) {
212     return ds.get(locName);
213   }
214
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;
222     
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;
230         return result;
231     }
232   }
233 }