Skip to content

Commit

Permalink
Support user-assigned country codes. (See issue TakahikoKawasaki#25.)
Browse files Browse the repository at this point in the history
  • Loading branch information
Derek Mahar committed Jul 15, 2015
1 parent 79ebefc commit f52d6b0
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/main/java/com/neovisionaries/i18n/CustomCountryCode.java
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();
}
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();
}
}
69 changes: 68 additions & 1 deletion src/test/java/com/neovisionaries/i18n/CountryCodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
package com.neovisionaries.i18n;


import com.neovisionaries.i18n.CountryCode.Assignment;
import static com.neovisionaries.i18n.CountryCode.getByCode;
import static com.neovisionaries.i18n.CountryCode.getByCodeIgnoreCase;
import static com.neovisionaries.i18n.CountryCode.getByLocale;
import java.util.Currency;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
Expand All @@ -30,7 +32,39 @@

public class CountryCodeTest
{
@Test
private static CustomCountryCode createUserAssignedCountryCode() {
return new CustomCountryCode() {
public String getName() {
return "USER_COUNTRY_CODE_1";
}

public String getAlpha2() {
return "ZZ";
}

public String getAlpha3() {
return "ZZZ";
}

public int getNumeric() {
return 999;
}

public Assignment getAssignment() {
return Assignment.USER_ASSIGNED;
}

public Locale toLocale() {
return null;
}

public Currency getCurrency() {
return null;
}
};
}

@Test
public void test1()
{
List<CountryCode> list = CountryCode.findByName(".*United.*");
Expand Down Expand Up @@ -185,4 +219,37 @@ public void test18()
{
assertNull(getByCode(""));
}

@Test
public void testUserAssignedCountryCodeGetByCodeAlpha2() {
final CustomCountryCode expected = createUserAssignedCountryCode();
StandardOrUserAssignedCountryCode.addCode(expected);
final CustomCountryCode actual = StandardOrUserAssignedCountryCode.getByCode("ZZ");
assertEquals("ZZ", actual.getAlpha2());
assertEquals("ZZZ", actual.getAlpha3());
assertEquals(999, actual.getNumeric());
assertEquals(Assignment.USER_ASSIGNED, actual.getAssignment());
}

@Test
public void testUserAssignedCountryCodeGetByCodeAlpha3() {
final CustomCountryCode expected = createUserAssignedCountryCode();
StandardOrUserAssignedCountryCode.addCode(expected);
final CustomCountryCode actual = StandardOrUserAssignedCountryCode.getByCode("ZZZ");
assertEquals("ZZ", actual.getAlpha2());
assertEquals("ZZZ", actual.getAlpha3());
assertEquals(999, actual.getNumeric());
assertEquals(Assignment.USER_ASSIGNED, actual.getAssignment());
}

@Test
public void testUserAssignedCountryCodeGetByCodeNumeric() {
final CustomCountryCode expected = createUserAssignedCountryCode();
StandardOrUserAssignedCountryCode.addCode(expected);
final CustomCountryCode actual = StandardOrUserAssignedCountryCode.getByCode(999);
assertEquals("ZZ", actual.getAlpha2());
assertEquals("ZZZ", actual.getAlpha3());
assertEquals(999, actual.getNumeric());
assertEquals(Assignment.USER_ASSIGNED, actual.getAssignment());
}
}

0 comments on commit f52d6b0

Please sign in to comment.