ICU-2755 registration for collation

X-SVN-Rev: 11594
This commit is contained in:
Doug Felt 2003-04-19 00:01:53 +00:00
parent 817977fbcc
commit aa58c6eed6
5 changed files with 522 additions and 48 deletions

View File

@ -0,0 +1,231 @@
/*
*******************************************************************************
* Copyright (C) 2003, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/collator/CollationServiceTest.java,v $
* $Date: 2003/04/19 00:01:52 $
* $Revision: 1.1 $
*
*****************************************************************************************
*/
package com.ibm.icu.dev.test.collator;
import java.util.Collections;
import java.util.Locale;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import com.ibm.icu.dev.test.TestFmwk;
import com.ibm.icu.impl.LocaleUtility;
import com.ibm.icu.text.Collator;
import com.ibm.icu.text.Collator.CollatorFactory;
public class CollationServiceTest extends TestFmwk {
public static void main(String[] args) {
new CollationServiceTest().run(args);
}
public void TestRegister() {
// register a singleton
Collator frcol = Collator.getInstance(Locale.FRANCE);
Collator uscol = Collator.getInstance(Locale.US);
{ // try override en_US collator
Object key = Collator.registerInstance(frcol, Locale.US);
Collator ncol = Collator.getInstance(Locale.US);
if (!frcol.equals(ncol)) {
errln("register of french collator for en_US failed");
}
if (!Collator.unregister(key)) {
errln("failed to unregister french collator");
}
ncol = Collator.getInstance(Locale.US);
if (!uscol.equals(ncol)) {
errln("collator after unregister does not match original");
}
}
Locale fu_FU = new Locale("fu", "FU", "FOO");
{ // try create collator for new locale
Collator fucol = Collator.getInstance(fu_FU);
Object key = Collator.registerInstance(frcol, fu_FU);
Collator ncol = Collator.getInstance(fu_FU);
if (!frcol.equals(ncol)) {
errln("register of fr collator for fu_FU failed");
}
Locale[] locales = Collator.getAvailableLocales();
boolean found = false;
for (int i = 0; i < locales.length; ++i) {
if (locales[i].equals(fu_FU)) {
found = true;
break;
}
}
if (!found) {
errln("new locale fu_FU not reported as supported locale");
}
String name = Collator.getDisplayName(fu_FU);
if (!"fu (FU,FOO)".equals(name)) {
errln("found " + name + " for fu_FU");
}
name = Collator.getDisplayName(fu_FU, fu_FU);
if (!"fu (FU,FOO)".equals(name)) {
errln("found " + name + " for fu_FU");
}
if (!Collator.unregister(key)) {
errln("failed to unregister french collator");
}
ncol = Collator.getInstance(fu_FU);
if (!fucol.equals(ncol)) {
errln("collator after unregister does not match original fu_FU");
}
}
}
public void TestRegisterFactory() {
class CollatorInfo {
Locale locale;
Collator collator;
Map displayNames; // locale -> string
CollatorInfo(Locale locale, Collator collator, Map displayNames) {
this.locale = locale;
this.collator = collator;
this.displayNames = displayNames;
}
String getDisplayName(Locale displayLocale) {
String name = null;
if (displayNames != null) {
name = (String)displayNames.get(displayLocale);
}
if (name == null) {
name = locale.getDisplayName(displayLocale);
}
return name;
}
}
class TestFactory extends CollatorFactory {
private Map map;
private Set ids;
TestFactory(CollatorInfo[] info) {
map = new HashMap();
for (int i = 0; i < info.length; ++i) {
CollatorInfo ci = info[i];
map.put(ci.locale, ci);
}
}
public Collator createCollator(Locale loc) {
CollatorInfo ci = (CollatorInfo)map.get(loc);
if (ci != null) {
return ci.collator;
}
return null;
}
public String getDisplayName(Locale objectLocale, Locale displayLocale) {
CollatorInfo ci = (CollatorInfo)map.get(objectLocale);
if (ci != null) {
return ci.getDisplayName(displayLocale);
}
return null;
}
public Set getSupportedLocaleIDs() {
if (ids == null) {
HashSet set = new HashSet();
Iterator iter = map.keySet().iterator();
while (iter.hasNext()) {
Locale locale = (Locale)iter.next();
String id = LocaleUtility.canonicalLocaleString(locale.toString());
set.add(id);
}
ids = Collections.unmodifiableSet(set);
}
return ids;
}
}
Locale fu_FU = new Locale("fu", "FU", "");
Locale fu_FU_FOO = new Locale("fu", "FU", "FOO");
Map fuFUNames = new HashMap();
fuFUNames.put(fu_FU, "ze leetle bunny Fu-Fu");
fuFUNames.put(fu_FU_FOO, "zee leetel bunny Foo-Foo");
fuFUNames.put(Locale.US, "little bunny Foo Foo");
Collator frcol = Collator.getInstance(Locale.FRANCE);
Collator uscol = Collator.getInstance(Locale.US);
Collator gecol = Collator.getInstance(Locale.GERMANY);
Collator jpcol = Collator.getInstance(Locale.JAPAN);
Collator fucol = Collator.getInstance(fu_FU);
System.out.println("jpcol: " + jpcol);
CollatorInfo[] info = {
new CollatorInfo(Locale.US, frcol, null),
new CollatorInfo(Locale.FRANCE, gecol, null),
new CollatorInfo(fu_FU, jpcol, fuFUNames),
};
TestFactory factory = new TestFactory(info);
{
Object key = Collator.registerFactory(factory);
Collator ncol = Collator.getInstance(Locale.US);
if (!frcol.equals(ncol)) {
errln("frcoll for en_US failed");
}
ncol = Collator.getInstance(fu_FU_FOO);
if (!jpcol.equals(ncol)) {
errln("jpcol for fu_FU_FOO failed, got: " + ncol);
}
Locale[] locales = Collator.getAvailableLocales();
boolean found = false;
for (int i = 0; i < locales.length; ++i) {
if (locales[i].equals(fu_FU)) {
found = true;
break;
}
}
if (!found) {
errln("new locale fu_FU not reported as supported locale");
}
String name = Collator.getDisplayName(fu_FU);
if (!"little bunny Foo Foo".equals(name)) {
errln("found " + name + " for fu_FU");
}
name = Collator.getDisplayName(fu_FU, fu_FU_FOO);
if (!"zee leetel bunny Foo-Foo".equals(name)) {
errln("found " + name + " for fu_FU in fu_FU_FOO");
}
if (!Collator.unregister(key)) {
errln("failed to unregister factory");
}
ncol = Collator.getInstance(fu_FU);
if (!fucol.equals(ncol)) {
errln("collator after unregister does not match original fu_FU");
}
}
}
}

