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