Compare commits
10 Commits
f93eec6d08
...
4559636e7d
Author | SHA1 | Date |
---|---|---|
ZhouXY108 | 4559636e7d | |
ZhouXY108 | 9f7eda47fe | |
ZhouXY108 | 8ac446e228 | |
ZhouXY108 | c9db3828a3 | |
ZhouXY108 | 4b9b38eeb9 | |
ZhouXY108 | ef35833dc0 | |
ZhouXY108 | 102ce5185a | |
ZhouXY108 | 672e180f43 | |
ZhouXY108 | 488aaad452 | |
ZhouXY108 | 1e4306005e |
|
@ -18,6 +18,9 @@ package xyz.zhouxy.plusone.commons.exception;
|
||||||
|
|
||||||
import java.time.format.DateTimeParseException;
|
import java.time.format.DateTimeParseException;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
|
import xyz.zhouxy.plusone.commons.base.IWithCode;
|
||||||
import xyz.zhouxy.plusone.commons.exception.business.RequestParamsException;
|
import xyz.zhouxy.plusone.commons.exception.business.RequestParamsException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -26,7 +29,10 @@ import xyz.zhouxy.plusone.commons.exception.business.RequestParamsException;
|
||||||
* <p>
|
* <p>
|
||||||
* 解析失败的不一定是客户传的参数,也可能是其它来源的数据解析失败。
|
* 解析失败的不一定是客户传的参数,也可能是其它来源的数据解析失败。
|
||||||
* 如果表示用户传参造成的解析失败,可使用 {@link RequestParamsException#RequestParamsException(Throwable)},
|
* 如果表示用户传参造成的解析失败,可使用 {@link RequestParamsException#RequestParamsException(Throwable)},
|
||||||
* 将 ParsingFailureException 包装成 {@link RequestParamsException} 再抛出
|
* 将 ParsingFailureException 包装成 {@link RequestParamsException} 再抛出。
|
||||||
|
* <pre>
|
||||||
|
* throw new RequestParamsException(ParsingFailureException.of(ParsingFailureException.Type.NUMBER_PARSING_FAILURE));
|
||||||
|
* </pre>
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||||
|
@ -56,6 +62,22 @@ public final class ParsingFailureException extends RuntimeException {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ParsingFailureException() {
|
||||||
|
this(Type.DEFAULT);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ParsingFailureException(String msg) {
|
||||||
|
this(Type.DEFAULT, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ParsingFailureException(Throwable e) {
|
||||||
|
this(Type.DEFAULT, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ParsingFailureException(String msg, Throwable e) {
|
||||||
|
this(Type.DEFAULT, msg, e);
|
||||||
|
}
|
||||||
|
|
||||||
public static ParsingFailureException of(Type type) {
|
public static ParsingFailureException of(Type type) {
|
||||||
return new ParsingFailureException(type);
|
return new ParsingFailureException(type);
|
||||||
}
|
}
|
||||||
|
@ -80,19 +102,29 @@ public final class ParsingFailureException extends RuntimeException {
|
||||||
return new ParsingFailureException(Type.DATE_TIME_PARSING_FAILURE, msg, e);
|
return new ParsingFailureException(Type.DATE_TIME_PARSING_FAILURE, msg, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static ParsingFailureException of(NumberFormatException e) {
|
||||||
|
return new ParsingFailureException(Type.NUMBER_PARSING_FAILURE, e.getMessage(), e);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ParsingFailureException of(String msg, NumberFormatException e) {
|
||||||
|
return new ParsingFailureException(Type.NUMBER_PARSING_FAILURE, msg, e);
|
||||||
|
}
|
||||||
|
|
||||||
public Type getType() {
|
public Type getType() {
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum Type {
|
public enum Type implements IWithCode<String> {
|
||||||
DEFAULT("4010500", "解析失败"),
|
DEFAULT("00", "解析失败"),
|
||||||
NUMBER_PARSING_FAILURE("4010501", "数字转换失败"),
|
NUMBER_PARSING_FAILURE("10", "数字转换失败"),
|
||||||
DATE_TIME_PARSING_FAILURE("4010502", "时间解析失败"),
|
DATE_TIME_PARSING_FAILURE("20", "时间解析失败"),
|
||||||
JSON_PARSING_FAILURE("4010503", "JSON 解析失败"),
|
JSON_PARSING_FAILURE("30", "JSON 解析失败"),
|
||||||
XML_PARSING_FAILURE("4010504", "XML 解析失败"),
|
XML_PARSING_FAILURE("40", "XML 解析失败"),
|
||||||
;
|
;
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
final String code;
|
final String code;
|
||||||
|
@Nonnull
|
||||||
final String defaultMsg;
|
final String defaultMsg;
|
||||||
|
|
||||||
Type(String code, String defaultMsg) {
|
Type(String code, String defaultMsg) {
|
||||||
|
@ -100,6 +132,8 @@ public final class ParsingFailureException extends RuntimeException {
|
||||||
this.defaultMsg = defaultMsg;
|
this.defaultMsg = defaultMsg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Nonnull
|
||||||
public String getCode() {
|
public String getCode() {
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,10 @@
|
||||||
|
|
||||||
package xyz.zhouxy.plusone.commons.exception.business;
|
package xyz.zhouxy.plusone.commons.exception.business;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
|
import xyz.zhouxy.plusone.commons.base.IWithCode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* InvalidInputException
|
* InvalidInputException
|
||||||
*
|
*
|
||||||
|
@ -53,6 +57,22 @@ public final class InvalidInputException extends RequestParamsException {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public InvalidInputException() {
|
||||||
|
this(Type.DEFAULT);
|
||||||
|
}
|
||||||
|
|
||||||
|
public InvalidInputException(String msg) {
|
||||||
|
this(Type.DEFAULT, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public InvalidInputException(Throwable e) {
|
||||||
|
this(Type.DEFAULT, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
public InvalidInputException(String msg, Throwable e) {
|
||||||
|
this(Type.DEFAULT, msg, e);
|
||||||
|
}
|
||||||
|
|
||||||
public static InvalidInputException of(Type type) {
|
public static InvalidInputException of(Type type) {
|
||||||
return new InvalidInputException(type);
|
return new InvalidInputException(type);
|
||||||
}
|
}
|
||||||
|
@ -81,7 +101,7 @@ public final class InvalidInputException extends RequestParamsException {
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum Type {
|
public enum Type implements IWithCode<String> {
|
||||||
DEFAULT("00", "用户输入内容非法"),
|
DEFAULT("00", "用户输入内容非法"),
|
||||||
CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS("01", "包含非法恶意跳转链接"),
|
CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS("01", "包含非法恶意跳转链接"),
|
||||||
CONTAINS_ILLEGAL_WORDS("02", "包含违禁敏感词"),
|
CONTAINS_ILLEGAL_WORDS("02", "包含违禁敏感词"),
|
||||||
|
@ -97,6 +117,8 @@ public final class InvalidInputException extends RequestParamsException {
|
||||||
this.defaultMsg = defaultMsg;
|
this.defaultMsg = defaultMsg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Nonnull
|
||||||
public String getCode() {
|
public String getCode() {
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
package xyz.zhouxy.plusone.commons.model;
|
package xyz.zhouxy.plusone.commons.model;
|
||||||
|
|
||||||
|
import xyz.zhouxy.plusone.commons.base.IWithIntCode;
|
||||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -23,7 +24,7 @@ import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||||
*
|
*
|
||||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||||
*/
|
*/
|
||||||
public enum Gender {
|
public enum Gender implements IWithIntCode {
|
||||||
UNKNOWN(0, "Unknown", "未知"),
|
UNKNOWN(0, "Unknown", "未知"),
|
||||||
MALE(1, "Male", "男"),
|
MALE(1, "Male", "男"),
|
||||||
FEMALE(2, "Female", "女"),
|
FEMALE(2, "Female", "女"),
|
||||||
|
@ -60,4 +61,9 @@ public enum Gender {
|
||||||
return displayNameZh;
|
return displayNameZh;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getCode() {
|
||||||
|
return getValue();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,9 +18,8 @@ package xyz.zhouxy.plusone.commons.model.dto;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||||
|
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回分页查询的结果
|
* 返回分页查询的结果
|
||||||
|
@ -37,7 +36,7 @@ public class PageResult<T> {
|
||||||
private final List<T> content;
|
private final List<T> content;
|
||||||
|
|
||||||
private PageResult(List<T> content, long total) {
|
private PageResult(List<T> content, long total) {
|
||||||
Preconditions.checkNotNull(content, "Content must not be null.");
|
AssertTools.checkNotNull(content, "Content must not be null.");
|
||||||
this.content = content;
|
this.content = content;
|
||||||
this.total = total;
|
this.total = total;
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,12 +21,15 @@ import java.util.Map;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
|
||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.commons.annotation.Virtual;
|
import xyz.zhouxy.plusone.commons.annotation.Virtual;
|
||||||
|
import xyz.zhouxy.plusone.commons.collection.CollectionTools;
|
||||||
|
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||||
|
import xyz.zhouxy.plusone.commons.util.RegexTools;
|
||||||
import xyz.zhouxy.plusone.commons.util.StringTools;
|
import xyz.zhouxy.plusone.commons.util.StringTools;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -48,15 +51,15 @@ public class PagingAndSortingQueryParams {
|
||||||
private Long pageNum;
|
private Long pageNum;
|
||||||
private List<String> orderBy;
|
private List<String> orderBy;
|
||||||
|
|
||||||
private static final Pattern sortStrPattern = Pattern.compile("^[a-zA-Z]\\w+-(desc|asc|DESC|ASC)$");
|
private static final Pattern SORT_STR_PATTERN = Pattern.compile("^[a-zA-Z]\\w+-(desc|asc|DESC|ASC)$");
|
||||||
|
|
||||||
private final Map<String, String> sortableProperties;
|
private final Map<String, String> sortableProperties;
|
||||||
|
|
||||||
public PagingAndSortingQueryParams(Map<String, String> sortableProperties) {
|
public PagingAndSortingQueryParams(@Nonnull Map<String, String> sortableProperties) {
|
||||||
Preconditions.checkArgument(sortableProperties != null && !sortableProperties.isEmpty(),
|
AssertTools.checkArgument(CollectionTools.isNotEmpty(sortableProperties),
|
||||||
"Sortable properties can not be empty.");
|
"Sortable properties can not be empty.");
|
||||||
sortableProperties.forEach((k, v) ->
|
sortableProperties.forEach((k, v) ->
|
||||||
Preconditions.checkArgument(StringTools.isNotBlank(k) && StringTools.isNotBlank(v),
|
AssertTools.checkArgument(StringTools.isNotBlank(k) && StringTools.isNotBlank(v),
|
||||||
"Property name must not be blank."));
|
"Property name must not be blank."));
|
||||||
this.sortableProperties = ImmutableMap.copyOf(sortableProperties);
|
this.sortableProperties = ImmutableMap.copyOf(sortableProperties);
|
||||||
}
|
}
|
||||||
|
@ -101,12 +104,12 @@ public class PagingAndSortingQueryParams {
|
||||||
}
|
}
|
||||||
|
|
||||||
private SortableProperty generateSortableProperty(String orderByStr) {
|
private SortableProperty generateSortableProperty(String orderByStr) {
|
||||||
Preconditions.checkArgument(PagingAndSortingQueryParams.sortStrPattern.matcher(orderByStr).matches());
|
AssertTools.checkArgument(RegexTools.matches(orderByStr, SORT_STR_PATTERN));
|
||||||
String[] propertyNameAndOrderType = orderByStr.split("-");
|
String[] propertyNameAndOrderType = orderByStr.split("-");
|
||||||
Preconditions.checkArgument(propertyNameAndOrderType.length == 2);
|
AssertTools.checkArgument(propertyNameAndOrderType.length == 2);
|
||||||
|
|
||||||
String propertyName = propertyNameAndOrderType[0];
|
String propertyName = propertyNameAndOrderType[0];
|
||||||
Preconditions.checkArgument(sortableProperties.containsKey(propertyName),
|
AssertTools.checkArgument(sortableProperties.containsKey(propertyName),
|
||||||
"The property name must be in the set of sortable properties.");
|
"The property name must be in the set of sortable properties.");
|
||||||
String columnName = sortableProperties.get(propertyName);
|
String columnName = sortableProperties.get(propertyName);
|
||||||
String orderType = propertyNameAndOrderType[1];
|
String orderType = propertyNameAndOrderType[1];
|
||||||
|
@ -123,7 +126,7 @@ public class PagingAndSortingQueryParams {
|
||||||
SortableProperty(String propertyName, String columnName, String orderType) {
|
SortableProperty(String propertyName, String columnName, String orderType) {
|
||||||
this.propertyName = propertyName;
|
this.propertyName = propertyName;
|
||||||
this.columnName = columnName;
|
this.columnName = columnName;
|
||||||
Preconditions.checkArgument("ASC".equalsIgnoreCase(orderType) || "DESC".equalsIgnoreCase(orderType));
|
AssertTools.checkArgument("ASC".equalsIgnoreCase(orderType) || "DESC".equalsIgnoreCase(orderType));
|
||||||
this.orderType = orderType.toUpperCase();
|
this.orderType = orderType.toUpperCase();
|
||||||
|
|
||||||
this.sqlSnippet = this.propertyName + " " + this.orderType;
|
this.sqlSnippet = this.propertyName + " " + this.orderType;
|
||||||
|
|
|
@ -23,13 +23,15 @@ import java.time.temporal.ChronoField;
|
||||||
import com.google.common.collect.Range;
|
import com.google.common.collect.Range;
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||||
|
import xyz.zhouxy.plusone.commons.base.IWithIntCode;
|
||||||
|
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 季度
|
* 季度
|
||||||
*
|
*
|
||||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||||
*/
|
*/
|
||||||
public enum Quarter {
|
public enum Quarter implements IWithIntCode {
|
||||||
/** 第一季度 */
|
/** 第一季度 */
|
||||||
Q1(1),
|
Q1(1),
|
||||||
/** 第二季度 */
|
/** 第二季度 */
|
||||||
|
@ -107,10 +109,7 @@ public enum Quarter {
|
||||||
*/
|
*/
|
||||||
@StaticFactoryMethod(Quarter.class)
|
@StaticFactoryMethod(Quarter.class)
|
||||||
public static Quarter of(int value) {
|
public static Quarter of(int value) {
|
||||||
if (value < 1 || value > 4) {
|
return ENUMS[checkValidIntValue(value) - 1];
|
||||||
throw new IllegalArgumentException("Invalid value for Quarter: " + value);
|
|
||||||
}
|
|
||||||
return ENUMS[value - 1];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// StaticFactoryMethods end
|
// StaticFactoryMethods end
|
||||||
|
@ -134,6 +133,11 @@ public enum Quarter {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getCode() {
|
||||||
|
return getValue();
|
||||||
|
}
|
||||||
|
|
||||||
public Month firstMonth() {
|
public Month firstMonth() {
|
||||||
return Month.of(firstMonthValue());
|
return Month.of(firstMonthValue());
|
||||||
}
|
}
|
||||||
|
@ -166,6 +170,11 @@ public enum Quarter {
|
||||||
|
|
||||||
// Getters end
|
// Getters end
|
||||||
|
|
||||||
|
public static int checkValidIntValue(int value) {
|
||||||
|
AssertTools.checkArgument(value >= 1 && value <= 4, () -> "Invalid value for Quarter: " + value);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
// Internal
|
// Internal
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -59,7 +59,7 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
|
||||||
this.lastDate = quarter.lastMonthDay().atYear(year);
|
this.lastDate = quarter.lastMonthDay().atYear(year);
|
||||||
}
|
}
|
||||||
|
|
||||||
// #region - StaticFactoryMethod
|
// #region - StaticFactory
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据指定年份与季度,创建 {@link YearQuarter} 实例
|
* 根据指定年份与季度,创建 {@link YearQuarter} 实例
|
||||||
|
@ -186,7 +186,7 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
|
||||||
// #region - computes
|
// #region - computes
|
||||||
|
|
||||||
public YearQuarter plusQuarters(long quartersToAdd) { // TODO 单元测试
|
public YearQuarter plusQuarters(long quartersToAdd) { // TODO 单元测试
|
||||||
if (quartersToAdd == 0) {
|
if (quartersToAdd == 0L) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
long quarterCount = this.year * 4L + (this.quarter.getValue() - 1);
|
long quarterCount = this.year * 4L + (this.quarter.getValue() - 1);
|
||||||
|
@ -200,8 +200,16 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
|
||||||
return plusQuarters(-quartersToAdd);
|
return plusQuarters(-quartersToAdd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public YearQuarter nextQuarter() { // TODO 单元测试
|
||||||
|
return plusQuarters(1L);
|
||||||
|
}
|
||||||
|
|
||||||
|
public YearQuarter lastQuarter() { // TODO 单元测试
|
||||||
|
return minusQuarters(1L);
|
||||||
|
}
|
||||||
|
|
||||||
public YearQuarter plusYears(long yearsToAdd) { // TODO 单元测试
|
public YearQuarter plusYears(long yearsToAdd) { // TODO 单元测试
|
||||||
if (yearsToAdd == 0) {
|
if (yearsToAdd == 0L) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
int newYear = YEAR.checkValidIntValue(this.year + yearsToAdd); // safe overflow
|
int newYear = YEAR.checkValidIntValue(this.year + yearsToAdd); // safe overflow
|
||||||
|
@ -212,6 +220,14 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
|
||||||
return plusYears(-yearsToAdd);
|
return plusYears(-yearsToAdd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public YearQuarter nextYear() {
|
||||||
|
return plusYears(1L);
|
||||||
|
}
|
||||||
|
|
||||||
|
public YearQuarter lastYear() {
|
||||||
|
return minusYears(1L);
|
||||||
|
}
|
||||||
|
|
||||||
// #endregion
|
// #endregion
|
||||||
|
|
||||||
// #region - hashCode & equals
|
// #region - hashCode & equals
|
||||||
|
@ -235,7 +251,7 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
|
||||||
|
|
||||||
// #endregion
|
// #endregion
|
||||||
|
|
||||||
// #region - compareTo
|
// #region - compare
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compareTo(YearQuarter other) {
|
public int compareTo(YearQuarter other) {
|
||||||
|
@ -254,6 +270,14 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
|
||||||
return this.compareTo(other) > 0;
|
return this.compareTo(other) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static YearQuarter min(YearQuarter yearQuarter1, YearQuarter yearQuarter2) { // TODO 单元测试
|
||||||
|
return yearQuarter1.compareTo(yearQuarter2) <= 0 ? yearQuarter1 : yearQuarter2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static YearQuarter max(YearQuarter yearQuarter1, YearQuarter yearQuarter2) { // TODO 单元测试
|
||||||
|
return yearQuarter1.compareTo(yearQuarter2) >= 0 ? yearQuarter1 : yearQuarter2;
|
||||||
|
}
|
||||||
|
|
||||||
// #endregion
|
// #endregion
|
||||||
|
|
||||||
// #region - toString
|
// #region - toString
|
||||||
|
|
|
@ -20,8 +20,6 @@ import java.util.function.Supplier;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 枚举工具类
|
* 枚举工具类
|
||||||
*
|
*
|
||||||
|
@ -36,18 +34,18 @@ public final class EnumTools {
|
||||||
/**
|
/**
|
||||||
* 通过 ordinal 获取枚举实例
|
* 通过 ordinal 获取枚举实例
|
||||||
*
|
*
|
||||||
* @param <E> 枚举的类型
|
* @param <E> 枚举的类型
|
||||||
* @param clazz 枚举的类型信息
|
* @param enumType 枚举的类型信息
|
||||||
* @param ordinal 序号
|
* @param ordinal 序号
|
||||||
* @return 枚举对象
|
* @return 枚举对象
|
||||||
* @deprecated 不推荐使用枚举的 ordinal。
|
* @deprecated 不推荐使用枚举的 ordinal。
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public static <E extends Enum<?>> E valueOf(Class<E> clazz, int ordinal) { // NOSONAR 该方法弃用,但不删掉
|
public static <E extends Enum<?>> E valueOf(Class<E> enumType, int ordinal) { // NOSONAR 该方法弃用,但不删掉
|
||||||
Preconditions.checkNotNull(clazz, "Clazz must not be null.");
|
AssertTools.checkNotNull(enumType, "Enum type must not be null.");
|
||||||
E[] values = clazz.getEnumConstants();
|
E[] values = enumType.getEnumConstants();
|
||||||
AssertTools.checkCondition((ordinal >= 0 && ordinal < values.length),
|
AssertTools.checkCondition((ordinal >= 0 && ordinal < values.length),
|
||||||
() -> new EnumConstantNotPresentException(clazz, Integer.toString(ordinal)));
|
() -> new EnumConstantNotPresentException(enumType, Integer.toString(ordinal)));
|
||||||
return values[ordinal];
|
return values[ordinal];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,25 +53,23 @@ public final class EnumTools {
|
||||||
* 通过 ordinal 获取枚举实例
|
* 通过 ordinal 获取枚举实例
|
||||||
*
|
*
|
||||||
* @param <E> 枚举的类型
|
* @param <E> 枚举的类型
|
||||||
* @param clazz 枚举的类型信息
|
* @param enumType 枚举的类型信息
|
||||||
* @param ordinal 序号
|
* @param ordinal 序号
|
||||||
* @param defaultValue 默认值
|
* @param defaultValue 默认值
|
||||||
* @return 枚举对象
|
* @return 枚举对象
|
||||||
* @deprecated 不推荐使用枚举的 ordinal。
|
* @deprecated 不推荐使用枚举的 ordinal。
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public static <E extends Enum<?>> E valueOf(Class<E> clazz, @Nullable Integer ordinal, E defaultValue) { // NOSONAR 该方法弃用,但不删掉
|
public static <E extends Enum<?>> E valueOf(Class<E> enumType, // NOSONAR 该方法弃用,但不删掉
|
||||||
if (null == ordinal) {
|
@Nullable Integer ordinal, E defaultValue) {
|
||||||
return defaultValue;
|
return null == ordinal ? defaultValue : valueOf(enumType, ordinal);
|
||||||
}
|
|
||||||
return valueOf(clazz, ordinal);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过 ordinal 获取枚举实例
|
* 通过 ordinal 获取枚举实例
|
||||||
*
|
*
|
||||||
* @param <E> 枚举的类型
|
* @param <E> 枚举的类型
|
||||||
* @param clazz 枚举的类型信息
|
* @param enumType 枚举的类型信息
|
||||||
* @param ordinal 序号
|
* @param ordinal 序号
|
||||||
* @param defaultValue 默认值
|
* @param defaultValue 默认值
|
||||||
* @return 枚举对象
|
* @return 枚举对象
|
||||||
|
@ -81,29 +77,26 @@ public final class EnumTools {
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public static <E extends Enum<?>> E getValueOrDefault( // NOSONAR 该方法弃用,但不删掉
|
public static <E extends Enum<?>> E getValueOrDefault( // NOSONAR 该方法弃用,但不删掉
|
||||||
Class<E> clazz,
|
Class<E> enumType,
|
||||||
@Nullable Integer ordinal,
|
@Nullable Integer ordinal,
|
||||||
Supplier<E> defaultValue) {
|
Supplier<E> defaultValue) {
|
||||||
if (null == ordinal) {
|
return null == ordinal ? defaultValue.get() : valueOf(enumType, ordinal);
|
||||||
return defaultValue.get();
|
|
||||||
}
|
|
||||||
return valueOf(clazz, ordinal);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过 ordinal 获取枚举实例
|
* 通过 ordinal 获取枚举实例
|
||||||
*
|
*
|
||||||
* @param <E> 枚举的类型
|
* @param <E> 枚举的类型
|
||||||
* @param clazz 枚举的类型信息
|
* @param enumType 枚举的类型信息
|
||||||
* @param ordinal 序号
|
* @param ordinal 序号
|
||||||
* @return 枚举对象
|
* @return 枚举对象
|
||||||
* @deprecated 不推荐使用枚举的 ordinal。
|
* @deprecated 不推荐使用枚举的 ordinal。
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public static <E extends Enum<?>> E getValueOrDefault(Class<E> clazz, @Nullable Integer ordinal) { // NOSONAR 该方法弃用,但不删掉
|
public static <E extends Enum<?>> E getValueOrDefault(Class<E> enumType, @Nullable Integer ordinal) { // NOSONAR 该方法弃用,但不删掉
|
||||||
return getValueOrDefault(clazz, ordinal, () -> {
|
return getValueOrDefault(enumType, ordinal, () -> {
|
||||||
Preconditions.checkNotNull(clazz, "Clazz must not be null.");
|
AssertTools.checkNotNull(enumType, "Enum type must not be null.");
|
||||||
E[] values = clazz.getEnumConstants();
|
E[] values = enumType.getEnumConstants();
|
||||||
return values[0];
|
return values[0];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -111,69 +104,65 @@ public final class EnumTools {
|
||||||
/**
|
/**
|
||||||
* 通过 ordinal 获取枚举实例
|
* 通过 ordinal 获取枚举实例
|
||||||
*
|
*
|
||||||
* @param <E> 枚举的类型
|
* @param <E> 枚举的类型
|
||||||
* @param clazz 枚举的类型信息
|
* @param enumType 枚举的类型信息
|
||||||
* @param ordinal 序号
|
* @param ordinal 序号
|
||||||
* @return 枚举对象
|
* @return 枚举对象
|
||||||
* @deprecated 不推荐使用枚举的 ordinal。
|
* @deprecated 不推荐使用枚举的 ordinal。
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public static <E extends Enum<?>> E getValueNullable(Class<E> clazz, @Nullable Integer ordinal) { // NOSONAR 该方法弃用,但不删掉
|
public static <E extends Enum<?>> E getValueNullable(Class<E> enumType, @Nullable Integer ordinal) { // NOSONAR 该方法弃用,但不删掉
|
||||||
return valueOf(clazz, ordinal, null);
|
return valueOf(enumType, ordinal, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <E extends Enum<?>> Integer checkOrdinal(Class<E> clazz, Integer ordinal) {
|
public static <E extends Enum<?>> Integer checkOrdinal(Class<E> enumType, Integer ordinal) {
|
||||||
Preconditions.checkNotNull(clazz, "Clazz must not be null.");
|
AssertTools.checkNotNull(enumType, "Enum type must not be null.");
|
||||||
Preconditions.checkNotNull(ordinal, "Ordinal must not be null.");
|
AssertTools.checkNotNull(ordinal, "Ordinal must not be null.");
|
||||||
E[] values = clazz.getEnumConstants();
|
E[] values = enumType.getEnumConstants();
|
||||||
if (ordinal >= 0 && ordinal < values.length) {
|
AssertTools.checkCondition(ordinal >= 0 && ordinal < values.length,
|
||||||
return ordinal;
|
() -> new EnumConstantNotPresentException(enumType, Integer.toString(ordinal)));
|
||||||
}
|
return ordinal;
|
||||||
throw new EnumConstantNotPresentException(clazz, Integer.toString(ordinal));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 校验枚举的 ordinal。
|
* 校验枚举的 ordinal。
|
||||||
*
|
*
|
||||||
* @param <E> 枚举类型
|
* @param <E> 枚举类型
|
||||||
* @param clazz 枚举类型
|
* @param enumType 枚举类型
|
||||||
* @param ordinal The ordinal
|
* @param ordinal The ordinal
|
||||||
* @return The ordinal
|
* @return The ordinal
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public static <E extends Enum<?>> Integer checkOrdinalNullable(Class<E> clazz, @Nullable Integer ordinal) {
|
public static <E extends Enum<?>> Integer checkOrdinalNullable(Class<E> enumType, @Nullable Integer ordinal) {
|
||||||
return checkOrdinalOrDefault(clazz, ordinal, null);
|
return checkOrdinalOrDefault(enumType, ordinal, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 校验枚举的 ordinal,如果 ordinal 为 {@code null},则返回 {@code 0}。
|
* 校验枚举的 ordinal,如果 ordinal 为 {@code null},则返回 {@code 0}。
|
||||||
*
|
*
|
||||||
* @param <E> 枚举类型
|
* @param <E> 枚举类型
|
||||||
* @param clazz 枚举类型
|
* @param enumType 枚举类型
|
||||||
* @param ordinal The ordinal
|
* @param ordinal The ordinal
|
||||||
* @return The ordinal
|
* @return The ordinal
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public static <E extends Enum<?>> Integer checkOrdinalOrDefault(Class<E> clazz, @Nullable Integer ordinal) {
|
public static <E extends Enum<?>> Integer checkOrdinalOrDefault(Class<E> enumType, @Nullable Integer ordinal) {
|
||||||
return checkOrdinalOrDefault(clazz, ordinal, 0);
|
return checkOrdinalOrDefault(enumType, ordinal, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 校验枚举的 ordinal,如果 ordinal 为 {@code null},则返回 {@code defaultValue}。
|
* 校验枚举的 ordinal,如果 ordinal 为 {@code null},则返回 {@code defaultValue}。
|
||||||
*
|
*
|
||||||
* @param <E> 枚举类型
|
* @param <E> 枚举类型
|
||||||
* @param clazz 枚举类型
|
* @param enumType 枚举类型
|
||||||
* @param ordinal The ordinal
|
* @param ordinal The ordinal
|
||||||
* @return The ordinal
|
* @return The ordinal
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public static <E extends Enum<?>> Integer checkOrdinalOrDefault(
|
public static <E extends Enum<?>> Integer checkOrdinalOrDefault(
|
||||||
Class<E> clazz,
|
Class<E> enumType,
|
||||||
@Nullable Integer ordinal,
|
@Nullable Integer ordinal,
|
||||||
@Nullable Integer defaultValue) {
|
@Nullable Integer defaultValue) {
|
||||||
if (ordinal != null) {
|
return ordinal != null ? checkOrdinal(enumType, ordinal) : defaultValue;
|
||||||
return checkOrdinal(clazz, ordinal);
|
|
||||||
}
|
|
||||||
return defaultValue;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,8 +24,6 @@ import java.util.Objects;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -43,7 +41,7 @@ public abstract class Enumeration<T extends Enumeration<T>> // NOSONAR 暂不移
|
||||||
protected final String name;
|
protected final String name;
|
||||||
|
|
||||||
protected Enumeration(final int id, final String name) {
|
protected Enumeration(final int id, final String name) {
|
||||||
Preconditions.checkArgument(StringTools.isNotBlank(name), "Name of enumeration must has text.");
|
AssertTools.checkArgument(StringTools.isNotBlank(name), "Name of enumeration must has text.");
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
@ -98,7 +96,7 @@ public abstract class Enumeration<T extends Enumeration<T>> // NOSONAR 暂不移
|
||||||
}
|
}
|
||||||
|
|
||||||
public T get(int id) {
|
public T get(int id) {
|
||||||
Preconditions.checkArgument(this.valueMap.containsKey(id), "[%s] 对应的值不存在", id);
|
AssertTools.checkArgument(this.valueMap.containsKey(id), "[%s] 对应的值不存在", id);
|
||||||
return this.valueMap.get(id);
|
return this.valueMap.get(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package xyz.zhouxy.plusone.commons.queryparams.test;
|
package xyz.zhouxy.plusone.commons.model.dto.test;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
|
@ -59,8 +59,8 @@ public class YearQuarterTests {
|
||||||
void of_ValidYearQuarter_GetsCorrectStartAndEndDate() {
|
void of_ValidYearQuarter_GetsCorrectStartAndEndDate() {
|
||||||
|
|
||||||
for (int year = 1990; year <= 2024; year++) {
|
for (int year = 1990; year <= 2024; year++) {
|
||||||
for (int qrtr = 1; qrtr <= 4; qrtr++) {
|
for (int quarterValue = 1; quarterValue <= 4; quarterValue++) {
|
||||||
Quarter quarter = Quarter.of(qrtr);
|
Quarter quarter = Quarter.of(quarterValue);
|
||||||
YearQuarter yearQuarter = YearQuarter.of(year, quarter);
|
YearQuarter yearQuarter = YearQuarter.of(year, quarter);
|
||||||
|
|
||||||
LocalDate expectedStartDate = quarter.firstMonthDay().atYear(year);
|
LocalDate expectedStartDate = quarter.firstMonthDay().atYear(year);
|
||||||
|
|
Loading…
Reference in New Issue