src/java.base/share/classes/sun/util/locale/provider/CollatorProviderImpl.java

Print this page
rev 10528 : 8038436: Re-examine the mechanism to determine available localedata and cldrdata
Reviewed-by:
   1 /*
   2  * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  37  * This notice and attribution to Taligent may not be removed.
  38  * Taligent is a registered trademark of Taligent, Inc.
  39  */
  40 
  41 package sun.util.locale.provider;
  42 
  43 import java.text.Collator;
  44 import java.text.ParseException;
  45 import java.text.RuleBasedCollator;
  46 import java.text.spi.CollatorProvider;
  47 import java.util.Locale;
  48 import java.util.Set;
  49 
  50 /**
  51  * Concrete implementation of the
  52  * {@link java.text.spi.CollatorProvider CollatorProvider} class
  53  * for the JRE LocaleProviderAdapter.
  54  */
  55 public class CollatorProviderImpl extends CollatorProvider implements AvailableLanguageTags {
  56     private final LocaleProviderAdapter.Type type;
  57     private final Set<String> langtags;
  58 
  59     public CollatorProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
  60         this.type = type;
  61         this.langtags = langtags;
  62     }
  63 
  64     /**
  65      * Returns an array of all locales for which this locale service provider
  66      * can provide localized objects or names.
  67      *
  68      * @return An array of all locales for which this locale service provider
  69      * can provide localized objects or names.
  70      */
  71     @Override
  72     public Locale[] getAvailableLocales() {
  73         return LocaleProviderAdapter.toLocaleArray(langtags);
  74     }
  75 
  76     @Override
  77     public boolean isSupportedLocale(Locale locale) {
  78         return LocaleProviderAdapter.isSupportedLocale(locale, type, langtags);
  79     }
  80 
  81     /**
  82      * Returns a new <code>Collator</code> instance for the specified locale.
  83      * @param locale the desired locale.
  84      * @return the <code>Collator</code> for the desired locale.
  85      * @exception NullPointerException if
  86      * <code>locale</code> is null
  87      * @exception IllegalArgumentException if <code>locale</code> isn't
  88      *     one of the locales returned from
  89      *     {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
  90      *     getAvailableLocales()}.
  91      * @see java.text.Collator#getInstance(java.util.Locale)
  92      */
  93     @Override
  94     public Collator getInstance(Locale locale) {
  95         if (locale == null) {
  96             throw new NullPointerException();
  97         }
  98 


 109         catch(ParseException foo)
 110         {
 111             // predefined tables should contain correct grammar
 112             try {
 113                 result = new RuleBasedCollator(CollationRules.DEFAULTRULES);
 114             } catch (ParseException bar) {
 115                 // the default rules should always be parsable.
 116                 throw new InternalError(bar);
 117             }
 118         }
 119         // Now that RuleBasedCollator adds expansions for pre-composed characters
 120         // into their decomposed equivalents, the default collators don't need
 121         // to have decomposition turned on.  Laura, 5/5/98, bug 4114077
 122         result.setDecomposition(Collator.NO_DECOMPOSITION);
 123 
 124         return (Collator)result.clone();
 125     }
 126 
 127     @Override
 128     public Set<String> getAvailableLanguageTags() {







 129         return langtags;
 130     }
 131 }
   1 /*
   2  * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  37  * This notice and attribution to Taligent may not be removed.
  38  * Taligent is a registered trademark of Taligent, Inc.
  39  */
  40 
  41 package sun.util.locale.provider;
  42 
  43 import java.text.Collator;
  44 import java.text.ParseException;
  45 import java.text.RuleBasedCollator;
  46 import java.text.spi.CollatorProvider;
  47 import java.util.Locale;
  48 import java.util.Set;
  49 
  50 /**
  51  * Concrete implementation of the
  52  * {@link java.text.spi.CollatorProvider CollatorProvider} class
  53  * for the JRE LocaleProviderAdapter.
  54  */
  55 public class CollatorProviderImpl extends CollatorProvider implements AvailableLanguageTags {
  56     private final LocaleProviderAdapter.Type type;
  57     private Set<String> langtags;
  58 
  59     public CollatorProviderImpl(LocaleProviderAdapter.Type type) {
  60         this.type = type;

  61     }
  62 
  63     /**
  64      * Returns an array of all locales for which this locale service provider
  65      * can provide localized objects or names.
  66      *
  67      * @return An array of all locales for which this locale service provider
  68      * can provide localized objects or names.
  69      */
  70     @Override
  71     public Locale[] getAvailableLocales() {
  72         return LocaleProviderAdapter.toLocaleArray(getLangTags());
  73     }
  74 
  75     @Override
  76     public boolean isSupportedLocale(Locale locale) {
  77         return LocaleProviderAdapter.isSupportedLocale(locale, type, getLangTags());
  78     }
  79 
  80     /**
  81      * Returns a new <code>Collator</code> instance for the specified locale.
  82      * @param locale the desired locale.
  83      * @return the <code>Collator</code> for the desired locale.
  84      * @exception NullPointerException if
  85      * <code>locale</code> is null
  86      * @exception IllegalArgumentException if <code>locale</code> isn't
  87      *     one of the locales returned from
  88      *     {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
  89      *     getAvailableLocales()}.
  90      * @see java.text.Collator#getInstance(java.util.Locale)
  91      */
  92     @Override
  93     public Collator getInstance(Locale locale) {
  94         if (locale == null) {
  95             throw new NullPointerException();
  96         }
  97 


 108         catch(ParseException foo)
 109         {
 110             // predefined tables should contain correct grammar
 111             try {
 112                 result = new RuleBasedCollator(CollationRules.DEFAULTRULES);
 113             } catch (ParseException bar) {
 114                 // the default rules should always be parsable.
 115                 throw new InternalError(bar);
 116             }
 117         }
 118         // Now that RuleBasedCollator adds expansions for pre-composed characters
 119         // into their decomposed equivalents, the default collators don't need
 120         // to have decomposition turned on.  Laura, 5/5/98, bug 4114077
 121         result.setDecomposition(Collator.NO_DECOMPOSITION);
 122 
 123         return (Collator)result.clone();
 124     }
 125 
 126     @Override
 127     public Set<String> getAvailableLanguageTags() {
 128         return getLangTags();
 129     }
 130 
 131     private Set<String> getLangTags() {
 132         if (langtags == null) {
 133             langtags = ((JRELocaleProviderAdapter)LocaleProviderAdapter.forType(type)).getLanguageTagSet("CollationData");
 134         }
 135         return langtags;
 136     }
 137 }