|
@@ -5,8 +5,6 @@ import java.util.Objects;
|
|
|
import java.util.stream.Collectors;
|
|
|
import java.util.stream.Stream;
|
|
|
|
|
|
-import com.chelvc.framework.common.util.AssertUtils;
|
|
|
-import com.chelvc.framework.common.util.ObjectUtils;
|
|
|
import com.chelvc.framework.common.util.StringUtils;
|
|
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
|
|
import lombok.AllArgsConstructor;
|
|
@@ -28,26 +26,6 @@ import lombok.NonNull;
|
|
|
@NoArgsConstructor
|
|
|
@AllArgsConstructor
|
|
|
public final class Region implements Serializable {
|
|
|
- /**
|
|
|
- * 地区类型枚举
|
|
|
- */
|
|
|
- public enum Type {
|
|
|
- /**
|
|
|
- * 省
|
|
|
- */
|
|
|
- PROVINCE,
|
|
|
-
|
|
|
- /**
|
|
|
- * 市
|
|
|
- */
|
|
|
- CITY,
|
|
|
-
|
|
|
- /**
|
|
|
- * 区
|
|
|
- */
|
|
|
- DISTRICT
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 地区编码最小值
|
|
|
*/
|
|
@@ -58,11 +36,6 @@ public final class Region implements Serializable {
|
|
|
*/
|
|
|
private static final int MAX_VALUE = 999999;
|
|
|
|
|
|
- /**
|
|
|
- * 地区类型
|
|
|
- */
|
|
|
- private Type type;
|
|
|
-
|
|
|
/**
|
|
|
* 地区编码
|
|
|
*/
|
|
@@ -79,162 +52,121 @@ public final class Region implements Serializable {
|
|
|
* @param code 地区编码
|
|
|
* @return true/false
|
|
|
*/
|
|
|
- public static boolean isRegion(Number code) {
|
|
|
- Integer value = ObjectUtils.ifNull(code, Number::intValue);
|
|
|
- return code != null && value >= MIN_VALUE && value <= MAX_VALUE;
|
|
|
+ public static boolean isRegion(int code) {
|
|
|
+ return code >= MIN_VALUE && code <= MAX_VALUE;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取可用的地区编码
|
|
|
+ * 判断是否是市级编码
|
|
|
*
|
|
|
* @param code 地区编码
|
|
|
- * @return 地区编码
|
|
|
- */
|
|
|
- public static Integer getActiveCode(Number code) {
|
|
|
- Integer value = ObjectUtils.ifNull(code, Number::intValue);
|
|
|
- if (value == null || value < MIN_VALUE) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- return value <= MAX_VALUE ? value : (int) (value / Math.pow(10, (int) Math.log10(value) - 5));
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 判断是否是同一地区
|
|
|
- *
|
|
|
- * @param code1 地区编码
|
|
|
- * @param code2 地区编码
|
|
|
* @return true/false
|
|
|
*/
|
|
|
- public static boolean isSame(Number code1, Number code2) {
|
|
|
- Integer value1 = ObjectUtils.ifNull(code1, Number::intValue);
|
|
|
- Integer value2 = ObjectUtils.ifNull(code2, Number::intValue);
|
|
|
- return isRegion(value1) && isRegion(value2)
|
|
|
- && (Objects.equals(value1, value2) || Objects.equals(value1, value2 / 100 * 100)
|
|
|
- || Objects.equals(value1, value2 / 10000 * 10000)
|
|
|
- || Objects.equals(value1 / 100 * 100, value2) || Objects.equals(value1 / 10000 * 10000, value2));
|
|
|
+ public static boolean isCity(int code) {
|
|
|
+ return isRegion(code) && (code % 10000 > 0) && (code % 100 == 0);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 判断地区编码是否是市级编码
|
|
|
+ * 判断是否是省级编码
|
|
|
*
|
|
|
* @param code 地区编码
|
|
|
* @return true/false
|
|
|
*/
|
|
|
- public static boolean isCity(Number code) {
|
|
|
- Integer value = ObjectUtils.ifNull(code, Number::intValue);
|
|
|
- return isRegion(value) && (value % 10000 > 0) && (value % 100 == 0);
|
|
|
+ public static boolean isProvince(int code) {
|
|
|
+ return isRegion(code) && (code % 10000 == 0);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 判断地区编码是否是省级编码
|
|
|
+ * 判断是否是区级编码
|
|
|
*
|
|
|
* @param code 地区编码
|
|
|
* @return true/false
|
|
|
*/
|
|
|
- public static boolean isProvince(Number code) {
|
|
|
- return isRegion(code) && (code.intValue() % 10000 == 0);
|
|
|
+ public static boolean isDistrict(int code) {
|
|
|
+ return isRegion(code) && (code % 100 > 0);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 判断地区编码是否是区级编码
|
|
|
+ * 判断地区是否是直辖市
|
|
|
*
|
|
|
* @param code 地区编码
|
|
|
* @return true/false
|
|
|
*/
|
|
|
- public static boolean isDistrict(Number code) {
|
|
|
- return isRegion(code) && (code.intValue() % 100 > 0);
|
|
|
+ public static boolean isDirectly(int code) {
|
|
|
+ code = code / 10000;
|
|
|
+ return code == 11 || code == 12 || code == 31 || code == 50;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 构建地区编码
|
|
|
+ * 判断地区是否属于敏感地区
|
|
|
*
|
|
|
- * @param type 地区类型
|
|
|
- * @param code 地区编码
|
|
|
- * @param boundary 地区边界编码
|
|
|
- * @return 地区实例
|
|
|
+ * @param code 地区编码
|
|
|
+ * @return true/false
|
|
|
*/
|
|
|
- public static Region of(Type type, int code, int boundary) {
|
|
|
- AssertUtils.check(code > 0, () -> "Region's code must be greater than 0");
|
|
|
- AssertUtils.check(boundary >= 0, () -> "Region's boundary must be greater than or equal 0");
|
|
|
- Region region = new Region();
|
|
|
- region.type = type;
|
|
|
- region.code = code;
|
|
|
- region.boundary = boundary;
|
|
|
- return region;
|
|
|
+ public static boolean isSensitive(int code) {
|
|
|
+ code = code / 10000;
|
|
|
+ return code == 11 || code == 65 || code == 54;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 构建地区编码
|
|
|
+ * 判断是否是同一地区
|
|
|
*
|
|
|
- * @param code 地区编码
|
|
|
- * @return 地区实例
|
|
|
+ * @param code1 地区编码
|
|
|
+ * @param code2 地区编码
|
|
|
+ * @return true/false
|
|
|
*/
|
|
|
- public static Region of(Number code) {
|
|
|
- Integer value = ObjectUtils.ifNull(code, Number::intValue);
|
|
|
- if (isProvince(value)) {
|
|
|
- return of(Type.PROVINCE, value, value + 9999);
|
|
|
- } else if (isCity(value)) {
|
|
|
- return of(Type.CITY, value, value + 99);
|
|
|
- } else if (isDistrict(value)) {
|
|
|
- return of(Type.DISTRICT, value, 0);
|
|
|
- }
|
|
|
- return null;
|
|
|
+ public static boolean isSame(int code1, int code2) {
|
|
|
+ return isRegion(code1) && isRegion(code2) && (Objects.equals(code1, code2)
|
|
|
+ || Objects.equals(code1, code2 / 100 * 100) || Objects.equals(code1, code2 / 10000 * 10000)
|
|
|
+ || Objects.equals(code1 / 100 * 100, code2) || Objects.equals(code1 / 10000 * 10000, code2));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 将字符串转换成地区编码
|
|
|
+ * 判断是否是同一地区
|
|
|
*
|
|
|
- * @param text 地区编码字符串格式
|
|
|
- * @return 地区实例
|
|
|
+ * @param region1 地区对象
|
|
|
+ * @param region2 地区对象
|
|
|
+ * @return true/false
|
|
|
*/
|
|
|
- public static Region parse(String text) {
|
|
|
- return text == null || (text = text.trim()).isEmpty() ? null : of(Integer.parseInt(text));
|
|
|
+ public static boolean isSame(Region region1, Region region2) {
|
|
|
+ return region1 != null && region2 != null && isSame(region1.getCode(), region2.getCode());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 将地区编码转换成市级编码
|
|
|
*
|
|
|
* @param code 地区编码
|
|
|
- * @return 地区实例
|
|
|
+ * @return 地区编码
|
|
|
*/
|
|
|
- public static Region code2city(Number code) {
|
|
|
- Integer value = ObjectUtils.ifNull(code, Number::intValue);
|
|
|
- return !isRegion(value) || isProvince(value) ? null : of(Type.CITY, value = value / 100 * 100, value + 99);
|
|
|
+ public static int code2city(int code) {
|
|
|
+ return code / 100 * 100;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 将地区编码转换成省级编码
|
|
|
*
|
|
|
* @param code 地区编码
|
|
|
- * @return 地区实例
|
|
|
+ * @return 地区编码
|
|
|
*/
|
|
|
- public static Region code2province(Number code) {
|
|
|
- Integer value = ObjectUtils.ifNull(code, Number::intValue);
|
|
|
- return !isRegion(value) ? null : of(Type.PROVINCE, value = value / 10000 * 10000, value + 9999);
|
|
|
+ public static int code2province(int code) {
|
|
|
+ return code / 10000 * 10000;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取市级地区
|
|
|
+ * 构建地区编码
|
|
|
*
|
|
|
* @param code 地区编码
|
|
|
* @return 地区实例
|
|
|
*/
|
|
|
- public static Region urbanize(Number code) {
|
|
|
- return isRegion(code) ? of(code.intValue() / 100 * 100) : null;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 判断地区是否属于敏感地区
|
|
|
- *
|
|
|
- * @param code 地区编码
|
|
|
- * @return true/false
|
|
|
- */
|
|
|
- public static boolean isSensitive(Number code) {
|
|
|
- if (code == null) {
|
|
|
- return false;
|
|
|
+ public static Region of(int code) {
|
|
|
+ if (isProvince(code)) {
|
|
|
+ return new Region(code, code + 9999);
|
|
|
+ } else if (isCity(code)) {
|
|
|
+ return new Region(code, code + 99);
|
|
|
+ } else if (isDistrict(code)) {
|
|
|
+ return new Region(code, 0);
|
|
|
}
|
|
|
- int province = (code.intValue() / 10000);
|
|
|
- return province == 11 || province == 65 || province == 54;
|
|
|
+ throw new IllegalArgumentException("Invalid region: " + code);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -284,71 +216,86 @@ public final class Region implements Serializable {
|
|
|
*/
|
|
|
@JsonIgnore
|
|
|
public Region getParent() {
|
|
|
- return this.type == Type.DISTRICT ? code2city(this.code) :
|
|
|
- this.type == Type.CITY ? code2province(this.code) : null;
|
|
|
+ if (this.isCity()) {
|
|
|
+ int province = code2province(this.code);
|
|
|
+ return new Region(province, province + 9999);
|
|
|
+ } else if (this.isDistrict()) {
|
|
|
+ int city = code2city(this.code);
|
|
|
+ return new Region(city, city + 99);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取当前地区所在省
|
|
|
+ * 判断是否是市级地区
|
|
|
*
|
|
|
- * @return 省
|
|
|
+ * @return true/false
|
|
|
*/
|
|
|
@JsonIgnore
|
|
|
- public Region getProvince() {
|
|
|
- return this.type == Type.PROVINCE ? this : code2province(this.code);
|
|
|
+ public boolean isCity() {
|
|
|
+ return isCity(this.code);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取当前地区所在市
|
|
|
+ * 判断是否是省级地区
|
|
|
*
|
|
|
- * @return 市
|
|
|
+ * @return true/false
|
|
|
*/
|
|
|
@JsonIgnore
|
|
|
- public Region getCity() {
|
|
|
- return this.type == Type.CITY ? this : this.type == Type.PROVINCE ? null : code2city(this.code);
|
|
|
+ public boolean isProvince() {
|
|
|
+ return isProvince(this.code);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取当前地区所在区
|
|
|
+ * 判断是否是区级地区
|
|
|
*
|
|
|
- * @return 区
|
|
|
+ * @return true/false
|
|
|
*/
|
|
|
@JsonIgnore
|
|
|
- public Region getDistrict() {
|
|
|
- return this.type == Type.DISTRICT ? this : null;
|
|
|
+ public boolean isDistrict() {
|
|
|
+ return isDistrict(this.code);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 判断是否是同一地区
|
|
|
+ * 判断是否是直辖市
|
|
|
*
|
|
|
- * @param code 地区编码
|
|
|
* @return true/false
|
|
|
*/
|
|
|
@JsonIgnore
|
|
|
- public boolean isSame(Number code) {
|
|
|
- return isSame(this.code, code);
|
|
|
+ public boolean isDirectly() {
|
|
|
+ return isDirectly(this.code);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断地区是否属于敏感地区
|
|
|
+ *
|
|
|
+ * @return true/false
|
|
|
+ */
|
|
|
+ @JsonIgnore
|
|
|
+ public boolean isSensitive() {
|
|
|
+ return isSensitive(this.code);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 判断是否是同一地区
|
|
|
*
|
|
|
- * @param region 地区实例
|
|
|
+ * @param code 地区编码
|
|
|
* @return true/false
|
|
|
*/
|
|
|
@JsonIgnore
|
|
|
- public boolean isSame(Region region) {
|
|
|
- return region != null && isSame(this.code, region.getCode());
|
|
|
+ public boolean isSame(int code) {
|
|
|
+ return isSame(this.code, code);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 判断是否是直辖市
|
|
|
+ * 判断是否是同一地区
|
|
|
*
|
|
|
+ * @param region 地区实例
|
|
|
* @return true/false
|
|
|
*/
|
|
|
@JsonIgnore
|
|
|
- public boolean isDirectly() {
|
|
|
- int code = this.code / 10000;
|
|
|
- return code == 11 || code == 12 || code == 31 || code == 50;
|
|
|
+ public boolean isSame(Region region) {
|
|
|
+ return isSame(this, region);
|
|
|
}
|
|
|
|
|
|
@Override
|