1/* GENERATED SOURCE. DO NOT MODIFY. */
2/*
3******************************************************************************
4* Copyright (C) 2007-2010, International Business Machines Corporation and   *
5* others. All Rights Reserved.                                               *
6******************************************************************************
7*/
8
9package android.icu.impl.duration;
10
11import java.util.Locale;
12
13import android.icu.impl.duration.impl.DataRecord.ECountVariant;
14import android.icu.impl.duration.impl.DataRecord.ESeparatorVariant;
15import android.icu.impl.duration.impl.DataRecord.EUnitVariant;
16import android.icu.impl.duration.impl.PeriodFormatterData;
17import android.icu.impl.duration.impl.PeriodFormatterDataService;
18
19/**
20 * An implementation of PeriodFormatterFactory that provides customization of
21 * formatting behavior. Instances of this factory are created by
22 * BasicPeriodFormatterService.
23 *
24 * The settings on BasicPeriodFormatterFactory are:
25 * <ul>
26 *
27 * <li><b>setDisplayLimit</b> controls whether phrases like 'more than'
28 * or 'less than' will be displayed when the Period has a defined
29 * limit.  Default is to display them.</li>
30 *
31 * <li><b>setDisplayPastFuture</b> controls whether phrases like 'ago'
32 * or 'from now' will be displayed to indicate past or future
33 * time. Default is to display them.</li>
34 *
35 * <li><b>setSeparatorVariant</b> controls how separators (between
36 * count and period, and multiple periods) will be displayed, when
37 * appropriate for the language. Default is to use full
38 * separators.</li>
39 *
40 * <li><b>setUnitVariant</b> controls which of various types of
41 * unit names to use.  PLURALIZED indicates that full names will be
42 * used.  MEDIUM indicates that medium-length (usually 2-3 character)
43 * names will be used.  SHORT indicates that short (usually single
44 * character) names will be used.  If there is no localization data
45 * available for either the SHORT or MEDIUM names, the other will be
46 * used, if neither is available, the PLURALIZED names will be used.
47 * Default is PLURALIZED.</li>
48 *
49 * <li><b>setCountVariant</b> controls how the count for the smallest
50 * unit will be formatted: either as an integer, a fraction to the
51 * smallest half, or as a decimal with 1, 2, or 3 decimal points.</li>
52 * Counts for higher units will be formatted as integers.
53 *
54 * </ul>
55 * @hide Only a subset of ICU is exposed in Android
56 */
57public class BasicPeriodFormatterFactory implements PeriodFormatterFactory {
58  private final PeriodFormatterDataService ds;
59  private PeriodFormatterData data;
60  private Customizations customizations;
61  private boolean customizationsInUse;
62  private String localeName;
63
64  // package-only constructor
65  BasicPeriodFormatterFactory(PeriodFormatterDataService ds) {
66    this.ds = ds;
67    this.customizations = new Customizations();
68    this.localeName = Locale.getDefault().toString();
69  }
70
71  /**
72   * Return the default rdf factory as a BasicPeriodFormatterFactory.
73   *
74   * @return a default BasicPeriodFormatterFactory
75   */
76  public static BasicPeriodFormatterFactory getDefault() {
77      return (BasicPeriodFormatterFactory)
78        BasicPeriodFormatterService.getInstance().newPeriodFormatterFactory();
79  }
80
81  /**
82   * Set the locale for this factory.
83   */
84  public PeriodFormatterFactory setLocale(String localeName) {
85    data = null;
86    this.localeName = localeName;
87    return this;
88  }
89
90  /**
91   * Set whether limits will be displayed.
92   *
93   * @param display true if limits will be displayed
94   * @return this PeriodFormatterFactory
95   */
96  public PeriodFormatterFactory setDisplayLimit(boolean display) {
97    updateCustomizations().displayLimit = display;
98    return this;
99  }
100
101  /**
102   * Return true if limits will be displayed.
103   *
104   * @return true if limits will be displayed
105   */
106  public boolean getDisplayLimit() {
107    return customizations.displayLimit;
108  }
109
110  /**
111   * Set whether past and future will be displayed.
112   *
113   * @param display true if past and future will be displayed
114   * @return this PeriodFormatterFactory
115   */
116  public PeriodFormatterFactory setDisplayPastFuture(boolean display) {
117    updateCustomizations().displayDirection = display;
118    return this;
119  }
120
121  /**
122   * Return true if past and future will be displayed.
123   *
124   * @return true if past and future will be displayed
125   */
126  public boolean getDisplayPastFuture() {
127    return customizations.displayDirection;
128  }
129
130  /**
131   * Set how separators will be displayed.
132   *
133   * @param variant the variant indicating separators will be displayed
134   * @return this PeriodFormatterFactory
135   */
136  public PeriodFormatterFactory setSeparatorVariant(int variant) {
137    updateCustomizations().separatorVariant = (byte) variant;
138    return this;
139  }
140
141  /**
142   * Return the variant indicating how separators will be displayed.
143   *
144   * @return the variant
145   */
146  public int getSeparatorVariant() {
147    return customizations.separatorVariant;
148  }
149
150  /**
151   * Set the variant of the time unit names to use.
152   *
153   * @param variant the variant to use
154   * @return this PeriodFormatterFactory
155   */
156  public PeriodFormatterFactory setUnitVariant(int variant) {
157    updateCustomizations().unitVariant = (byte) variant;
158    return this;
159  }
160
161  /**
162   * Return the unit variant.
163   *
164   * @return the unit variant
165   */
166  public int getUnitVariant() {
167    return customizations.unitVariant;
168  }
169
170  /**
171   * Set the variant of the count to use.
172   *
173   * @param variant the variant to use
174   * @return this PeriodFormatterFactory
175   */
176  public PeriodFormatterFactory setCountVariant(int variant) {
177    updateCustomizations().countVariant = (byte) variant;
178    return this;
179  }
180
181  /**
182   * Return the count variant.
183   *
184   * @return the count variant
185   */
186  public int getCountVariant() {
187    return customizations.countVariant;
188  }
189
190  public PeriodFormatter getFormatter() {
191    customizationsInUse = true;
192    return new BasicPeriodFormatter(this, localeName, getData(),
193                                    customizations);
194  }
195
196  private Customizations updateCustomizations() {
197    if (customizationsInUse) {
198      customizations = customizations.copy();
199      customizationsInUse = false;
200    }
201    return customizations;
202  }
203
204  // package access only
205  PeriodFormatterData getData() {
206    if (data == null) {
207      data = ds.get(localeName);
208    }
209    return data;
210  }
211
212  // package access for use by BasicPeriodFormatter
213  PeriodFormatterData getData(String locName) {
214    return ds.get(locName);
215  }
216
217  // package access for use by BasicPeriodFormatter
218  static class Customizations {
219    boolean displayLimit = true;
220    boolean displayDirection = true;
221    byte separatorVariant = ESeparatorVariant.FULL;
222    byte unitVariant = EUnitVariant.PLURALIZED;
223    byte countVariant = ECountVariant.INTEGER;
224
225    public Customizations copy() {
226        Customizations result = new Customizations();
227        result.displayLimit = displayLimit;
228        result.displayDirection = displayDirection;
229        result.separatorVariant = separatorVariant;
230        result.unitVariant = unitVariant;
231        result.countVariant = countVariant;
232        return result;
233    }
234  }
235}
236