Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
139 additions
and
139 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,139 +1,139 @@ | ||
package cn.gb2260; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.regex.Pattern; | ||
|
||
public class GB2260 { | ||
private final Revision revision; | ||
private HashMap<String, String> data; | ||
private ArrayList<Division> provinces; | ||
|
||
public GB2260() { | ||
this(Revision.V2014); | ||
} | ||
|
||
public GB2260(Revision revision) { | ||
this.revision = revision; | ||
data = new HashMap<String, String>(); | ||
provinces = new ArrayList<Division>(); | ||
InputStream inputStream = getClass().getResourceAsStream("/data/" + revision.getCode() + ".txt"); | ||
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream)); | ||
try { | ||
while (r.ready()) { | ||
String line = r.readLine(); | ||
String[] split = line.split("\t"); | ||
String code = split[0]; | ||
String name = split[1]; | ||
|
||
data.put(code, name); | ||
|
||
if (Pattern.matches("^\\d{2}0{4}$", code)) { | ||
Division division = new Division(); | ||
division.setCode(code); | ||
division.setName(name); | ||
provinces.add(division); | ||
} | ||
} | ||
} catch (IOException e) { | ||
System.err.println("Error in loading GB2260 data!"); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public Division getDivision(String code) { | ||
if (code.length() != 6) { | ||
throw new InvalidCodeException("Invalid code"); | ||
} | ||
|
||
if (!data.containsKey(code)) { | ||
return null; | ||
} | ||
|
||
Division division = new Division(); | ||
division.setName(data.get(code)); | ||
division.setRevision(getRevision().getCode()); | ||
division.setCode(code); | ||
|
||
if (Pattern.matches("^\\d{2}0{4}$", code)) { | ||
return division; | ||
} | ||
|
||
String provinceCode = code.substring(0, 2) + "0000"; | ||
division.setProvince(data.get(provinceCode)); | ||
|
||
if (Pattern.matches("^\\d{4}0{2}$", code)) { | ||
return division; | ||
} | ||
|
||
String prefectureCode = code.substring(0, 4) + "00"; | ||
division.setPrefecture(data.get(prefectureCode)); | ||
|
||
division.setRevision(this.revision.getCode()); | ||
return division; | ||
} | ||
|
||
public Revision getRevision() { | ||
return revision; | ||
} | ||
|
||
public ArrayList<Division> getProvinces() { | ||
return provinces; | ||
} | ||
|
||
public ArrayList<Division> getPrefectures(String code) { | ||
ArrayList<Division> rv = new ArrayList<Division>(); | ||
|
||
if (!Pattern.matches("^\\d{2}0{4}$", code)) { | ||
throw new InvalidCodeException("Invalid province code"); | ||
} | ||
|
||
if (!data.containsKey(code)) { | ||
throw new InvalidCodeException("Province code not found"); | ||
} | ||
|
||
Division province = getDivision(code); | ||
|
||
Pattern pattern = Pattern.compile("^" + code.substring(0, 2) + "\\d{2}00$"); | ||
for (String key : data.keySet()) { | ||
if (pattern.matcher(key).matches()) { | ||
Division division = getDivision(key); | ||
division.setProvince(province.getName()); | ||
rv.add(division); | ||
} | ||
} | ||
|
||
return rv; | ||
} | ||
|
||
public ArrayList<Division> getCounties(String code) { | ||
ArrayList<Division> rv = new ArrayList<Division>(); | ||
|
||
if (!Pattern.matches("^\\d+[1-9]0{2,3}$", code)) { | ||
throw new InvalidCodeException("Invalid prefecture code"); | ||
} | ||
|
||
if (!data.containsKey(code)) { | ||
throw new InvalidCodeException("Prefecture code not found"); | ||
} | ||
|
||
Division prefecture = getDivision(code); | ||
Division province = getDivision(code.substring(0, 2) + "0000"); | ||
|
||
Pattern pattern = Pattern.compile("^" + code.substring(0, 4) + "\\d+$"); | ||
for (String key : data.keySet()) { | ||
if (pattern.matcher(key).matches()) { | ||
Division division = getDivision(key); | ||
division.setProvince(province.getName()); | ||
division.setPrefecture(prefecture.getName()); | ||
rv.add(division); | ||
} | ||
} | ||
|
||
return rv; | ||
} | ||
} | ||
package cn.gb2260; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.regex.Pattern; | ||
|
||
public class GB2260 { | ||
private final Revision revision; | ||
private HashMap<String, String> data; | ||
private ArrayList<Division> provinces; | ||
|
||
public GB2260() { | ||
this(Revision.V2014); | ||
} | ||
|
||
public GB2260(Revision revision) { | ||
this.revision = revision; | ||
data = new HashMap<String, String>(); | ||
provinces = new ArrayList<Division>(); | ||
InputStream inputStream = getClass().getResourceAsStream("/data/" + revision.getCode() + ".txt"); | ||
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream)); | ||
try { | ||
while (r.ready()) { | ||
String line = r.readLine(); | ||
String[] split = line.split("\t"); | ||
String code = split[0]; | ||
String name = split[1]; | ||
|
||
data.put(code, name); | ||
|
||
if (Pattern.matches("^\\d{2}0{4}$", code)) { | ||
Division division = new Division(); | ||
division.setCode(code); | ||
division.setName(name); | ||
provinces.add(division); | ||
} | ||
} | ||
} catch (IOException e) { | ||
System.err.println("Error in loading GB2260 data!"); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public Division getDivision(String code) { | ||
if (code.length() != 6) { | ||
throw new InvalidCodeException("Invalid code"); | ||
} | ||
|
||
if (!data.containsKey(code)) { | ||
return null; | ||
} | ||
|
||
Division division = new Division(); | ||
division.setName(data.get(code)); | ||
division.setRevision(getRevision().getCode()); | ||
division.setCode(code); | ||
|
||
if (Pattern.matches("^\\d{2}0{4}$", code)) { | ||
return division; | ||
} | ||
|
||
String provinceCode = code.substring(0, 2) + "0000"; | ||
division.setProvince(data.get(provinceCode)); | ||
|
||
if (Pattern.matches("^\\d{4}0{2}$", code)) { | ||
return division; | ||
} | ||
|
||
String prefectureCode = code.substring(0, 4) + "00"; | ||
division.setPrefecture(data.get(prefectureCode)); | ||
|
||
division.setRevision(this.revision.getCode()); | ||
return division; | ||
} | ||
|
||
public Revision getRevision() { | ||
return revision; | ||
} | ||
|
||
public ArrayList<Division> getProvinces() { | ||
return provinces; | ||
} | ||
|
||
public ArrayList<Division> getPrefectures(String code) { | ||
ArrayList<Division> rv = new ArrayList<Division>(); | ||
|
||
if (!Pattern.matches("^\\d{2}0{4}$", code)) { | ||
throw new InvalidCodeException("Invalid province code"); | ||
} | ||
|
||
if (!data.containsKey(code)) { | ||
throw new InvalidCodeException("Province code not found"); | ||
} | ||
|
||
Division province = getDivision(code); | ||
|
||
Pattern pattern = Pattern.compile("^" + code.substring(0, 2) + "\\d{2}00$"); | ||
for (String key : data.keySet()) { | ||
if (pattern.matcher(key).matches() && !code.equals(key)) { | ||
Division division = getDivision(key); | ||
division.setProvince(province.getName()); | ||
rv.add(division); | ||
} | ||
} | ||
|
||
return rv; | ||
} | ||
|
||
public ArrayList<Division> getCounties(String code) { | ||
ArrayList<Division> rv = new ArrayList<Division>(); | ||
|
||
if (!Pattern.matches("^\\d+[1-9]0{2,3}$", code)) { | ||
throw new InvalidCodeException("Invalid prefecture code"); | ||
} | ||
|
||
if (!data.containsKey(code)) { | ||
throw new InvalidCodeException("Prefecture code not found"); | ||
} | ||
|
||
Division prefecture = getDivision(code); | ||
Division province = getDivision(code.substring(0, 2) + "0000"); | ||
|
||
Pattern pattern = Pattern.compile("^" + code.substring(0, 4) + "\\d+$"); | ||
for (String key : data.keySet()) { | ||
if (pattern.matcher(key).matches() && !code.equals(key)) { | ||
Division division = getDivision(key); | ||
division.setProvince(province.getName()); | ||
division.setPrefecture(prefecture.getName()); | ||
rv.add(division); | ||
} | ||
} | ||
|
||
return rv; | ||
} | ||
} |