本文整理了Java中com.google.common.collect.Range
类的一些代码示例,展示了Range
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Range
类的具体详情如下:
包路径:com.google.common.collect.Range
类名称:Range
[英]A range (or "interval") defines the boundaries around a contiguous span of values of some Comparable type; for example, "integers from 1 to 100 inclusive." Note that it is not possible to iterate over these contained values. To do so, pass this range instance and an appropriate DiscreteDomain to ContiguousSet#create.
Each end of the range may be bounded or unbounded. If bounded, there is an associated endpoint value, and the range is considered to be either open (does not include the endpoint) or closed (includes the endpoint) on that side. With three possibilities on each side, this yields nine basic types of ranges, enumerated below. (Notation: a square bracket ( [ ]) indicates that the range is closed on that side; a parenthesis ( ( )) means it is either open or unbounded. The construct {x | statement}} is read "the set of all x such that statement.")
Range TypesNotation Definition Factory method (a..b) {x | a < x < b}} Range#open[a..b] {x | a Range#closed(a..b] {x | a < x Range#openClosed[a..b) {x | a Range#closedOpen(a..+∞) {x | x > a}} Range#greaterThan[a..+∞) {x | x >= a}} Range#atLeast(-∞..b) {x | x < b}} Range#lessThan(-∞..b] {x | x Range#atMost(-∞..+∞){x}} Range#all
When both endpoints exist, the upper endpoint may not be less than the lower. The endpoints may be equal only if at least one of the bounds is closed:
See the Guava User Guide article on Range.
[中]一个范围(或“区间”)定义了一些类似类型的连续值范围周围的边界;例如,“1到100之间的整数。”请注意,不可能迭代这些包含的值。要执行此操作,请将此范围实例和适当的离散域传递给ContiguousSet#create。
####范围的类型
范围的每一端可以是有界的,也可以是无界的。如果有界,则存在一个关联的端点值,并且该范围在该侧被视为打开(不包括端点)或关闭(包括端点)。每边有三种可能性,这就产生了九种基本类型的范围,如下所述。(表示法:方括号([])表示该范围在该侧闭合;括号(())表示它是开放的或无界的。构造{x | statement}}被读取为“所有x的集合,因此该语句。”)
范围类型站点定义工厂方法(a..b){x | a<x<b}范围#打开[a..b]{x | a范围#关闭(a..b){x | a<x范围#打开关闭[a..b){x | a范围#关闭(a+∞) {x|x>a}}范围#大于[a+∞) {x | x>=a}}范围#至少(-∞..b) {x | x<b}范围#lessThan(-∞..b] {x | x范围#大气(-∞..+∞){x} }范围#全部
当两个端点都存在时,上端点不得小于下端点。只有当至少一个边界闭合时,端点才可能相等:
*[a..a]:一个单一的范围
*[a..a];(a..a]:#为空范围;也有效
*(a..a):无效;将引发异常
####警告
*尽可能只使用不可变值类型。如果必须使用可变类型,请不要允许端点实例在创建范围后发生变化!
*如果可能的话,你的价值类型的比较方法应该是可比较的。否则,请注意,本文档中使用的“相等”、“相同”、“唯一”等概念实际上指的是Comparable#compareTo是否返回零,而不是Object#Equals是否返回真。
*一个实现Comparable的类是非常脆弱的,它会导致范围内发生未定义的可怕事情。目前,Range API并没有阻止它的使用,因为这也将排除所有未泛化(JDK1.5之前)的数据类型。这在未来可能会改变。
####其他注释
*这种类型的实例是使用此类中的静态工厂方法获得的。
*范围是凸的:每当包含两个值时,它们之间的所有值都必须包含。更正式地说,因为任何c1都不能用来表示,比如说,“从1到100的所有素数。”
*当作为谓词计算时,范围产生的结果与调用#contains的结果相同。
*术语注:a范围a被称为具有属性P的最大范围,如果对于所有也具有属性P的范围b,a封闭(b)。同样,当b.为所有具有属性P的b封闭(a)时,a是最小的。例如,参见#交的定义。
####进一步阅读
请参阅关于Range的Guava用户指南文章。
代码示例来源:origin: google/guava
public void testCanonical() {
assertEquals(Range.closedOpen(1, 5), Range.closed(1, 4).canonical(integers()));
assertEquals(Range.closedOpen(1, 5), Range.open(0, 5).canonical(integers()));
assertEquals(Range.closedOpen(1, 5), Range.closedOpen(1, 5).canonical(integers()));
assertEquals(Range.closedOpen(1, 5), Range.openClosed(0, 4).canonical(integers()));
assertEquals(
Range.closedOpen(Integer.MIN_VALUE, 0),
Range.closedOpen(Integer.MIN_VALUE, 0).canonical(integers()));
assertEquals(Range.closedOpen(Integer.MIN_VALUE, 0), Range.lessThan(0).canonical(integers()));
assertEquals(Range.closedOpen(Integer.MIN_VALUE, 1), Range.atMost(0).canonical(integers()));
assertEquals(Range.atLeast(0), Range.atLeast(0).canonical(integers()));
assertEquals(Range.atLeast(1), Range.greaterThan(0).canonical(integers()));
assertEquals(Range.atLeast(Integer.MIN_VALUE), Range.<Integer>all().canonical(integers()));
}
代码示例来源:origin: google/guava
complementLowerBoundWindow.hasUpperBound()
? complementLowerBoundWindow.upperEndpoint()
: Cut.<C>aboveAll();
boolean inclusive =
complementLowerBoundWindow.hasUpperBound()
&& complementLowerBoundWindow.upperBoundType() == BoundType.CLOSED;
final PeekingIterator<Range<C>> positiveItr =
Iterators.peekingIterator(
positiveRangesByUpperBound
.headMap(startingPoint, inclusive)
.descendingMap()
.values()
.iterator());
Cut<C> cut;
if (positiveItr.hasNext()) {
cut =
(positiveItr.peek().upperBound == Cut.<C>aboveAll())
? positiveItr.next().lowerBound
: positiveRangesByLowerBound.higherKey(positiveItr.peek().upperBound);
} else if (!complementLowerBoundWindow.contains(Cut.<C>belowAll())
|| positiveRangesByLowerBound.containsKey(Cut.belowAll())) {
return Iterators.emptyIterator();
} else {
cut = positiveRangesByLowerBound.higherKey(Cut.<C>belowAll());
代码示例来源:origin: google/guava
if (complementLowerBoundWindow.hasLowerBound()) {
positiveRanges =
positiveRangesByUpperBound
.tailMap(
complementLowerBoundWindow.lowerEndpoint(),
complementLowerBoundWindow.lowerBoundType() == BoundType.CLOSED)
.values();
} else {
positiveRanges = positiveRangesByUpperBound.values();
Iterators.peekingIterator(positiveRanges.iterator());
final Cut<C> firstComplementRangeLowerBound;
if (complementLowerBoundWindow.contains(Cut.<C>belowAll())
&& (!positiveItr.hasNext() || positiveItr.peek().lowerBound != Cut.<C>belowAll())) {
firstComplementRangeLowerBound = Cut.belowAll();
} else if (positiveItr.hasNext()) {
firstComplementRangeLowerBound = positiveItr.next().upperBound;
} else {
return Iterators.emptyIterator();
代码示例来源:origin: google/guava
/** Converts a Range to a GeneralRange. */
static <T extends Comparable> GeneralRange<T> from(Range<T> range) {
@Nullable T lowerEndpoint = range.hasLowerBound() ? range.lowerEndpoint() : null;
BoundType lowerBoundType = range.hasLowerBound() ? range.lowerBoundType() : OPEN;
@Nullable T upperEndpoint = range.hasUpperBound() ? range.upperEndpoint() : null;
BoundType upperBoundType = range.hasUpperBound() ? range.upperBoundType() : OPEN;
return new GeneralRange<T>(
Ordering.natural(),
range.hasLowerBound(),
lowerEndpoint,
lowerBoundType,
range.hasUpperBound(),
upperEndpoint,
upperBoundType);
}
代码示例来源:origin: google/guava
@Override
public Range<C> rangeContaining(C value) {
int index =
SortedLists.binarySearch(
ranges,
Range.<C>lowerBoundFn(),
Cut.belowValue(value),
Ordering.natural(),
ANY_PRESENT,
NEXT_LOWER);
if (index != -1) {
Range<C> range = ranges.get(index);
return range.contains(value) ? range : null;
}
return null;
}
代码示例来源:origin: google/guava
if (map.comparator() != null
&& map.comparator() != Ordering.natural()
&& range.hasLowerBound()
&& range.hasUpperBound()) {
checkArgument(
map.comparator().compare(range.lowerEndpoint(), range.upperEndpoint()) <= 0,
"map is using a custom comparator which is inconsistent with the natural ordering.");
if (range.hasLowerBound() && range.hasUpperBound()) {
return map.subMap(
range.lowerEndpoint(),
range.lowerBoundType() == BoundType.CLOSED,
range.upperEndpoint(),
range.upperBoundType() == BoundType.CLOSED);
} else if (range.hasLowerBound()) {
return map.tailMap(range.lowerEndpoint(), range.lowerBoundType() == BoundType.CLOSED);
} else if (range.hasUpperBound()) {
return map.headMap(range.upperEndpoint(), range.upperBoundType() == BoundType.CLOSED);
代码示例来源:origin: google/guava
if (ranges.isEmpty() || range.isEmpty()) {
return ImmutableList.of();
} else if (range.encloses(span())) {
return ranges;
if (range.hasLowerBound()) {
fromIndex =
SortedLists.binarySearch(
ranges,
Range.<C>upperBoundFn(),
range.lowerBound,
KeyPresentBehavior.FIRST_AFTER,
if (range.hasUpperBound()) {
toIndex =
SortedLists.binarySearch(
ranges,
Range.<C>lowerBoundFn(),
range.upperBound,
KeyPresentBehavior.FIRST_PRESENT,
KeyAbsentBehavior.NEXT_HIGHER);
} else {
toIndex = ranges.size();
代码示例来源:origin: google/guava
public void testAsList() {
ImmutableList<Integer> list = ContiguousSet.create(Range.closed(1, 3), integers()).asList();
for (int i = 0; i < 3; i++) {
assertEquals(i + 1, list.get(i).intValue());
}
assertEquals(ImmutableList.of(1, 2, 3), ImmutableList.copyOf(list.iterator()));
assertEquals(ImmutableList.of(1, 2, 3), ImmutableList.copyOf(list.toArray(new Integer[0])));
}
代码示例来源:origin: google/guava
public void testToImmutableRangeSet() {
Range<Integer> rangeOne = Range.closedOpen(1, 5);
Range<Integer> rangeTwo = Range.openClosed(6, 7);
ImmutableRangeSet<Integer> ranges =
ImmutableRangeSet.copyOf(ImmutableList.of(rangeOne, rangeTwo));
CollectorTester.of(ImmutableRangeSet.<Integer>toImmutableRangeSet())
.expectCollects(ranges, rangeOne, rangeTwo);
}
}
代码示例来源:origin: google/google-java-format
/**
* Format an input string (a Java compilation unit) into an output string.
*
* <p>Leaves import statements untouched.
*
* @param input the input string
* @return the output string
* @throws FormatterException if the input string cannot be parsed
*/
public String formatSource(String input) throws FormatterException {
return formatSource(input, ImmutableList.of(Range.closedOpen(0, input.length())));
}
代码示例来源:origin: google/guava
public void testOrderingCuts() {
Cut<Integer> a = Range.lessThan(0).lowerBound;
Cut<Integer> b = Range.atLeast(0).lowerBound;
Cut<Integer> c = Range.greaterThan(0).lowerBound;
Cut<Integer> d = Range.atLeast(1).lowerBound;
Cut<Integer> e = Range.greaterThan(1).lowerBound;
Cut<Integer> f = Range.greaterThan(1).upperBound;
Helpers.testCompareToAndEquals(ImmutableList.of(a, b, c, d, e, f));
}
代码示例来源:origin: apache/incubator-druid
@Test
public void testSimpleFilter()
{
RangeSet expected1 = rangeSet(point("a"));
Assert.assertEquals(expected1, selector1.getDimensionRangeSet("dim1"));
Assert.assertNull(selector1.getDimensionRangeSet("dim2"));
RangeSet expected2 = rangeSet(point(null));
Assert.assertEquals(expected2, selector5.getDimensionRangeSet("dim1"));
RangeSet expected3 = rangeSet(ImmutableList.of(point("testing"), point("this"), point("filter"), point("tillend")));
Assert.assertEquals(expected3, in1.getDimensionRangeSet("dim1"));
RangeSet expected4 = rangeSet(ImmutableList.of(point("null"), point(null)));
Assert.assertEquals(expected4, in3.getDimensionRangeSet("dim1"));
RangeSet expected5 = ImmutableRangeSet.of(Range.closed("from", "to"));
Assert.assertEquals(expected5, bound1.getDimensionRangeSet("dim1"));
RangeSet expected6 = ImmutableRangeSet.of(Range.atMost("tillend"));
Assert.assertEquals(expected6, bound2.getDimensionRangeSet("dim1"));
RangeSet expected7 = ImmutableRangeSet.of(Range.greaterThan("notincluded"));
Assert.assertEquals(expected7, bound3.getDimensionRangeSet("dim1"));
Assert.assertNull(other1.getDimensionRangeSet("someDim"));
Assert.assertNull(other2.getDimensionRangeSet("someOtherDim"));
Assert.assertNull(other3.getDimensionRangeSet("dim"));
Assert.assertNull(interval1.getDimensionRangeSet(ColumnHolder.TIME_COLUMN_NAME));
Assert.assertNull(interval2.getDimensionRangeSet("dim1"));
}
代码示例来源:origin: org.apache.brooklyn/brooklyn-utils-common
@Test
public void testPortRulesToRanges() throws Exception {
RangeSet<Integer> actualRangeSet = Networking.portRulesToRanges(ImmutableList.of(
"22", "23", "5000-6000", "8081", "80-90", "90-100", "23", "8081"));
Asserts.assertEquals(actualRangeSet, ImmutableRangeSet.<Integer>builder()
.add(Range.closed(22, 22))
.add(Range.closed(23, 23))
.add(Range.closed(80, 100))
.add(Range.closed(5000, 6000))
.add(Range.closed(8081, 8081))
.build());
}
代码示例来源:origin: google/guava
@Override
Iterator<Entry<Cut<C>, Range<C>>> descendingEntryIterator() {
Collection<Range<C>> candidates;
if (upperBoundWindow.hasUpperBound()) {
candidates =
rangesByLowerBound
.headMap(upperBoundWindow.upperEndpoint(), false)
.descendingMap()
.values();
} else {
candidates = rangesByLowerBound.descendingMap().values();
}
final PeekingIterator<Range<C>> backingItr = Iterators.peekingIterator(candidates.iterator());
if (backingItr.hasNext()
&& upperBoundWindow.upperBound.isLessThan(backingItr.peek().upperBound)) {
backingItr.next();
}
return new AbstractIterator<Entry<Cut<C>, Range<C>>>() {
@Override
protected Entry<Cut<C>, Range<C>> computeNext() {
if (!backingItr.hasNext()) {
return endOfData();
}
Range<C> range = backingItr.next();
return upperBoundWindow.lowerBound.isLessThan(range.upperBound)
? Maps.immutableEntry(range.upperBound, range)
: endOfData();
}
};
}
代码示例来源:origin: google/guava
public void testSubRangeSet() {
ImmutableList.Builder<Range<Integer>> rangesBuilder = ImmutableList.builder();
rangesBuilder.add(Range.<Integer>all());
for (int i = -2; i <= 2; i++) {
for (BoundType boundType : BoundType.values()) {
rangesBuilder.add(Range.upTo(i, boundType));
rangesBuilder.add(Range.downTo(i, boundType));
rangesBuilder.add(Range.range(i, lbType, j, ubType));
rangesBuilder.add(Range.closedOpen(i, i));
rangesBuilder.add(Range.openClosed(i, i));
if (!range1.isConnected(range2) || range1.intersection(range2).isEmpty()) {
ImmutableRangeSet<Integer> rangeSet =
ImmutableRangeSet.<Integer>builder().add(range1).add(range2).build();
for (Range<Integer> subRange : subRanges) {
RangeSet<Integer> expected = TreeRangeSet.create();
for (Range<Integer> range : rangeSet.asRanges()) {
if (range.isConnected(subRange)) {
expected.add(range.intersection(subRange));
ImmutableRangeSet<Integer> subRangeSet = rangeSet.subRangeSet(subRange);
assertEquals(expected, subRangeSet);
assertEquals(expected.asRanges(), subRangeSet.asRanges());
代码示例来源:origin: google/guava
public void testClosedOpen() {
Range<Integer> range = Range.closedOpen(5, 8);
checkContains(range);
assertTrue(range.hasLowerBound());
assertEquals(5, (int) range.lowerEndpoint());
assertEquals(CLOSED, range.lowerBoundType());
assertTrue(range.hasUpperBound());
assertEquals(8, (int) range.upperEndpoint());
assertEquals(OPEN, range.upperBoundType());
assertFalse(range.isEmpty());
assertEquals("[5..8)", range.toString());
reserializeAndAssert(range);
}
代码示例来源:origin: google/google-java-format
private RangeSet<Integer> characterRanges(String input) {
final RangeSet<Integer> characterRanges = TreeRangeSet.create();
if (parameters.lines().isEmpty() && parameters.offsets().isEmpty()) {
characterRanges.add(Range.closedOpen(0, input.length()));
return characterRanges;
}
characterRanges.addAll(Formatter.lineRangesToCharRanges(input, parameters.lines()));
for (int i = 0; i < parameters.offsets().size(); i++) {
Integer length = parameters.lengths().get(i);
if (length == 0) {
// 0 stands for "format the line under the cursor"
length = 1;
}
characterRanges.add(
Range.closedOpen(parameters.offsets().get(i), parameters.offsets().get(i) + length));
}
return characterRanges;
}
}
代码示例来源:origin: google/guava
@Override
public @Nullable Range<C> get(@Nullable Object key) {
if (key instanceof Cut) {
try {
@SuppressWarnings("unchecked") // we catch CCE's
Cut<C> cut = (Cut<C>) key;
if (!lowerBoundWindow.contains(cut)
|| cut.compareTo(restriction.lowerBound) < 0
|| cut.compareTo(restriction.upperBound) >= 0) {
return null;
} else if (cut.equals(restriction.lowerBound)) {
// it might be present, truncated on the left
Range<C> candidate = Maps.valueOrNull(rangesByLowerBound.floorEntry(cut));
if (candidate != null && candidate.upperBound.compareTo(restriction.lowerBound) > 0) {
return candidate.intersection(restriction);
}
} else {
Range<C> result = rangesByLowerBound.get(cut);
if (result != null) {
return result.intersection(restriction);
}
}
} catch (ClassCastException e) {
return null;
}
}
return null;
}
代码示例来源:origin: google/guava
if (!upperBoundWindow.hasLowerBound()) {
backingItr = rangesByLowerBound.values().iterator();
} else {
Entry<Cut<C>, Range<C>> lowerEntry =
rangesByLowerBound.lowerEntry(upperBoundWindow.lowerEndpoint());
if (lowerEntry == null) {
backingItr = rangesByLowerBound.values().iterator();
} else if (upperBoundWindow.lowerBound.isLessThan(lowerEntry.getValue().upperBound)) {
backingItr = rangesByLowerBound.tailMap(lowerEntry.getKey(), true).values().iterator();
} else {
backingItr =
rangesByLowerBound
.tailMap(upperBoundWindow.lowerEndpoint(), true)
.values()
.iterator();
代码示例来源:origin: google/guava
public void testEquivalentFactories() {
new EqualsTester()
.addEqualityGroup(Range.all())
.addEqualityGroup(Range.atLeast(1), Range.downTo(1, CLOSED))
.addEqualityGroup(Range.greaterThan(1), Range.downTo(1, OPEN))
.addEqualityGroup(Range.atMost(7), Range.upTo(7, CLOSED))
.addEqualityGroup(Range.lessThan(7), Range.upTo(7, OPEN))
.addEqualityGroup(Range.open(1, 7), Range.range(1, OPEN, 7, OPEN))
.addEqualityGroup(Range.openClosed(1, 7), Range.range(1, OPEN, 7, CLOSED))
.addEqualityGroup(Range.closed(1, 7), Range.range(1, CLOSED, 7, CLOSED))
.addEqualityGroup(Range.closedOpen(1, 7), Range.range(1, CLOSED, 7, OPEN))
.testEquals();
}
}
内容来源于网络,如有侵权,请联系作者删除!