新增区间的建模
parent
5e5202ff3a
commit
1a9960dbf1
|
@ -0,0 +1,62 @@
|
||||||
|
package xyz.zhouxy.plusone.commons.math;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
|
import com.google.common.base.Preconditions;
|
||||||
|
|
||||||
|
import xyz.zhouxy.plusone.commons.util.Numbers;
|
||||||
|
|
||||||
|
public class Interval<T extends Comparable<T>> {
|
||||||
|
@Nonnull
|
||||||
|
private final IntervalType intervalType;
|
||||||
|
private final T lowerBound;
|
||||||
|
private final T upperBound;
|
||||||
|
|
||||||
|
public Interval(@Nonnull IntervalType intervalType, T lowerBound, T upperBound) {
|
||||||
|
Preconditions.checkNotNull(intervalType);
|
||||||
|
if (intervalType.isLeftClosed()) {
|
||||||
|
Preconditions.checkArgument(lowerBound != null,
|
||||||
|
"The lower bound cannot be null, when the interval is left-closed.");
|
||||||
|
}
|
||||||
|
if (intervalType.isRightClosed()) {
|
||||||
|
Preconditions.checkArgument(upperBound != null,
|
||||||
|
"The upper bound cannot be null, when the interval is right-closed.");
|
||||||
|
}
|
||||||
|
if (lowerBound != null && upperBound != null) {
|
||||||
|
Preconditions.checkArgument(lowerBound.compareTo(upperBound) <= 0,
|
||||||
|
"The lower bound must less than the upper bound.");
|
||||||
|
}
|
||||||
|
this.intervalType = intervalType;
|
||||||
|
this.lowerBound = lowerBound;
|
||||||
|
this.upperBound = upperBound;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
public IntervalType getIntervalType() {
|
||||||
|
return intervalType;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
public Optional<T> getLowerBound() {
|
||||||
|
return Optional.ofNullable(lowerBound);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
public Optional<T> getUpperBound() {
|
||||||
|
return Optional.ofNullable(upperBound);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isLeftClosed() {
|
||||||
|
return this.intervalType.isLeftClosed();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isRightClosed() {
|
||||||
|
return this.intervalType.isRightClosed();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean validValue(@Nonnull T value) {
|
||||||
|
return Numbers.between(value, this.lowerBound, this.upperBound, this.intervalType);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
package xyz.zhouxy.plusone.commons.math;
|
||||||
|
|
||||||
|
public enum IntervalType {
|
||||||
|
/** 开区间。(a,b)={x|a < x < b} */
|
||||||
|
OPEN(false, false),
|
||||||
|
/** 闭区间。[a,b]={x|a ≤ x ≤ b} */
|
||||||
|
CLOSED(true, true),
|
||||||
|
/** 左闭右开区间。[a,b)={x|a ≤ x < b} */
|
||||||
|
CLOSED_OPEN(true, false),
|
||||||
|
/** 左开右闭区间。(a,b]={x|a < x ≤ b} */
|
||||||
|
OPEN_CLOSED(false, true);
|
||||||
|
|
||||||
|
private final boolean leftClosed;
|
||||||
|
private final boolean rightClosed;
|
||||||
|
|
||||||
|
IntervalType(boolean leftClosed, boolean rightClosed) {
|
||||||
|
this.leftClosed = leftClosed;
|
||||||
|
this.rightClosed = rightClosed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public final boolean isLeftClosed() {
|
||||||
|
return leftClosed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public final boolean isRightClosed() {
|
||||||
|
return rightClosed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public final <T extends Comparable<T>> Interval<T> buildInterval(T left, T right) {
|
||||||
|
return new Interval<>(this, left, right);
|
||||||
|
}
|
||||||
|
}
|
|
@ -18,6 +18,12 @@ package xyz.zhouxy.plusone.commons.util;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
|
import com.google.common.base.Preconditions;
|
||||||
|
|
||||||
|
import xyz.zhouxy.plusone.commons.math.IntervalType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Numbers
|
* Numbers
|
||||||
*
|
*
|
||||||
|
@ -77,28 +83,93 @@ public class Numbers {
|
||||||
|
|
||||||
// between
|
// between
|
||||||
|
|
||||||
public static boolean between(short value, short min, short max) {
|
public static boolean between(int value, int min, int max) {
|
||||||
return value >= min && value < max;
|
return between(value, min, max, IntervalType.CLOSED_OPEN);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean between(int value, int min, int max) {
|
public static boolean between(int value, int min, int max, IntervalType intervalType) {
|
||||||
return value >= min && value < max;
|
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) {
|
public static boolean between(long value, long min, long max) {
|
||||||
return value >= min && value < max;
|
return between(value, min, max, IntervalType.CLOSED_OPEN);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean between(float value, float min, float max) {
|
public static boolean between(long value, long min, long max, IntervalType intervalType) {
|
||||||
return value >= min && value < max;
|
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) {
|
public static boolean between(double value, double min, double max) {
|
||||||
return value >= min && value < max;
|
return between(value, min, max, IntervalType.CLOSED_OPEN);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean between(BigDecimal value, BigDecimal min, BigDecimal max) {
|
public static boolean between(double value, double min, double max, IntervalType intervalType) {
|
||||||
return BigDecimals.ge(value, min) && BigDecimals.lt(value, max);
|
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() {
|
private Numbers() {
|
||||||
|
|
Loading…
Reference in New Issue