litesoft
@ 943
litesoft / trunk / DeviceDesktopTest / src / org / litesoft / locale / DerivedLocaleGraph.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
package org.litesoft.locale; import java.util.*; @SuppressWarnings("Convert2Diamond") public class DerivedLocaleGraph { private static final AbstractLocale BASE = Locale_en_US.INSTANCE; private static final Map<String, AbstractLocale> DEFAULT_LOCALE_BY_LANGUAGE = new HashMap<String, AbstractLocale>(); private static final Map<String, AbstractLocale> LOCALE_BY_CODE = new HashMap<String, AbstractLocale>(); private static final Map<AbstractLocale, AbstractLocale> DERIVED_FROM_LOCALE_BY_LOCALE = new HashMap<AbstractLocale, AbstractLocale>(); static { addToDirectMaps( BASE ); add( Locale_en_US.INSTANCE, Locale_en_GB.INSTANCE, Locale_en_CA.INSTANCE ); add( Locale_es_ES.INSTANCE, Locale_es_MX.INSTANCE ); add( Locale_fr_FR.INSTANCE, Locale_fr_CA.INSTANCE ); add( Locale_zh_CN.INSTANCE, Locale_zh_TW.INSTANCE ); add( Locale_de_DE.INSTANCE ); // , Locale_de_AT.INSTANCE, Locale_de_CH.INSTANCE ); add( Locale_it_IT.INSTANCE ); // , Locale_it_CH.INSTANCE ); add( Locale_ja_JP.INSTANCE ); add( Locale_ko_KR.INSTANCE ); } public static AbstractLocale select( Set<AbstractLocale> pSupported, List<String> pOrderedCodes ) { AbstractLocale zBest = BASE; if ( pOrderedCodes != null ) { for ( String zCode : pOrderedCodes ) { AbstractLocale zCandidate = candidateForFrom( zCode, pSupported ); if ( zCandidate.mDepth > zBest.mDepth ) { zBest = zCandidate; } } } return zBest; } private static AbstractLocale candidateForFrom( String pCode, Set<AbstractLocale> pSupported ) { AbstractLocale zLocale = LOCALE_BY_CODE.get( pCode ); if ( null == zLocale ) { if ( null == (zLocale = DEFAULT_LOCALE_BY_LANGUAGE.get( AbstractLocale.languageFrom( pCode ) )) ) { return BASE; } } while ( !pSupported.contains( zLocale ) ) { if ( null == (zLocale = DERIVED_FROM_LOCALE_BY_LOCALE.get( zLocale )) ) { return BASE; } } return zLocale; } public static void add( AbstractLocale pBaseOrDerivedFrom, AbstractLocale... pDerived ) { if ( addToDirectMaps( pBaseOrDerivedFrom ) ) { setDerived( BASE, pBaseOrDerivedFrom ); } for ( AbstractLocale zLocale : pDerived ) { addToDirectMaps( zLocale ); setDerived( pBaseOrDerivedFrom, zLocale ); } } private static void setDerived( AbstractLocale pDerivedFrom, AbstractLocale pDerived ) { pDerived.mDepth = pDerivedFrom.mDepth + 1; DERIVED_FROM_LOCALE_BY_LOCALE.put( pDerived, pDerivedFrom ); } /** * @param pLocale !null * * @return true - If not already in the data structures */ private static boolean addToDirectMaps( AbstractLocale pLocale ) { if ( null != LOCALE_BY_CODE.put( pLocale.getCode(), pLocale ) ) { return false; // Already There! } // New, so might be the first of this language if ( !DEFAULT_LOCALE_BY_LANGUAGE.containsKey( pLocale.getLanguage() ) ) { DEFAULT_LOCALE_BY_LANGUAGE.put( pLocale.getLanguage(), pLocale ); } return true; // Added } private DerivedLocaleGraph() { } } |
Commits for litesoft/trunk/DeviceDesktopTest/src/org/litesoft/locale/DerivedLocaleGraph.java
Revision | Author | Commited | Message |
---|---|---|---|
943 Diff | GeorgeS | Tue 03 Jun, 2014 04:25:50 +0000 | Extracting commonfoundation |
939 Diff | GeorgeS | Mon 02 Jun, 2014 21:30:31 +0000 | Extracting commonfoundation |
935 Diff | GeorgeS | Fri 30 May, 2014 20:28:08 +0000 | Reformatted. |
933 Diff | GeorgeS | Fri 04 Apr, 2014 22:08:13 +0000 | Locale Work w/ Tests done! |
931 Diff | GeorgeS | Tue 01 Apr, 2014 20:11:20 +0000 | Locale Support. |
930 Diff | GeorgeS | Mon 31 Mar, 2014 01:47:32 +0000 | Locale Work... |
929 | GeorgeS | Mon 31 Mar, 2014 01:42:14 +0000 | Locale Work... |