删除代表区间的 Interval,使用 guava 的 Range 即可。

This commit is contained in:
2024-11-19 20:29:33 +08:00
parent 7951172d68
commit 3f0c14f2d9
4 changed files with 14 additions and 241 deletions

View File

@@ -18,12 +18,6 @@ package xyz.zhouxy.plusone.commons.util;
import java.math.BigDecimal;
import javax.annotation.Nonnull;
import com.google.common.base.Preconditions;
import xyz.zhouxy.plusone.commons.math.IntervalType;
/**
* Numbers
*
@@ -81,97 +75,6 @@ public class Numbers {
return result;
}
// between
public static boolean between(int value, int min, int max) {
return between(value, min, max, IntervalType.CLOSED_OPEN);
}
public static boolean between(int value, int min, int max, IntervalType intervalType) {
final IntervalType intervalTypeToUse = intervalType != null
? intervalType
: IntervalType.CLOSED_OPEN;
switch (intervalTypeToUse) {
case OPEN:
return min < value && value < max;
case CLOSED:
return min <= value && value <= max;
case OPEN_CLOSED:
return min < value && value <= max;
case CLOSED_OPEN:
default:
return min <= value && value < max;
}
}
public static boolean between(long value, long min, long max) {
return between(value, min, max, IntervalType.CLOSED_OPEN);
}
public static boolean between(long value, long min, long max, IntervalType intervalType) {
final IntervalType intervalTypeToUse = intervalType != null
? intervalType
: IntervalType.CLOSED_OPEN;
switch (intervalTypeToUse) {
case OPEN:
return min < value && value < max;
case CLOSED:
return min <= value && value <= max;
case OPEN_CLOSED:
return min < value && value <= max;
case CLOSED_OPEN:
default:
return min <= value && value < max;
}
}
public static boolean between(double value, double min, double max) {
return between(value, min, max, IntervalType.CLOSED_OPEN);
}
public static boolean between(double value, double min, double max, IntervalType intervalType) {
final IntervalType intervalTypeToUse = intervalType != null
? intervalType
: IntervalType.CLOSED_OPEN;
switch (intervalTypeToUse) {
case OPEN:
return min < value && value < max;
case CLOSED:
return min <= value && value <= max;
case OPEN_CLOSED:
return min < value && value <= max;
case CLOSED_OPEN:
default:
return min <= value && value < max;
}
}
public static <T extends Comparable<T>> boolean between(@Nonnull T value, T min, T max) {
return between(value, min, max, IntervalType.CLOSED_OPEN);
}
public static <T extends Comparable<T>> boolean between(@Nonnull T value, T min, T max, IntervalType intervalType) {
Preconditions.checkArgument(value != null, "The value to valid connot be null.");
IntervalType intervalTypeToUse = intervalType != null
? intervalType
: IntervalType.CLOSED_OPEN;
switch (intervalTypeToUse) {
case OPEN:
return (min == null || min.compareTo(value) < 0)
&& (max == null || value.compareTo(max) < 0);
case CLOSED:
return (min == null || min.compareTo(value) <= 0)
&& (max == null || value.compareTo(max) <= 0);
case OPEN_CLOSED:
return (min == null || min.compareTo(value) < 0)
&& (max == null || value.compareTo(max) <= 0);
case CLOSED_OPEN:
default:
return (min == null || min.compareTo(value) <= 0)
&& (max == null || value.compareTo(max) < 0);
}
}
private Numbers() {
throw new IllegalStateException("Utility class");
}