2 * Copyright (C) 2007-2009, International Business Machines
\r
3 * Corporation and others. All Rights Reserved.
\r
6 package com.ibm.icu.util;
\r
8 import java.util.Locale;
\r
9 import java.util.MissingResourceException;
\r
10 import java.util.Set;
\r
12 import com.ibm.icu.impl.ICULocaleService;
\r
13 import com.ibm.icu.impl.ICUResourceBundle;
\r
14 import com.ibm.icu.impl.ICUService;
\r
15 import com.ibm.icu.impl.ICULocaleService.LocaleKey;
\r
16 import com.ibm.icu.impl.ICULocaleService.LocaleKeyFactory;
\r
17 import com.ibm.icu.impl.ICUService.Factory;
\r
18 import com.ibm.icu.impl.ICUService.Key;
\r
19 import com.ibm.icu.util.Calendar.CalendarFactory;
\r
21 class CalendarServiceShim extends Calendar.CalendarShim {
\r
23 Locale[] getAvailableLocales() {
\r
24 if (service.isDefault()) {
\r
25 return ICUResourceBundle.getAvailableLocales();
\r
27 return service.getAvailableLocales();
\r
30 ULocale[] getAvailableULocales() {
\r
31 if (service.isDefault()) {
\r
32 return ICUResourceBundle.getAvailableULocales();
\r
34 return service.getAvailableULocales();
\r
37 private static final class CalFactory extends LocaleKeyFactory {
\r
38 private CalendarFactory delegate;
\r
39 CalFactory(CalendarFactory delegate) {
\r
40 super(delegate.visible() ? VISIBLE : INVISIBLE);
\r
41 this.delegate = delegate;
\r
44 public Object create(Key key, ICUService srvc) {
\r
45 if (handlesKey(key)) {
\r
46 LocaleKey lkey = (LocaleKey)key;
\r
47 ULocale loc = lkey.canonicalLocale();
\r
48 Object result = delegate.createCalendar(loc);
\r
49 if (result == null) {
\r
50 result = srvc.getKey(key, null, this);
\r
57 protected Set<String> getSupportedIDs() {
\r
58 return delegate.getSupportedLocaleNames();
\r
62 Calendar createInstance(ULocale desiredLocale) {
\r
63 ULocale[] actualLoc = new ULocale[1];
\r
64 if (desiredLocale.equals(ULocale.ROOT)) {
\r
65 desiredLocale = ULocale.ROOT;
\r
67 Calendar cal = (Calendar)service.get(desiredLocale, actualLoc);
\r
69 throw new MissingResourceException("Unable to construct Calendar", "", "");
\r
71 cal = (Calendar)cal.clone();
\r
73 /* !!! TODO !!! actualLoc returned by service is not properly set.
\r
74 * When this Calendar object is being created, cal.setLocale is called
\r
75 * and proper actual locale is set at that time. Revisit this later.
\r
79 ULocale uloc = actualLoc[0];
\r
80 cal.setLocale(uloc, uloc); // service make no distinction between actual and valid
\r
85 Object registerFactory(CalendarFactory factory) {
\r
86 return service.registerFactory(new CalFactory(factory));
\r
89 boolean unregister(Object k) {
\r
90 return service.unregisterFactory((Factory)k);
\r
93 private static class CalService extends ICULocaleService {
\r
96 class RBCalendarFactory extends ICUResourceBundleFactory {
\r
97 protected Object handleCreate(ULocale loc, int kind, ICUService sercice) {
\r
98 return Calendar.createInstance(loc);
\r
101 this.registerFactory(new RBCalendarFactory());
\r
106 private static ICULocaleService service = new CalService();
\r