CityHelper
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.logging.log4j.util.Strings;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
public class CityHelper {
private static final String SPLIT = " / ";
private static CityHelper instance = new CityHelper();
public static CityHelper getInstance() {
return instance;
}
public CityBean get(Integer cityId) {
return cloneCity(ID_INDEX_MAP.get(cityId));
}
public List<CityBean> get(Collection<Integer> cityIds) {
return cityIds.stream().map(this::get).filter(Objects::nonNull)
.collect(Collectors.toList());
}
public List<CityBean> getAllCity() {
return get(ID_INDEX_MAP.keySet());
}
public Map<Integer, CityBean> getCityMap(Collection<Integer> cityIds) {
return get(cityIds).stream().collect(Collectors.toMap(CityBean::getId, Function.identity(), (k1, k2) -> k2));
}
public Map<Integer,CityBean> getAll(){
return ID_INDEX_MAP;
}
private final Map<Integer, CityBean> ID_INDEX_MAP = new HashMap<>();
private void add(CityBean cityBean) {
ID_INDEX_MAP.put(cityBean.getId(), cityBean);
}
private CityBean newCityBean(Integer id, String name, Integer parentId, String parentName, Integer rootId, String rootName) {
CityBean cityBean = new CityBean();
cityBean.setId(id);
cityBean.setName(name);
cityBean.setParentId(parentId);
cityBean.setParentName(parentName);
cityBean.setRootId(rootId);
cityBean.setRootName(rootName);
return cityBean;
}
private CityBean cloneCity(CityBean cityBean) {
if (cityBean == null) {
return null;
}
CityBean clone = new CityBean();
clone.setId(cityBean.getId());
clone.setName(cityBean.getName());
clone.setParentId(cityBean.getParentId());
clone.setParentName(cityBean.getParentName());
clone.setRootId(cityBean.getRootId());
clone.setRootName(cityBean.getRootName());
clone.setFullName(cityBean.getFullName());
clone.setSplitCityName(cityBean.getSplitCityName());
clone.setCityBeanList(cityBean.getCityBeanList());
clone.setResultStatusBean(cityBean.getResultStatusBean());
return clone;
}
private CityHelper() {
InputStream is = getClass().getResourceAsStream("/CITY");
BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
reader.lines().forEach(line -> {
String[] split = line.split(",");
Integer id = toInt(split[0]);
String name = StringUtils.trimToNull(split[1]);
Integer parentId = null;
String parentName = null;
Integer rootId = null;
String rootName = null;
if (split.length > 2) {
parentId = toInt(split[2]);
parentName = StringUtils.trimToNull(split[3]);
rootId = toInt(split[4]);
rootName = StringUtils.trimToNull(split[5]);
}
add(newCityBean(id, name, parentId, parentName, rootId, rootName));
});
}
private Integer toInt(String s) {
if (StringUtils.isEmpty(s)) {
return null;
} else {
return NumberUtils.toInt(s);
}
}
public static void main(String[] args) {
CityBean cityBean = getInstance().get(713803);
System.out.println(1);
}
public String getName(Integer cityId) {
return Optional.ofNullable(get(cityId)).map(CityBean::getName).orElse(null);
}
public static String buildCityNameBySplit(CityBean city) {
String rootName = filter(city.getRootName());
String parentName = filter(city.getParentName());
String name = filter(city.getName());
if (org.apache.commons.lang.StringUtils.equals(city.getRootName(), city.getParentName())) {
return rootName +
(org.apache.commons.lang.StringUtils.isNotBlank(rootName) && org.apache.commons.lang.StringUtils.isNotBlank(name) ? SPLIT : Strings.EMPTY) +
name;
} else {
return rootName + (org.apache.commons.lang.StringUtils.isNotBlank(rootName) ? SPLIT : Strings.EMPTY) +
parentName + (org.apache.commons.lang.StringUtils.isNotBlank(parentName) && org.apache.commons.lang.StringUtils.isNotBlank(name) ? SPLIT : Strings.EMPTY) +
name;
}
}
public static String buildCityName(CityBean city) {
return filter(city.getRootName()) +
filter(city.getParentName()) +
filter(city.getName());
}
public static boolean isCity(Integer cityId) {
return cityId % 100 == 0;
}
public static boolean isProvince(Integer cityId) {
return cityId % 10000 == 0;
}
private static String filter(String cityName) {
List<String> invalidNames = Arrays.asList("市辖区", "县");
if (invalidNames.contains(cityName)) {
return "";
}
return org.apache.commons.lang.StringUtils.trimToEmpty(cityName);
}
/**
* 获取省市id前缀,用于模糊匹配
*
* @param cityId 省市区id,
* @return 省id、或者城市id前缀
*/
public static Integer getLikeValue(Integer cityId) {
if (cityId == null) {
return null;
} else if (cityId % 10000 == 0) {
// 省份
return cityId / 10000;
} else if (cityId % 100 == 0) {
// 市
return cityId / 100;
} else {
// 区/县
return cityId;
}
}
public static String getFullAddress(Integer districtId, String address) {
return CityHelper.buildCityName(CityHelper.getInstance().get(districtId)) + address;
}
}
CityBean
public class CityBean {
private Integer id;
private String name;
private Integer parentId;
private String parentName;
private Integer rootId;
private String rootName;
private String fullName;
private String splitCityName;
private List<CityBean> cityBeanList;
public CityBean() {
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Integer getParentId() {
return this.parentId;
}
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
public String getParentName() {
return this.parentName;
}
public void setParentName(String parentName) {
this.parentName = parentName;
}
public Integer getRootId() {
return this.rootId;
}
public void setRootId(Integer rootId) {
this.rootId = rootId;
}
public String getRootName() {
return this.rootName;
}
public void setRootName(String rootName) {
this.rootName = rootName;
}
public String getFullName() {
return this.fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getSplitCityName() {
return this.splitCityName;
}
public void setSplitCityName(String splitCityName) {
this.splitCityName = splitCityName;
}
public List<CityBean> getCityBeanList() {
return this.cityBeanList;
}
public void setCityBeanList(List<CityBean> cityBeanList) {
this.cityBeanList = cityBeanList;
}
public BaseBean fromPojo(Object o) {
return null;
}
}