/**
*******************************************************************************
- * Copyright (C) 2003-2008, International Business Machines Corporation and
+ * Copyright (C) 2003-2014, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
* Partial port from ICU4C's Grego class in i18n/gregoimp.h.
package com.ibm.icu.impl;
-import com.ibm.icu.util.Calendar;
+import java.util.Locale;
+
/**
* A utility class providing proleptic Gregorian calendar functions
*/
public static int dayOfWeek(long day) {
long[] remainder = new long[1];
- floorDivide(day + Calendar.THURSDAY, 7, remainder);
+ floorDivide(day + 5 /* Calendar.THURSDAY */, 7, remainder);
int dayOfWeek = (int)remainder[0];
dayOfWeek = (dayOfWeek == 0) ? 7 : dayOfWeek;
return dayOfWeek;
}
return weekInMonth;
}
+
+ /**
+ * Convenient method for formatting time to ISO 8601 style
+ * date string.
+ * @param time long time
+ * @return ISO-8601 date string
+ */
+ public static String timeToString(long time) {
+ int[] fields = timeToFields(time, null);
+ int millis = fields[5];
+ int hour = millis / MILLIS_PER_HOUR;
+ millis = millis % MILLIS_PER_HOUR;
+ int min = millis / MILLIS_PER_MINUTE;
+ millis = millis % MILLIS_PER_MINUTE;
+ int sec = millis / MILLIS_PER_SECOND;
+ millis = millis % MILLIS_PER_SECOND;
+
+ return String.format((Locale)null, "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
+ fields[0], fields[1] + 1, fields[2], hour, min, sec, millis);
+ }
}
*/
package com.ibm.icu.text;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
import java.util.Locale;
-import com.ibm.icu.impl.LocaleDisplayNamesImpl;
+import com.ibm.icu.impl.ICUConfig;
+import com.ibm.icu.lang.UScript;
+import com.ibm.icu.text.DisplayContext.Type;
import com.ibm.icu.util.ULocale;
/**
* @stable ICU 4.4
*/
public static LocaleDisplayNames getInstance(ULocale locale, DialectHandling dialectHandling) {
- return LocaleDisplayNamesImpl.getInstance(locale, dialectHandling);
+ LocaleDisplayNames result = null;
+ if (FACTORY_DIALECTHANDLING != null) {
+ try {
+ result = (LocaleDisplayNames) FACTORY_DIALECTHANDLING.invoke(null,
+ locale, dialectHandling);
+ } catch (InvocationTargetException e) {
+ // fall through
+ } catch (IllegalAccessException e) {
+ // fall through
+ }
+ }
+ if (result == null) {
+ result = new LastResortLocaleDisplayNames(locale, dialectHandling);
+ }
+ return result;
}
/**
* @stable ICU 51
*/
public static LocaleDisplayNames getInstance(ULocale locale, DisplayContext... contexts) {
- return LocaleDisplayNamesImpl.getInstance(locale, contexts);
+ LocaleDisplayNames result = null;
+ if (FACTORY_DISPLAYCONTEXT != null) {
+ try {
+ result = (LocaleDisplayNames) FACTORY_DISPLAYCONTEXT.invoke(null,
+ locale, (Object[])contexts);
+ } catch (InvocationTargetException e) {
+ // fall through
+ } catch (IllegalAccessException e) {
+ // fall through
+ }
+ }
+ if (result == null) {
+ result = new LastResortLocaleDisplayNames(locale, contexts);
+ }
+ return result;
}
// getters for state
@Deprecated
protected LocaleDisplayNames() {
}
+
+ private static final Method FACTORY_DIALECTHANDLING;
+ private static final Method FACTORY_DISPLAYCONTEXT;
+
+ static {
+ String implClassName = ICUConfig.get("com.ibm.icu.text.LocaleDisplayNames.impl", "com.ibm.icu.impl.LocaleDisplayNamesImpl");
+
+ Method factoryDialectHandling = null;
+ Method factoryDisplayContext = null;
+
+ try {
+ Class<?> implClass = Class.forName(implClassName);
+ try {
+ factoryDialectHandling = implClass.getMethod("getInstance",
+ ULocale.class, DialectHandling.class);
+ } catch (NoSuchMethodException e) {
+ }
+ try {
+ factoryDisplayContext = implClass.getMethod("getInstance",
+ ULocale.class, DisplayContext[].class);
+ } catch (NoSuchMethodException e) {
+ }
+
+ } catch (ClassNotFoundException e) {
+ // fallback to last resort impl
+ }
+
+ FACTORY_DIALECTHANDLING = factoryDialectHandling;
+ FACTORY_DISPLAYCONTEXT = factoryDisplayContext;
+ }
+
+ /**
+ * Minimum implementation of LocaleDisplayNames
+ */
+ private static class LastResortLocaleDisplayNames extends LocaleDisplayNames {
+
+ private ULocale locale;
+ private DisplayContext[] contexts;
+
+ private LastResortLocaleDisplayNames(ULocale locale, DialectHandling dialectHandling) {
+ this.locale = locale;
+ DisplayContext context = (dialectHandling == DialectHandling.DIALECT_NAMES) ?
+ DisplayContext.DIALECT_NAMES : DisplayContext.STANDARD_NAMES;
+ this.contexts = new DisplayContext[] {context};
+ }
+
+ private LastResortLocaleDisplayNames(ULocale locale, DisplayContext... contexts) {
+ this.locale = locale;
+ this.contexts = new DisplayContext[contexts.length];
+ System.arraycopy(contexts, 0, this.contexts, 0, contexts.length);
+ }
+
+ @Override
+ public ULocale getLocale() {
+ return locale;
+ }
+
+ @Override
+ public DialectHandling getDialectHandling() {
+ DialectHandling result = DialectHandling.STANDARD_NAMES;
+ for (DisplayContext context : contexts) {
+ if (context.type() == DisplayContext.Type.DIALECT_HANDLING) {
+ if (context.value() == DisplayContext.DIALECT_NAMES.ordinal()) {
+ result = DialectHandling.DIALECT_NAMES;
+ break;
+ }
+ }
+ }
+ return result;
+ }
+
+ @Override
+ public DisplayContext getContext(Type type) {
+ DisplayContext result = DisplayContext.STANDARD_NAMES; // final fallback
+ for (DisplayContext context : contexts) {
+ if (context.type() == type) {
+ result = context;
+ break;
+ }
+ }
+ return result;
+ }
+
+ @Override
+ public String localeDisplayName(ULocale locale) {
+ return locale.getName();
+ }
+
+ @Override
+ public String localeDisplayName(Locale locale) {
+ return ULocale.forLocale(locale).getName();
+ }
+
+ @Override
+ public String localeDisplayName(String localeId) {
+ return new ULocale(localeId).getName();
+ }
+
+ @Override
+ public String languageDisplayName(String lang) {
+ return lang;
+ }
+
+ @Override
+ public String scriptDisplayName(String script) {
+ return script;
+ }
+
+ @Override
+ public String scriptDisplayName(int scriptCode) {
+ return UScript.getShortName(scriptCode);
+ }
+
+ @Override
+ public String regionDisplayName(String region) {
+ return region;
+ }
+
+ @Override
+ public String variantDisplayName(String variant) {
+ return variant;
+ }
+
+ @Override
+ public String keyDisplayName(String key) {
+ return key;
+ }
+
+ @Override
+ public String keyValueDisplayName(String key, String value) {
+ return value;
+ }
+
+ }
}