litesoft
@ 935
litesoft / trunk / DeviceDesktopTest / src / org / litesoft / locale / AbstractLocale.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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
package org.litesoft.locale; public abstract class AbstractLocale { public static final String BAD_LANGUAGE_OR_COUNTRY = "??"; private static final String LOCALE_CLASS_NAME_PREFIX = ".Locale_"; private final boolean mActive; private final String mLanguage, mCountry, mCode; /* friend( DerivedLocaleGraph ) */ int mDepth; protected AbstractLocale( boolean pActive ) { mActive = pActive; String zName = "." + this.getClass().getName(); int zAt = zName.indexOf( LOCALE_CLASS_NAME_PREFIX ); if ( zAt == -1 ) { throw new IllegalStateException( "'" + LOCALE_CLASS_NAME_PREFIX + "' not found in: " + zName ); } String zLanguageCountry = zName.substring( zAt + LOCALE_CLASS_NAME_PREFIX.length() ); if ( !isValidStructuredCode( zLanguageCountry ) ) { throw new IllegalStateException( "No Language & Country found in: " + zName ); } mLanguage = assertLowerCase( "Language", codeToLanguage( zLanguageCountry ) ); mCountry = assertUpperCase( "Country", codeToCountry( zLanguageCountry ) ); mCode = toCode( mLanguage, mCountry ); } private static String assertLowerCase( String pWhat, String pString ) { return assertCase( pWhat, pString, pString.toLowerCase(), "LowerCase" ); } private static String assertUpperCase( String pWhat, String pString ) { return assertCase( pWhat, pString, pString.toUpperCase(), "UpperCase" ); } private static String assertCase( String pWhat, String pString, String pAdjustedString, String pCase ) { if ( pString.equals( pAdjustedString ) ) { return pString; } throw new IllegalStateException( pWhat + " '" + pString + "' NOT " + pCase ); } protected AbstractLocale() { this( true ); } public boolean isActive() { return mActive; } public String getLanguage() { return mLanguage; } public String getCountry() { return mCountry; } public String getCode() { return mCode; } @Override public boolean equals( Object o ) { return (this == o) || ((o instanceof AbstractLocale) && equals( (AbstractLocale) o )); } public boolean equals( AbstractLocale them ) { return (this == them) || ((them != null) && this.mCode.equals( them.mCode )); } @Override public int hashCode() { return mCode.hashCode(); } @Override public String toString() { return getCode(); } public static String languageFrom( String pCode ) { return isValidStructuredCode( pCode ) ? codeToLanguage( pCode ) : BAD_LANGUAGE_OR_COUNTRY; } public static String countryFrom( String pCode ) { return isValidStructuredCode( pCode ) ? codeToCountry( pCode ) : BAD_LANGUAGE_OR_COUNTRY; } public static String toCode( String pLanguage, String pCountry ) { return ensure2Char( pLanguage ).toLowerCase() + "_" + ensure2Char( pCountry ).toUpperCase(); } private static boolean isValidStructuredCode( String pCode ) { return (pCode != null) && (pCode.length() == 5) && (pCode.charAt( 2 ) == '_'); } private static String codeToLanguage( String pLanguageCountry ) { return pLanguageCountry.substring( 0, 2 ); } private static String codeToCountry( String pLanguageCountry ) { return pLanguageCountry.substring( 3 ); } private static String ensure2Char( String pString ) { if ( pString != null ) { if ( (pString = pString.trim()).length() == 2 ) { return pString; } } return BAD_LANGUAGE_OR_COUNTRY; } } |