View File

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/collator/TestAll.java,v $
* $Date: 2003/02/05 05:45:16 $
* $Revision: 1.2 $
* $Date: 2003/04/19 00:01:52 $
* $Revision: 1.3 $
*
*****************************************************************************************
*/
@ -44,6 +44,7 @@ public class TestAll extends TestGroup {
"G7CollationTest",
"LotusCollationKoreanTest",
"CollationMiscTest",
"CollationServiceTest",
"com.ibm.icu.dev.test.search.SearchTest"
},
"All Collation Tests and Search Test"

View File

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/impl/ICULocaleService.java,v $
* $Date: 2002/12/12 18:02:09 $
* $Revision: 1.12 $
* $Date: 2003/04/19 00:01:52 $
* $Revision: 1.13 $
*
*******************************************************************************
*/
@ -61,7 +61,7 @@ public class ICULocaleService extends ICUService {
/**
* Convenience override for callers using locales. This calls
* get(Locale, String, Locale[]) with a null kind.
* get(Locale, int, Locale[]) with KIND_ANY.
*/
public Object get(Locale locale, Locale[] actualReturn) {
return get(locale, LocaleKey.KIND_ANY, actualReturn);
@ -373,7 +373,7 @@ public class ICULocaleService extends ICUService {
public Object create(Key key, ICUService service) {
if (handlesKey(key)) {
LocaleKey lkey = (LocaleKey)key;
Locale loc = lkey.canonicalLocale();
Locale loc = lkey.currentLocale();
int kind = lkey.kind();
return handleCreate(loc, kind, service);

View File

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/text/Collator.java,v $
* $Date: 2003/02/27 23:43:16 $
* $Revision: 1.23 $
* $Date: 2003/04/19 00:01:53 $
* $Revision: 1.24 $
*
*******************************************************************************
*/
@ -15,12 +15,14 @@ package com.ibm.icu.text;
import java.util.Comparator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import com.ibm.icu.impl.ICULocaleData;
import com.ibm.icu.impl.ICULocaleService;
import com.ibm.icu.impl.ICULocaleService.ICUResourceBundleFactory;
import com.ibm.icu.impl.ICUService;
import com.ibm.icu.impl.ICUService.Factory;
import com.ibm.icu.impl.LocaleUtility;
/**
* <p>Collator performs locale-sensitive string comparison. A concrete
@ -310,8 +312,84 @@ public abstract class Collator implements Comparator, Cloneable
return getInstance(Locale.getDefault());
}
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
// begin registry stuff
public static abstract class CollatorFactory {
/**
* Return true if this factory will be visible. Default is true.
* If not visible, the locales supported by this factory will not
* be listed by getAvailableLocales.
*
* @return true if this factory is visible
*/
public boolean visible() {
return true;
}
/**
* Return an instance of the appropriate collator. If the locale
* is not supported, return null.
* @param loc the locale for which this collator is to be created.
* @return the newly created collator.
*/
public abstract Collator createCollator(Locale loc);
/**
* Return the name of the collator for the objectLocale, localized for the displayLocale.
* If objectLocale is not visible or not defined by the factory, return null.
* @param objectLocale the locale identifying the collator
* @param displayLocale the locale for which the display name of the collator should be localized
* @return the display name
*/
public String getDisplayName(Locale objectLocale, Locale displayLocale) {
if (visible()) {
Set supported = getSupportedLocaleIDs();
String name = LocaleUtility.canonicalLocaleString(objectLocale.toString());
if (supported.contains(name)) {
return objectLocale.getDisplayName(displayLocale);
}
}
return null;
}
/**
* Return an unmodifiable collection of the locale names directly
* supported by this factory.
*
* @return the set of supported locale IDs.
*/
public abstract Set getSupportedLocaleIDs();
}
static abstract class ServiceShim {
abstract Collator getInstance(Locale l);
abstract Object registerInstance(Collator c, Locale l);
abstract Object registerFactory(CollatorFactory f);
abstract boolean unregister(Object k);
abstract Locale[] getAvailableLocales();
abstract Map getDisplayNames(Locale l);
abstract String getDisplayName(Locale ol, Locale dl);
}
private static ServiceShim shim;
private static ServiceShim getShim() {
if (shim == null) {
try {
Class cls = Class.forName("com.ibm.icu.text.CollatorServiceShim");
shim = (ServiceShim)cls.newInstance();
}
catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
}
return shim;
}
/**
* Gets the Collator for the desired locale.
* @param locale the desired locale.
@ -326,21 +404,51 @@ public abstract class Collator implements Comparator, Cloneable
*/
public static final Collator getInstance(Locale locale)
{
if (service == null) {
if (shim == null) {
return new RuleBasedCollator(locale);
} else {
///CLOVER:OFF
try {
return (Collator)((Collator)service.get(locale)).clone();
}
catch (CloneNotSupportedException e) {
throw new InternalError(e.getMessage());
}
///CLOVER:ON
}
return shim.getInstance(locale);
}
///CLOVER:ON
/**
* Register a collator as the default collator for the provided locale. The
* collator should not be modified after it is registered.
*
* @param collator the collator to register
* @param locale the locale for which this is the default collator
* @return an object that can be used to unregister the registered collator.
*
* @draft ICU 2.6
*/
public static final Object registerInstance(Collator collator, Locale locale) {
return getShim().registerInstance(collator, locale);
}
/**
* Register a collator factory.
*
* @param factory the factory to register
* @return an object that can be used to unregister the registered factory.
*
* @draft ICU 2.6
*/
public static final Object registerFactory(CollatorFactory factory) {
return getShim().registerFactory(factory);
}
/**
* Unregister a collator previously registered using registerInstance.
* @param registryKey the object previously returned by registerInstance.
* @return true if the collator was successfully unregistered.
* @draft ICU 2.6
*/
public static final boolean unregister(Object registryKey) {
if (shim == null) {
return false;
}
return shim.unregister(registryKey);
}
/**
* Get the set of Locales for which Collators are installed.
* @return the list of available locales which collators are installed.
@ -349,16 +457,37 @@ public abstract class Collator implements Comparator, Cloneable
* be the set of Locales that are installed with ICU4J.
* @draft ICU 2.4
*/
public static final Locale[] getAvailableLocales() {
if (service != null) {
return service.getAvailableLocales();
} else {
///CLOVER:OFF
public static Locale[] getAvailableLocales() {
if (shim == null) {
return ICULocaleData.getAvailableLocales();
///CLOVER:ON
}
return shim.getAvailableLocales();
}
/**
* Return a mapping of display names to collators, localized to the provided locale.
* @draft 2.6
*/
static final Map getDisplayNames(Locale displayLocale) {
return getShim().getDisplayNames(displayLocale);
}
/**
* Get the name of the collator for the objectLocale, localized for the displayLocale.
* @draft 2.6
*/
static public String getDisplayName(Locale objectLocale, Locale displayLocale) {
return getShim().getDisplayName(objectLocale, displayLocale);
}
/**
* Return a mapping of display names to collators, localized to the provided locale.
* Get the name of the collator for the objectLocale, localized for the current locale.
* @draft 2.6
*/
static public String getDisplayName(Locale objectLocale) {
return getShim().getDisplayName(objectLocale, Locale.getDefault());
}
/**
* <p>Returns this Collator's strength property. The strength property
@ -573,27 +702,6 @@ public abstract class Collator implements Comparator, Cloneable
// package private methods -----------------------------------------------
/* @prototype */
/* public */ static final Object register(Collator collator, Locale locale) {
return getService().registerObject(collator, locale);
}
/* @prototype */
/* public */ static final boolean unregister(Object registryKey) {
if (service != null) {
return service.unregisterFactory((Factory)registryKey);
}
return false;
}
////CLOVER:OFF
/* @prototype */
/* public */ static final Map getDisplayNames(Locale locale) {
Collator col = Collator.getInstance(locale);
return getService().getDisplayNames(locale, col, null);
}
////CLOVER:ON
// private data members --------------------------------------------------
/**

View File

@ -0,0 +1,134 @@
/**
*******************************************************************************
* Copyright (C) 2003, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/text/CollatorServiceShim.java,v $
* $Date: 2003/04/19 00:01:53 $
* $Revision: 1.1 $
*
*******************************************************************************
*/
package com.ibm.icu.text;
import java.util.Comparator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import com.ibm.icu.impl.ICULocaleData;
import com.ibm.icu.impl.ICULocaleService;
import com.ibm.icu.impl.ICULocaleService.ICUResourceBundleFactory;
import com.ibm.icu.impl.ICULocaleService.LocaleKeyFactory;
import com.ibm.icu.impl.ICUService;
import com.ibm.icu.impl.ICUService.Factory;
import com.ibm.icu.impl.LocaleUtility;
import com.ibm.icu.text.Collator.CollatorFactory;
final class CollatorServiceShim extends Collator.ServiceShim {
Collator getInstance(Locale locale) {
if (service == null) {
return new RuleBasedCollator(locale);
}
try {
return (Collator)((Collator)service.get(locale)).clone();
}
catch (CloneNotSupportedException e) {
throw new InternalError(e.getMessage());
}
}
Object registerInstance(Collator collator, Locale locale) {
return getService().registerObject(collator, locale);
}
Object registerFactory(CollatorFactory f) {
class CFactory extends LocaleKeyFactory {
CollatorFactory delegate;
CFactory(CollatorFactory f) {
super(f.visible()
? LocaleKeyFactory.VISIBLE
: LocaleKeyFactory.INVISIBLE,
"CFactory");
this.delegate = f;
}
public Object handleCreate(Locale loc, int kind, ICUService service) {
Object coll = delegate.createCollator(loc);
return coll;
}
public String getDisplayName(String id, Locale displayLocale) {
Locale objectLocale = LocaleUtility.getLocaleFromName(id);
return delegate.getDisplayName(objectLocale, displayLocale);
}
public Set getSupportedIDs() {
return delegate.getSupportedLocaleIDs();
}
}
return getService().registerFactory(new CFactory(f));
}
boolean unregister(Object registryKey) {
if (service == null) {
return false;
}
return service.unregisterFactory((Factory)registryKey);
}
Locale[] getAvailableLocales() {
if (service == null) {
return ICULocaleData.getAvailableLocales();
}
return service.getAvailableLocales();
}
Map getDisplayNames(Locale locale) {
Collator col = Collator.getInstance(locale);
return getService().getDisplayNames(locale, col, null);
}
String getDisplayName(Locale objectLocale, Locale displayLocale) {
String id = LocaleUtility.canonicalLocaleString(objectLocale);
return getService().getDisplayName(id, displayLocale);
}
private static ICULocaleService service;
private static ICULocaleService getService() {
if (service == null) {
ICULocaleService newService = new ICULocaleService("Collator") {
protected Object handleDefault(Key key, String[] actualIDReturn) {
if (actualIDReturn != null) {
actualIDReturn[0] = "root";
}
return new RuleBasedCollator(new Locale("", "", ""));
}
};
class CollatorFactory extends ICUResourceBundleFactory {
protected Object handleCreate(Locale loc, int kind, ICUService service) {
return new RuleBasedCollator(loc);
}
protected Set getSupportedIDs() {
return ICULocaleData.getAvailableLocaleNameSet();
}
}
newService.registerFactory(new CollatorFactory());
synchronized (Collator.class) {
if (service == null) {
service = newService;
}
}
}
return service;
}
}