T
- 边界值类型public class BoundedRange<T extends Comparable<? super T>> extends Object implements SerPredicate<T>
参考Guava的Range实现,用于描述作为上下界的两个Bound
实例围成的一段区间。
作为Predicate
使用时,可检验指定值是否在区间中,即指定值是否同时满足上下界的Bound.test(T)
方法。
区间的类型,支持通过工厂方法创建下述几种类型的区间:
区间 | 数学定义 | 工厂方法 |
---|---|---|
(a, b) | {x | a < x < b} | open(T, T)
|
[a, b] | {x | a <= x <= b} | close(T, T)
|
(a, b] | {x | a < x <= b} | openClose(T, T)
|
[a, b) | {x | a <= x < b} | closeOpen(T, T)
|
(a, +∞) | {x | x > a} | greaterThan(T)
|
[a, +∞) | {x | x >= a} | atLeast(T)
|
(-∞, b) | {x | x < b} | lessThan(T)
|
(-∞, b] | {x | x <= b} | atMost(T)
|
(-∞, +∞) | {x} | all()
|
空区间
根据数学定义,当区间中无任何实数时,认为该区间 代表的集合为空集,
用户可通过isEmpty()
确认当前实例是否为空区间。
若实例上界a,下界为b,则当实例满足下述任意条件时,认为其为一个空区间:
a > b
;[a, b)
,且a == b
;(a, b)
,且a == b
;(a, b]
,且a == b
;IllegalArgumentException
,
但是通过交并操作仍有可能创建出满足上述描述的空区间。
此时若空区间参与操作可能得到意外的结果,
因此对通过非工厂方法得到的区间,在操作前有必要通过isEmpty()
进行检验。Bound
,
Serialized FormModifier and Type | Method and Description |
---|---|
static <T extends Comparable<? super T>> |
all()
构建一个上下界皆无限大的区间,即
{x | -∞ < x < +∞} |
static <T extends Comparable<? super T>> |
atLeast(T lowerBound)
{x | lowerBound < x < +∞} |
static <T extends Comparable<? super T>> |
atMost(T upperBound)
{x | -∞ < x <= max} |
static <T extends Comparable<? super T>> |
close(T lowerBound,
T upperBound)
构建一个闭区间,即
{x | lowerBound <= x <= upperBound} |
static <T extends Comparable<? super T>> |
closeOpen(T lowerBound,
T upperBound)
构建一个左闭右开区间,即
{x | lowerBound <= x < upperBound} |
boolean |
equals(Object o)
比较两个实例是否相等
|
BoundedRange<T> |
gap(BoundedRange<T> other)
若
other 与当前区间不相连,则获得两区间中间的间隔部分 |
Bound<T> |
getLowerBound()
获取下界
|
T |
getLowerBoundValue()
获取下界值
|
Bound<T> |
getUpperBound()
获取上界
|
T |
getUpperBoundValue()
获取上界值
|
static <T extends Comparable<? super T>> |
greaterThan(T lowerBound)
{x | lowerBound < x < +∞} |
int |
hashCode()
获取实例哈希值
|
boolean |
hasLowerBound()
是否有下界
|
boolean |
hasUpperBound()
是否有上界
|
BoundedRange<T> |
intersection(BoundedRange<T> other)
若
other 与当前区间相交,则获得该区间与当前区间的交集 |
boolean |
isDisjoint(BoundedRange<T> other)
other 是否与当前区间不相交 |
boolean |
isEmpty()
当前区间是否为空。
|
boolean |
isIntersected(BoundedRange<T> other)
other 是否与当前区间相交: |
boolean |
isProperSubset(BoundedRange<T> other)
当前区间是否是
other 的真子集 |
boolean |
isProperSuperset(BoundedRange<T> other)
other 是否是当前区间的子集 |
boolean |
isSubset(BoundedRange<T> other)
当前区间是否是
other 的子集 |
boolean |
isSuperset(BoundedRange<T> other)
other 是否是当前区间的子集 |
static <T extends Comparable<? super T>> |
lessThan(T upperBound)
{x | -∞ < x < upperBound} |
static <T extends Comparable<? super T>> |
open(T lowerBound,
T upperBound)
构建一个开区间,即
{x | lowerBound < x < upperBound} |
static <T extends Comparable<? super T>> |
openClose(T lowerBound,
T upperBound)
构建一个左闭右开区间,即
{x | lowerBound < x <= upperBound} |
BoundedRange<T> |
span(BoundedRange<T> other)
获得包含当前区间与指定区间的最小的区间
|
BoundedRange<T> |
subAtLeast(T min)
截取当前区间中大于等于
min 的部分,若min 不在该区间中,则返回当前区间本身 |
BoundedRange<T> |
subAtMost(T max)
截取当前区间中小于等于
max 的部分,若max 不在该区间中,则返回当前区间本身 |
BoundedRange<T> |
subGreatThan(T min)
截取当前区间中大于
min 的部分,若min 不在该区间中,则返回当前区间本身 |
BoundedRange<T> |
subLessThan(T max)
截取当前区间中小于
max 的部分,若max 不在该区间中,则返回当前区间本身 |
boolean |
testing(T value)
指定值是否在当前区间内
|
String |
toString()
输出当前区间的字符串,格式为
"[a, b]" |
BoundedRange<T> |
unionIfIntersected(BoundedRange<T> other)
若
other 与当前区间相交,则将其与当前区间合并。 |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
public static <T extends Comparable<? super T>> BoundedRange<T> all()
{x | -∞ < x < +∞}
T
- 比较对象类型public static <T extends Comparable<? super T>> BoundedRange<T> close(T lowerBound, T upperBound)
{x | lowerBound <= x <= upperBound}
T
- 边界值类型lowerBound
- 下界,不能为空upperBound
- 上界,不能为空IllegalArgumentException
- 当创建的区间表示的集合为空时抛出NullPointerException
- 上界或下界为null
时抛出public static <T extends Comparable<? super T>> BoundedRange<T> open(T lowerBound, T upperBound)
{x | lowerBound < x < upperBound}
T
- 边界值类型lowerBound
- 下界,不能为空upperBound
- 上界,不能为空IllegalArgumentException
- 当创建的区间表示的集合为空时抛出NullPointerException
- 上界或下界为null
时抛出public static <T extends Comparable<? super T>> BoundedRange<T> closeOpen(T lowerBound, T upperBound)
{x | lowerBound <= x < upperBound}
T
- 边界值类型lowerBound
- 下界,不能为空upperBound
- 上界,不能为空IllegalArgumentException
- 当创建的区间表示的集合为空时抛出NullPointerException
- 上界或下界为null
时抛出public static <T extends Comparable<? super T>> BoundedRange<T> openClose(T lowerBound, T upperBound)
{x | lowerBound < x <= upperBound}
T
- 边界值类型lowerBound
- 下界,不能为空upperBound
- 上界,不能为空IllegalArgumentException
- 当创建的区间表示的集合为空时抛出NullPointerException
- 上界或下界为null
时抛出public static <T extends Comparable<? super T>> BoundedRange<T> greaterThan(T lowerBound)
{x | lowerBound < x < +∞}
T
- 边界值类型lowerBound
- 下界,不能为空NullPointerException
- 下界为null
时抛出Bound.toRange()
public static <T extends Comparable<? super T>> BoundedRange<T> atLeast(T lowerBound)
{x | lowerBound < x < +∞}
T
- 边界值类型lowerBound
- 下界,不能为空NullPointerException
- 下界为null
时抛出Bound.toRange()
public static <T extends Comparable<? super T>> BoundedRange<T> lessThan(T upperBound)
{x | -∞ < x < upperBound}
T
- 边界值类型upperBound
- 上界,不能为空NullPointerException
- 上界为null
时抛出Bound.toRange()
public static <T extends Comparable<? super T>> BoundedRange<T> atMost(T upperBound)
{x | -∞ < x <= max}
T
- 边界值类型upperBound
- 上界,不能为空NullPointerException
- 上界为null
时抛出Bound.toRange()
public T getLowerBoundValue()
public boolean hasLowerBound()
public T getUpperBoundValue()
public boolean hasUpperBound()
public boolean isEmpty()
当前区间是否为空。
当由下界left与上界right构成的区间,
符合下述任意条件时,认为当前区间为空:
left > right
;[left, right)
,有left == right
;(left, right)
,有left == right
;(left, right]
,有left == right
;public String toString()
"[a, b]"
public boolean equals(Object o)
public boolean isSuperset(BoundedRange<T> other)
other
是否是当前区间的子集other
- 另一个区间public boolean isProperSuperset(BoundedRange<T> other)
other
是否是当前区间的子集other
- 另一个区间public boolean isSubset(BoundedRange<T> other)
other
的子集other
- 另一个区间public boolean isProperSubset(BoundedRange<T> other)
other
的真子集other
- 另一个区间public boolean isDisjoint(BoundedRange<T> other)
other
是否与当前区间不相交other
- 另一个区间public boolean isIntersected(BoundedRange<T> other)
other
是否与当前区间相交:other
- 另一个区间public boolean testing(T value)
testing
in interface SerPredicate<T extends Comparable<? super T>>
value
- 要检测的值public BoundedRange<T> unionIfIntersected(BoundedRange<T> other)
other
与当前区间相交,则将其与当前区间合并。other
- 另一个区间public BoundedRange<T> span(BoundedRange<T> other)
other
- 另一个区间public BoundedRange<T> gap(BoundedRange<T> other)
other
与当前区间不相连,则获得两区间中间的间隔部分other
- 另一个区间null
public BoundedRange<T> intersection(BoundedRange<T> other)
other
与当前区间相交,则获得该区间与当前区间的交集other
- 另一个区间null
public BoundedRange<T> subGreatThan(T min)
min
的部分,若min
不在该区间中,则返回当前区间本身min
- 最大的左值public BoundedRange<T> subAtLeast(T min)
min
的部分,若min
不在该区间中,则返回当前区间本身min
- 最大的左值public BoundedRange<T> subLessThan(T max)
max
的部分,若max
不在该区间中,则返回当前区间本身max
- 最大的左值public BoundedRange<T> subAtMost(T max)
max
的部分,若max
不在该区间中,则返回当前区间本身max
- 最大的左值Copyright © 2025. All rights reserved.