|
@@ -1,90 +1,115 @@ |
1 |
1 |
|
package org.litesoft.locale; |
2 |
2 |
|
|
3 |
|
- |
public abstract class AbstractLocale |
4 |
|
- |
{ |
|
3 |
+ |
public abstract class AbstractLocale { |
|
4 |
+ |
public static final String BAD_LANGUAGE_OR_COUNTRY = "??"; |
|
5 |
+ |
|
5 |
6 |
|
private static final String LOCALE_CLASS_NAME_PREFIX = ".Locale_"; |
6 |
7 |
|
|
7 |
8 |
|
private final boolean mActive; |
8 |
9 |
|
private final String mLanguage, mCountry, mCode; |
|
10 |
+ |
/* friend( DerivedLocaleGraph ) */ int mDepth; |
9 |
11 |
|
|
10 |
|
- |
protected AbstractLocale( boolean pActive ) |
11 |
|
- |
{ |
|
12 |
+ |
protected AbstractLocale(boolean pActive) { |
12 |
13 |
|
mActive = pActive; |
13 |
14 |
|
String zName = "." + this.getClass().getName(); |
14 |
|
- |
int zAt = zName.indexOf( LOCALE_CLASS_NAME_PREFIX ); |
15 |
|
- |
if ( zAt == -1 ) |
16 |
|
- |
{ |
17 |
|
- |
throw new IllegalStateException( "'" + LOCALE_CLASS_NAME_PREFIX + "' not found in: " + zName ); |
|
15 |
+ |
int zAt = zName.indexOf(LOCALE_CLASS_NAME_PREFIX); |
|
16 |
+ |
if (zAt == -1) { |
|
17 |
+ |
throw new IllegalStateException("'" + LOCALE_CLASS_NAME_PREFIX + "' not found in: " + zName); |
18 |
18 |
|
} |
19 |
|
- |
String zLanguageCountry = zName.substring( zAt + LOCALE_CLASS_NAME_PREFIX.length() ); |
20 |
|
- |
if ( (zLanguageCountry.length() != 5) || (zLanguageCountry.charAt( 2 ) == '_') ) |
21 |
|
- |
{ |
22 |
|
- |
throw new IllegalStateException( "No Language & Country found in: " + zName ); |
|
19 |
+ |
String zLanguageCountry = zName.substring(zAt + LOCALE_CLASS_NAME_PREFIX.length()); |
|
20 |
+ |
if (!isValidStructuredCode(zLanguageCountry)) { |
|
21 |
+ |
throw new IllegalStateException("No Language & Country found in: " + zName); |
23 |
22 |
|
} |
24 |
|
- |
mLanguage = assertLowerCase( "Language", zLanguageCountry.substring( 0, 2 ) ); |
25 |
|
- |
mCountry = assertUpperCase( "Country", zLanguageCountry.substring( 3 ) ); |
26 |
|
- |
mCode = mLanguage + "_" + mCountry; |
|
23 |
+ |
mLanguage = assertLowerCase("Language", codeToLanguage(zLanguageCountry)); |
|
24 |
+ |
mCountry = assertUpperCase("Country", codeToCountry(zLanguageCountry)); |
|
25 |
+ |
mCode = toCode(mLanguage, mCountry); |
27 |
26 |
|
} |
28 |
27 |
|
|
29 |
|
- |
private static String assertLowerCase( String pWhat, String pString ) |
30 |
|
- |
{ |
31 |
|
- |
return assertCase( pWhat, pString, pString.toLowerCase(), "LowerCase" ); |
|
28 |
+ |
private static String assertLowerCase(String pWhat, String pString) { |
|
29 |
+ |
return assertCase(pWhat, pString, pString.toLowerCase(), "LowerCase"); |
32 |
30 |
|
} |
33 |
31 |
|
|
34 |
|
- |
private static String assertUpperCase( String pWhat, String pString ) |
35 |
|
- |
{ |
36 |
|
- |
return assertCase( pWhat, pString, pString.toUpperCase(), "UpperCase" ); |
|
32 |
+ |
private static String assertUpperCase(String pWhat, String pString) { |
|
33 |
+ |
return assertCase(pWhat, pString, pString.toUpperCase(), "UpperCase"); |
37 |
34 |
|
} |
38 |
35 |
|
|
39 |
|
- |
private static String assertCase( String pWhat, String pString, String pAdjustedString, String pCase ) |
40 |
|
- |
{ |
41 |
|
- |
if ( pString.equals( pAdjustedString ) ) |
42 |
|
- |
{ |
|
36 |
+ |
private static String assertCase(String pWhat, String pString, String pAdjustedString, String pCase) { |
|
37 |
+ |
if (pString.equals(pAdjustedString)) { |
43 |
38 |
|
return pString; |
44 |
39 |
|
} |
45 |
|
- |
throw new IllegalStateException( pWhat + " '" + pString + "' NOT " + pCase ); |
|
40 |
+ |
throw new IllegalStateException(pWhat + " '" + pString + "' NOT " + pCase); |
46 |
41 |
|
} |
47 |
42 |
|
|
48 |
|
- |
protected AbstractLocale() |
49 |
|
- |
{ |
50 |
|
- |
this( true ); |
|
43 |
+ |
protected AbstractLocale() { |
|
44 |
+ |
this(true); |
51 |
45 |
|
} |
52 |
46 |
|
|
53 |
|
- |
public boolean isActive() |
54 |
|
- |
{ |
|
47 |
+ |
public boolean isActive() { |
55 |
48 |
|
return mActive; |
56 |
49 |
|
} |
57 |
50 |
|
|
58 |
|
- |
public String getLanguage() |
59 |
|
- |
{ |
|
51 |
+ |
public String getLanguage() { |
60 |
52 |
|
return mLanguage; |
61 |
53 |
|
} |
62 |
54 |
|
|
63 |
|
- |
public String getCountry() |
64 |
|
- |
{ |
|
55 |
+ |
public String getCountry() { |
65 |
56 |
|
return mCountry; |
66 |
57 |
|
} |
67 |
58 |
|
|
68 |
|
- |
public String getCode() |
69 |
|
- |
{ |
|
59 |
+ |
public String getCode() { |
70 |
60 |
|
return mCode; |
71 |
61 |
|
} |
72 |
62 |
|
|
73 |
63 |
|
@Override |
74 |
|
- |
public boolean equals( Object o ) |
75 |
|
- |
{ |
76 |
|
- |
return (this == o) || ((o instanceof AbstractLocale) && equals( (AbstractLocale) o )); |
|
64 |
+ |
public boolean equals(Object o) { |
|
65 |
+ |
return (this == o) || ((o instanceof AbstractLocale) && equals((AbstractLocale) o)); |
77 |
66 |
|
} |
78 |
67 |
|
|
79 |
|
- |
public boolean equals( AbstractLocale them ) |
80 |
|
- |
{ |
|
68 |
+ |
public boolean equals(AbstractLocale them) { |
81 |
69 |
|
return (this == them) || ((them != null) |
82 |
|
- |
&& this.mCode.equals( them.mCode )); |
|
70 |
+ |
&& this.mCode.equals(them.mCode)); |
83 |
71 |
|
} |
84 |
72 |
|
|
85 |
73 |
|
@Override |
86 |
|
- |
public int hashCode() |
87 |
|
- |
{ |
|
74 |
+ |
public int hashCode() { |
88 |
75 |
|
return mCode.hashCode(); |
89 |
76 |
|
} |
|
77 |
+ |
|
|
78 |
+ |
@Override |
|
79 |
+ |
public String toString() { |
|
80 |
+ |
return getCode(); |
|
81 |
+ |
} |
|
82 |
+ |
|
|
83 |
+ |
public static String languageFrom(String pCode) { |
|
84 |
+ |
return isValidStructuredCode(pCode) ? codeToLanguage(pCode) : BAD_LANGUAGE_OR_COUNTRY; |
|
85 |
+ |
} |
|
86 |
+ |
|
|
87 |
+ |
public static String countryFrom(String pCode) { |
|
88 |
+ |
return isValidStructuredCode(pCode) ? codeToCountry(pCode) : BAD_LANGUAGE_OR_COUNTRY; |
|
89 |
+ |
} |
|
90 |
+ |
|
|
91 |
+ |
public static String toCode(String pLanguage, String pCountry) { |
|
92 |
+ |
return ensure2Char(pLanguage).toLowerCase() + "_" + ensure2Char(pCountry).toUpperCase(); |
|
93 |
+ |
} |
|
94 |
+ |
|
|
95 |
+ |
private static boolean isValidStructuredCode(String pCode) { |
|
96 |
+ |
return (pCode != null) && (pCode.length() == 5) && (pCode.charAt(2) == '_'); |
|
97 |
+ |
} |
|
98 |
+ |
|
|
99 |
+ |
private static String codeToLanguage(String pLanguageCountry) { |
|
100 |
+ |
return pLanguageCountry.substring(0, 2); |
|
101 |
+ |
} |
|
102 |
+ |
|
|
103 |
+ |
private static String codeToCountry(String pLanguageCountry) { |
|
104 |
+ |
return pLanguageCountry.substring(3); |
|
105 |
+ |
} |
|
106 |
+ |
|
|
107 |
+ |
private static String ensure2Char(String pString) { |
|
108 |
+ |
if (pString != null) { |
|
109 |
+ |
if ((pString = pString.trim()).length() == 2) { |
|
110 |
+ |
return pString; |
|
111 |
+ |
} |
|
112 |
+ |
} |
|
113 |
+ |
return BAD_LANGUAGE_OR_COUNTRY; |
|
114 |
+ |
} |
90 |
115 |
|
} |