forked from TakahikoKawasaki/nv-i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support user-assigned country codes. (See issue TakahikoKawasaki#25.)
- Loading branch information
Derek Mahar
committed
Jul 15, 2015
1 parent
79ebefc
commit f52d6b0
Showing
3 changed files
with
221 additions
and
1 deletion.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
src/main/java/com/neovisionaries/i18n/CustomCountryCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright (C) 2015 Opes Software Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.neovisionaries.i18n; | ||
|
||
import com.neovisionaries.i18n.CountryCode.Assignment; | ||
import java.util.Currency; | ||
import java.util.Locale; | ||
|
||
/** | ||
* | ||
* @author Derek Mahar <[email protected]> | ||
*/ | ||
public interface CustomCountryCode { | ||
String getAlpha2(); | ||
String getAlpha3(); | ||
Assignment getAssignment(); | ||
Currency getCurrency(); | ||
String getName(); | ||
int getNumeric(); | ||
Locale toLocale(); | ||
} |
119 changes: 119 additions & 0 deletions
119
src/main/java/com/neovisionaries/i18n/StandardOrUserAssignedCountryCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright (C) 2015 Opes Software Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.neovisionaries.i18n; | ||
|
||
import com.neovisionaries.i18n.CountryCode.Assignment; | ||
import java.util.Currency; | ||
import java.util.HashMap; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
|
||
/** | ||
* | ||
* @author Derek Mahar <[email protected]> | ||
*/ | ||
public class StandardOrUserAssignedCountryCode implements CustomCountryCode { | ||
|
||
public static Map<String, CustomCountryCode> alphaCountryCodes = new HashMap<String, CustomCountryCode>(); | ||
public static Map<Integer, CustomCountryCode> numericCountryCodes = new HashMap<Integer, CustomCountryCode>(); | ||
|
||
public static void addCode(CustomCountryCode code) { | ||
if (code.getNumeric() != -1) { | ||
numericCountryCodes.put(code.getNumeric(), code); | ||
} | ||
|
||
if (code.getAlpha2() != null) { | ||
alphaCountryCodes.put(code.getAlpha2(), code); | ||
} | ||
|
||
if (code.getAlpha3() != null) { | ||
alphaCountryCodes.put(code.getAlpha3(), code); | ||
} | ||
} | ||
|
||
public static CustomCountryCode getByCode(int code) { | ||
return new StandardOrUserAssignedCountryCode(CountryCode.getByCode(code), | ||
numericCountryCodes.get(code)); | ||
} | ||
|
||
public static CustomCountryCode getByCode(String code) { | ||
return new StandardOrUserAssignedCountryCode(CountryCode.getByCode(code), | ||
alphaCountryCodes.get(code)); | ||
} | ||
|
||
public static CustomCountryCode getByCodeIgnoreCase(String code) { | ||
return getByCode(code.toUpperCase()); | ||
} | ||
|
||
private final CountryCode standardCountryCode; | ||
private final CustomCountryCode userAssignedCountryCode; | ||
|
||
public StandardOrUserAssignedCountryCode(CountryCode standardCountryCode) { | ||
assert (standardCountryCode != null); | ||
|
||
this.standardCountryCode = standardCountryCode; | ||
this.userAssignedCountryCode = null; | ||
} | ||
|
||
public StandardOrUserAssignedCountryCode(CustomCountryCode userAssignedCountryCode) { | ||
assert (userAssignedCountryCode != null); | ||
|
||
this.standardCountryCode = null; | ||
this.userAssignedCountryCode = userAssignedCountryCode; | ||
} | ||
|
||
public StandardOrUserAssignedCountryCode(CountryCode standardCountryCode, CustomCountryCode userAssignedCountryCode) { | ||
assert (standardCountryCode == null | userAssignedCountryCode == null); | ||
|
||
this.standardCountryCode = standardCountryCode; | ||
this.userAssignedCountryCode = userAssignedCountryCode; | ||
} | ||
|
||
public String getAlpha2() { | ||
return this.standardCountryCode != null ? this.standardCountryCode.getAlpha2() | ||
: this.userAssignedCountryCode.getAlpha2(); | ||
} | ||
|
||
public String getAlpha3() { | ||
return this.standardCountryCode != null ? this.standardCountryCode.getAlpha3() | ||
: this.userAssignedCountryCode.getAlpha3(); | ||
} | ||
|
||
public Assignment getAssignment() { | ||
return this.standardCountryCode != null ? this.standardCountryCode.getAssignment() | ||
: this.userAssignedCountryCode.getAssignment(); | ||
} | ||
|
||
public Currency getCurrency() { | ||
return this.standardCountryCode != null ? this.standardCountryCode.getCurrency() | ||
: this.userAssignedCountryCode.getCurrency(); | ||
} | ||
|
||
public String getName() { | ||
return this.standardCountryCode != null ? this.standardCountryCode.getName() | ||
: this.userAssignedCountryCode.getName(); | ||
} | ||
|
||
public int getNumeric() { | ||
return this.standardCountryCode != null ? this.standardCountryCode.getNumeric() | ||
: this.userAssignedCountryCode.getNumeric(); | ||
} | ||
|
||
public Locale toLocale() { | ||
return this.standardCountryCode != null ? this.standardCountryCode.toLocale() | ||
: this.userAssignedCountryCode.toLocale(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters