@Override public String select(Object ctx, double number) { if(rules == null) { rules = PluralRules.forLocale(msgFormat.ulocale, type); if(context.formatter instanceof DecimalFormat) { FixedDecimal dec = ((DecimalFormat)context.formatter).getFixedDecimal(number); return rules.select(dec); } else { return rules.select(number);
/** * Parses a plural rules description and returns a PluralRules. * @param description the rule description. * @throws ParseException if the description cannot be parsed. * The exception index is typically not set, it will be -1. * @stable ICU 3.8 */ public static PluralRules parseDescription(String description) throws ParseException { description = description.trim(); return description.length() == 0 ? DEFAULT : new PluralRules(parseRuleChain(description)); }
/** * Set plural rules. These are initially set in the constructor based on the locale, * and usually do not need to be changed. * * @param ruleDescription new plural rule description * @stable ICU 4.2 */ public void setPluralRules(String ruleDescription) { pluralRules = PluralRules.createRules(ruleDescription); }
@Override public String select(Object context, double number) { FixedDecimal dec = (FixedDecimal) context; assert dec.source == (dec.isNegative ? -number : number); return pluralRules.select(dec); } }
private void initialize(ULocale uloc) { ulocale = uloc; pluralRules = PluralRules.forLocale(uloc); setupCurrencyPluralPattern(uloc); }
public PluralSamples(PluralRules pluralRules) { this.pluralRules = pluralRules; Set<String> keywords = pluralRules.getKeywords(); temp.put(k, pluralRules.isLimited(k)); String keyword = pluralRules.select(s); addRelation(foundKeywords, keyword, s); mentioned.addAll(fractions(mentioned)); for (FixedDecimal s : mentioned) { String keyword = pluralRules.select(s); Set<FixedDecimal> list = sampleFractionMap.get(keyword); if (list == null) {
private void setup() { if (locale == null) { if (format != null) { locale = format.getLocale(null); } else { locale = ULocale.getDefault(Category.FORMAT); } // Needed for getLocale(ULocale.VALID_LOCALE) setLocale(locale, locale); } if (format == null) { format = NumberFormat.getNumberInstance(locale); } pluralRules = PluralRules.forLocale(locale); timeUnitToCountToPatterns = new HashMap<TimeUnit, Map<String, Object[]>>(); Set<String> pluralKeywords = pluralRules.getKeywords(); setup("units/duration", timeUnitToCountToPatterns, FULL_NAME, pluralKeywords); setup("unitsShort/duration", timeUnitToCountToPatterns, ABBREVIATED_NAME, pluralKeywords); isReady = true; }
/** * Returns all the values that trigger this keyword, or null if the number of such * values is unlimited. * * @param keyword the keyword * @return the values that trigger this keyword, or null. The returned collection * is immutable. It will be empty if the keyword is not defined. * @stable ICU 4.8 */ Collection<Double> getAllKeywordValues(String keyword) { // HACK for now if (!pluralRules.getKeywords().contains(keyword)) { return Collections.<Double>emptyList(); } Collection<Double> result = getKeySamplesMap().get(keyword); // We depend on MAX_SAMPLES here. It's possible for a conjunction // of unlimited rules that 'looks' unlimited to return a limited // number of values. There's no bounds to this limited number, in // general, because you can construct arbitrarily complex rules. Since // we always generate 3 samples if a rule is really unlimited, that's // where we put the cutoff. if (result.size() > 2 && !_keyLimitedMap.get(keyword)) { return null; } return result; } }
if (!pluralRules.getKeywords().contains(keyword)) { return KeywordStatus.INVALID; Collection<Double> values = pluralRules.getAllKeywordValues(keyword); if (values == null) { return KeywordStatus.UNBOUNDED;
int maxCount = isLimited(keyword, sampleType) ? Integer.MAX_VALUE : 20; if (!addSample(keyword, i, maxCount, result)) { break; addSample(keyword, 1000000, maxCount, result); // hack for Welsh break; case DECIMAL: for (int i = 0; i < 2000; ++i) { if (!addSample(keyword, new FixedDecimal(i/10d, 1), maxCount, result)) { break; addSample(keyword, new FixedDecimal(1000000d, 1), maxCount, result); // hack for Welsh break;
/** * {@inheritDoc} * @stable ICU 3.8 */ @Override public boolean equals(Object rhs) { return rhs instanceof PluralRules && equals((PluralRules)rhs); }
private String getPluralForm(FixedDecimal fixedDecimal) { if (pluralRules == null) { return CompactDecimalDataCache.OTHER; } return pluralRules.select(fixedDecimal); }
private PluralRules createPluralRules() { ULocale locale = ULocale.forLanguageTag(this.locale); PluralType pluralType = "cardinal".equals(type) ? PluralType.CARDINAL : PluralType.ORDINAL; return PluralRules.forLocale(locale, pluralType); }
/** * [[PluralCategories]] * * @return the plural categories */ public ArrayObject getPluralCategories() { if (pluralCategories == null) { pluralCategories = CreateArrayFromList(realm.defaultContext(), getPluralRules().getKeywords()); } return pluralCategories; }
/** * Override equals * * @stable ICU 4.2 */ public boolean equals(Object a) { if (a instanceof CurrencyPluralInfo) { CurrencyPluralInfo other = (CurrencyPluralInfo)a; return pluralRules.equals(other.pluralRules) && pluralCountToCurrencyUnitPattern.equals(other.pluralCountToCurrencyUnitPattern); } return false; }
/** * Given a number, returns the keyword of the first rule that applies * to the number. * @internal * @deprecated This API is ICU internal only. */ @Deprecated String select(PluralRules.FixedDecimal numberInfo) { return pluralRules.select(numberInfo); }
/** * Provides access to the predefined <code>PluralRules</code> for a given * {@link java.util.Locale} and the plural type. * * <p>ICU defines plural rules for many locales based on CLDR <i>Language Plural Rules</i>. * For these predefined rules, see CLDR page at * http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html * * @param locale The locale for which a <code>PluralRules</code> object is * returned. * @param type The plural type (e.g., cardinal or ordinal). * @return The predefined <code>PluralRules</code> object for this locale. * If there's no predefined rules for this locale, the rules * for the closest parent in the locale hierarchy that has one will * be returned. The final fallback always returns the default * rules. * @stable ICU 54 */ public static PluralRules forLocale(Locale locale, PluralType type) { return forLocale(ULocale.forLocale(locale), type); }
this.units = otherPluralVariant(pluralAffixes, divisor, debugCreationErrors); this.currencyUnits = otherPluralVariant(pluralAffixes, divisor, debugCreationErrors); if (!pluralRules.getKeywords().equals(this.units.keySet())) { debugCreationErrors.add("Missmatch in pluralCategories, should be: " + pluralRules.getKeywords() + ", was actually " + this.units.keySet());
/** * {@inheritDoc} * @stable ICU 49 */ @Override public boolean equals(Object obj) { if (obj == null) return false; if (!super.equals(obj)) return false; // super does class check CompactDecimalFormat other = (CompactDecimalFormat) obj; return mapsAreEqual(units, other.units) && Arrays.equals(divisor, other.divisor) && (pluralToCurrencyAffixes == other.pluralToCurrencyAffixes || pluralToCurrencyAffixes != null && pluralToCurrencyAffixes.equals(other.pluralToCurrencyAffixes)) && pluralRules.equals(other.pluralRules); }