优化功能
parent
2297536307
commit
48bd4f1b31
|
@ -6,7 +6,6 @@ import javax.annotation.Nullable;
|
|||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
public class BigDecimals {
|
||||
|
@ -17,21 +16,26 @@ public class BigDecimals {
|
|||
return (a == b) || (a != null && a.compareTo(b) == 0);
|
||||
}
|
||||
|
||||
@Beta
|
||||
public static boolean greaterThan(BigDecimal a, BigDecimal b) {
|
||||
public static boolean gt(BigDecimal a, BigDecimal b) {
|
||||
Preconditions.checkNotNull(a, "Parameter could not be null.");
|
||||
Preconditions.checkNotNull(b, "Parameter could not be null.");
|
||||
return (a != b) && (a.compareTo(b) > 0);
|
||||
}
|
||||
|
||||
@Beta
|
||||
public static boolean lessThan(BigDecimal a, BigDecimal b) {
|
||||
public static boolean ge(BigDecimal a, BigDecimal b) {
|
||||
return gt(a, b) || equals(a, b);
|
||||
}
|
||||
|
||||
public static boolean lt(BigDecimal a, BigDecimal b) {
|
||||
Preconditions.checkNotNull(a, "Parameter could not be null.");
|
||||
Preconditions.checkNotNull(b, "Parameter could not be null.");
|
||||
return (a != b) && (a.compareTo(b) < 0);
|
||||
}
|
||||
|
||||
@Beta
|
||||
public static boolean le(BigDecimal a, BigDecimal b) {
|
||||
return lt(a, b) || equals(a, b);
|
||||
}
|
||||
|
||||
public static BigDecimal of(final String val) {
|
||||
return (StringUtils.isBlank(val)) ? ZERO : new BigDecimal(val);
|
||||
}
|
||||
|
|
|
@ -85,9 +85,8 @@ public abstract class Enumeration<T extends Enumeration<T>> implements Comparabl
|
|||
this.valueMap = valueMap;
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
@StaticFactoryMethod(ValueSet.class)
|
||||
public static <T extends Enumeration<T>> ValueSet<T> of(T... values) {
|
||||
public static <T extends Enumeration<T>> ValueSet<T> of(T[] values) {
|
||||
Map<Integer, T> temp = Arrays.stream(values)
|
||||
.collect(Collectors.toMap(Enumeration::getId, Function.identity()));
|
||||
return new ValueSet<>(Collections.unmodifiableMap(temp));
|
||||
|
|
|
@ -16,6 +16,8 @@
|
|||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* Numbers
|
||||
*
|
||||
|
@ -23,10 +25,6 @@ package xyz.zhouxy.plusone.commons.util;
|
|||
*/
|
||||
public class Numbers {
|
||||
|
||||
private Numbers() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
|
||||
// sum
|
||||
|
||||
public static int sum(final short... numbers) {
|
||||
|
@ -69,6 +67,14 @@ public class Numbers {
|
|||
return result;
|
||||
}
|
||||
|
||||
public static BigDecimal sum(final BigDecimal... numbers) {
|
||||
BigDecimal result = BigDecimals.of("0.00");
|
||||
for (BigDecimal number : numbers) {
|
||||
result = result.add(number);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// between
|
||||
|
||||
public static boolean between(short value, short min, short max) {
|
||||
|
@ -90,4 +96,12 @@ public class Numbers {
|
|||
public static boolean between(double value, double min, double max) {
|
||||
return value >= min && value < max;
|
||||
}
|
||||
|
||||
public static boolean between(BigDecimal value, BigDecimal min, BigDecimal max) {
|
||||
return BigDecimals.ge(value, min) && BigDecimals.lt(value, max);
|
||||
}
|
||||
|
||||
private Numbers() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,7 +38,7 @@ final class EntityStatus extends Enumeration<EntityStatus> {
|
|||
public static final EntityStatus AVAILABLE = new EntityStatus(0, "正常");
|
||||
public static final EntityStatus DISABLED = new EntityStatus(1, "禁用");
|
||||
|
||||
private static final ValueSet<EntityStatus> VALUE_SET = ValueSet.of(AVAILABLE, DISABLED);
|
||||
private static final ValueSet<EntityStatus> VALUE_SET = ValueSet.of(new EntityStatus[] { AVAILABLE, DISABLED });
|
||||
|
||||
public static EntityStatus of(int value) {
|
||||
return VALUE_SET.get(value);
|
||||
|
@ -57,7 +57,7 @@ final class Result extends Enumeration<Result> {
|
|||
public static final Result SUCCESSFUL = new Result(1, "成功");
|
||||
public static final Result FAILURE = new Result(0, "失败");
|
||||
|
||||
private static final ValueSet<Result> VALUE_SET = ValueSet.of(SUCCESSFUL, FAILURE);
|
||||
private static final ValueSet<Result> VALUE_SET = ValueSet.of(new Result[] { SUCCESSFUL, FAILURE });
|
||||
|
||||
public static Result of(int id) {
|
||||
return VALUE_SET.get(id);
|
||||
|
|
Loading…
Reference in New Issue