inScope(int min, int max, boolean minInclude, boolean maxInclude)
这样的代码看起来很繁琐。
Guava提供了一种数据结构叫做Range,正好用于表达这样一种数据。
Range提供了一些静态工厂方法,用于构造各种是否包含边界的范围:
Notation |
Definition |
Factory method |
(a..b) |
{x | a < x < b} |
|
[a..b] |
{x | a <= x <= b} |
|
(a..b] |
{x | a < x <= b} |
|
[a..b) |
{x | a <= x < b} |
|
(a..+∞) |
{x | x > a} |
|
[a..+∞) |
{x | x >= a} |
|
(-∞..b) |
{x | x < b} |
|
(-∞..b] |
{x | x <= b} |
|
{x} |
如:Range.openClosed(4,6) 就表示(4,6]这样的范围,其实包含数据5,6。
range的参数支持范型,这样任何实现Comparable的对象都能用于Range。
此外,Range也能通过参数指定是否包含边界:
range(C, BoundType, C, BoundType) : 如range(4, BoundType.CLOSE,6, BoundType.OPEN) 表示[4,6)
downTo(C, BoundType) 表示(a..+∞) or [a..+∞)
upTo(C, BoundType) 表示(-∞..b) or (-∞..b]
得到Range对象后,Range提供了一些方法来使用Range对象:
contains(c):判断是否包含数据c
containsAll(list):判断是否包含集合所有数据
hasLowerBound() and hasUpperBound(): 判断是否包含下边界及上边界
lowerBoundType() and upperBoundType() :返回边界包含类型(OPEN or CLOSED),如果没有边界将抛IllegalStateException
lowerEndpoint() and upperEndpoint() :返回边界值,如果没有边界将抛IllegalStateException
isEmpty() :是否是空范围,如(4,4]
集合处理:
Encloses:是否包含,如:[3..6] encloses [4..5],[4..5] does not enclose (3..6)
isConnected:两个范围是否是相邻的,如:[3,5]与(5,10)相邻,(0,9)与(5,10)相邻,(3,5)与(5,10)不相邻
Intersection:返回2个范围的交集。如果两个范围不相邻,抛IllegalArgumentException。
如:(3,5]与[5,10]的交集是[5,5];(0,9)和(3,4)的交集是(3,4)
Span: 返回包含2个范围的最小范围,如:(1, 5)和(6, 10)的span是(1,10)
相关推荐
赠送jar包:guava-30.0-jre.jar; 赠送原API文档:guava-30.0-jre-javadoc.jar; 赠送源代码:guava-30.0-jre-sources.jar; 赠送Maven依赖信息文件:guava-30.0-jre.pom; 包含翻译后的API文档:guava-30.0-jre-...
赠送jar包:guava-27.0.1-jre.jar; 赠送原API文档:guava-27.0.1-jre-javadoc.jar; 赠送源代码:guava-27.0.1-jre-sources.jar; 赠送Maven依赖信息文件:guava-27.0.1-jre.pom; 包含翻译后的API文档:guava-...
赠送jar包:guava-25.0-jre.jar; 赠送原API文档:guava-25.0-jre-javadoc.jar; 赠送源代码:guava-25.0-jre-sources.jar; 赠送Maven依赖信息文件:guava-25.0-jre.pom; 包含翻译后的API文档:guava-25.0-jre-...
赠送jar包:guava-28.2-jre.jar; 赠送原API文档:guava-28.2-jre-javadoc.jar; 赠送源代码:guava-28.2-jre-sources.jar; 赠送Maven依赖信息文件:guava-28.2-jre.pom; 包含翻译后的API文档:guava-28.2-jre-...
guava-27.0-jre.jar
赠送jar包:guava-11.0.2.jar; 赠送原API文档:guava-11.0.2-javadoc.jar; 赠送源代码:guava-11.0.2-sources.jar; 赠送Maven依赖信息文件:guava-11.0.2.pom; 包含翻译后的API文档:guava-11.0.2-javadoc-API...
赠送jar包:guava-29.0-jre.jar; 赠送原API文档:guava-29.0-jre-javadoc.jar; 赠送源代码:guava-29.0-jre-sources.jar; 赠送Maven依赖信息文件:guava-29.0-jre.pom; 包含翻译后的API文档:guava-29.0-jre-...
赠送jar包:guava-20.0.jar; 赠送原API文档:guava-20.0-javadoc.jar; 赠送源代码:guava-20.0-sources.jar; 赠送Maven依赖信息文件:guava-20.0.pom; 包含翻译后的API文档:guava-20.0-javadoc-API文档-中文...
guava-28.2-android.jar
最新版 guava-30.1-jre.jar
赠送jar包:guava-28.2-jre.jar; 赠送原API文档:guava-28.2-jre-javadoc.jar; 赠送源代码:guava-28.2-jre-sources.jar; 赠送Maven依赖信息文件:guava-28.2-jre.pom; 包含翻译后的API文档:guava-28.2-jre-...
guava-27.1-jre
赠送jar包:guava-29.0-jre.jar; 赠送原API文档:guava-29.0-jre-javadoc.jar; 赠送源代码:guava-29.0-jre-sources.jar; 赠送Maven依赖信息文件:guava-29.0-jre.pom; 包含翻译后的API文档:guava-29.0-jre-...
赠送jar包:guava-26.0-android.jar; 赠送原API文档:guava-26.0-android-javadoc.jar; 赠送源代码:guava-26.0-android-sources.jar; 赠送Maven依赖信息文件:guava-26.0-android.pom; 包含翻译后的API文档:...
赠送jar包:guava-24.1-jre.jar; 赠送原API文档:guava-24.1-jre-javadoc.jar; 赠送源代码:guava-24.1-jre-sources.jar; 赠送Maven依赖信息文件:guava-24.1-jre.pom; 包含翻译后的API文档:guava-24.1-jre-...
赠送jar包:guava-20.0.jar; 赠送原API文档:guava-20.0-javadoc.jar; 赠送源代码:guava-20.0-sources.jar; 赠送Maven依赖信息文件:guava-20.0.pom; 包含翻译后的API文档:guava-20.0-javadoc-API文档-中文...
赠送jar包:guava-23.0.jar; 赠送原API文档:guava-23.0-javadoc.jar; 赠送源代码:guava-23.0-sources.jar; 赠送Maven依赖信息文件:guava-23.0.pom; 包含翻译后的API文档:guava-23.0-javadoc-API文档-中文...
赠送jar包:guava-18.0.jar; 赠送原API文档:guava-18.0-javadoc.jar; 赠送源代码:guava-18.0-sources.jar; 包含翻译后的API文档:guava-18.0-javadoc-API文档-中文(简体)版.zip 对应Maven信息:groupId:...
赠送jar包:guava-28.0-android.jar; 赠送原API文档:guava-28.0-android-javadoc.jar; 赠送源代码:guava-28.0-android-sources.jar; 赠送Maven依赖信息文件:guava-28.0-android.pom; 包含翻译后的API文档:...
guava