com.google.common.collect.Range.isEmpty()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(9.4k)|赞(0)|评价(0)|浏览(107)

本文整理了Java中com.google.common.collect.Range.isEmpty方法的一些代码示例,展示了Range.isEmpty的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Range.isEmpty方法的具体详情如下:
包路径:com.google.common.collect.Range
类名称:Range
方法名:isEmpty

Range.isEmpty介绍

[英]Returns true if this range is of the form [v..v) or (v..v]. (This does not encompass ranges of the form (v..v), because such ranges are invalid and can't be constructed at all.)

Note that certain discrete ranges such as the integer range (3..4) are not considered empty, even though they contain no actual values. In these cases, it may be helpful to preprocess ranges with #canonical(DiscreteDomain).
[中]如果此范围的形式为[v..v]或(v..v]),则返回true(这不包括形式(v..v)的范围,因为此类范围无效,根本无法构造。)
请注意,某些离散范围(如整数范围(3..4))不被视为空,即使它们不包含实际值。在这些情况下,使用#规范(离散域)预处理范围可能会有所帮助。

代码示例

代码示例来源:origin: google/guava

private void replaceRangeWithSameLowerBound(Range<C> range) {
 if (range.isEmpty()) {
  rangesByLowerBound.remove(range.lowerBound);
 } else {
  rangesByLowerBound.put(range.lowerBound, range);
 }
}

代码示例来源:origin: google/guava

/**
 * Add the specified range to this builder. Adjacent ranges are permitted and will be merged,
 * but overlapping ranges will cause an exception when {@link #build()} is called.
 *
 * @throws IllegalArgumentException if {@code range} is empty
 */
@CanIgnoreReturnValue
public Builder<C> add(Range<C> range) {
 checkArgument(!range.isEmpty(), "range must not be empty, but was %s", range);
 ranges.add(range);
 return this;
}

代码示例来源:origin: google/guava

/**
 * Associates the specified range with the specified value.
 *
 * @throws IllegalArgumentException if {@code range} is empty
 */
@CanIgnoreReturnValue
public Builder<K, V> put(Range<K> range, V value) {
 checkNotNull(range);
 checkNotNull(value);
 checkArgument(!range.isEmpty(), "Range must not be empty, but was %s", range);
 entries.add(Maps.immutableEntry(range, value));
 return this;
}

代码示例来源:origin: google/guava

@Override
public void put(Range<K> range, V value) {
 // don't short-circuit if the range is empty - it may be between two ranges we can coalesce.
 if (!range.isEmpty()) {
  checkNotNull(value);
  remove(range);
  entriesByLowerBound.put(range.lowerBound, new RangeMapEntry<K, V>(range, value));
 }
}

代码示例来源:origin: google/guava

@Override
public boolean intersects(Range<C> range) {
 checkNotNull(range);
 Entry<Cut<C>, Range<C>> ceilingEntry = rangesByLowerBound.ceilingEntry(range.lowerBound);
 if (ceilingEntry != null
   && ceilingEntry.getValue().isConnected(range)
   && !ceilingEntry.getValue().intersection(range).isEmpty()) {
  return true;
 }
 Entry<Cut<C>, Range<C>> priorEntry = rangesByLowerBound.lowerEntry(range.lowerBound);
 return priorEntry != null
   && priorEntry.getValue().isConnected(range)
   && !priorEntry.getValue().intersection(range).isEmpty();
}

代码示例来源:origin: google/guava

/**
 * Returns an immutable range set containing the specified single range. If {@link Range#isEmpty()
 * range.isEmpty()}, this is equivalent to {@link ImmutableRangeSet#of()}.
 */
public static <C extends Comparable> ImmutableRangeSet<C> of(Range<C> range) {
 checkNotNull(range);
 if (range.isEmpty()) {
  return of();
 } else if (range.equals(Range.all())) {
  return all();
 } else {
  return new ImmutableRangeSet<C>(ImmutableList.of(range));
 }
}

代码示例来源:origin: google/guava

public void testSpanSingleRange() {
 for (Range<Integer> range : RANGES) {
  RangeMap<Integer, Integer> rangeMap = TreeRangeMap.create();
  rangeMap.put(range, 1);
  try {
   assertEquals(range, rangeMap.span());
   assertFalse(range.isEmpty());
  } catch (NoSuchElementException e) {
   assertTrue(range.isEmpty());
  }
 }
}

代码示例来源:origin: google/guava

private void verify(Map<Integer, Integer> model, RangeMap<Integer, Integer> test) {
 for (int i = MIN_BOUND - 1; i <= MAX_BOUND + 1; i++) {
  assertEquals(model.get(i), test.get(i));
  Entry<Range<Integer>, Integer> entry = test.getEntry(i);
  assertEquals(model.containsKey(i), entry != null);
  if (entry != null) {
   assertTrue(test.asMapOfRanges().entrySet().contains(entry));
  }
 }
 for (Range<Integer> range : test.asMapOfRanges().keySet()) {
  assertFalse(range.isEmpty());
 }
}

代码示例来源:origin: google/guava

public void testAll() {
 Range<Integer> range = Range.all();
 assertTrue(range.contains(Integer.MIN_VALUE));
 assertTrue(range.contains(Integer.MAX_VALUE));
 assertUnboundedBelow(range);
 assertUnboundedAbove(range);
 assertFalse(range.isEmpty());
 assertEquals("(-\u221e..+\u221e)", range.toString());
 assertSame(range, reserializeAndAssert(range));
 assertSame(range, Range.all());
}

代码示例来源:origin: google/guava

public void testGreaterThan() {
 Range<Integer> range = Range.greaterThan(5);
 assertFalse(range.contains(5));
 assertTrue(range.contains(6));
 assertTrue(range.contains(Integer.MAX_VALUE));
 assertTrue(range.hasLowerBound());
 assertEquals(5, (int) range.lowerEndpoint());
 assertEquals(OPEN, range.lowerBoundType());
 assertUnboundedAbove(range);
 assertFalse(range.isEmpty());
 assertEquals("(5..+\u221e)", range.toString());
 reserializeAndAssert(range);
}

代码示例来源:origin: google/guava

public void testAtLeast() {
 Range<Integer> range = Range.atLeast(6);
 assertFalse(range.contains(5));
 assertTrue(range.contains(6));
 assertTrue(range.contains(Integer.MAX_VALUE));
 assertTrue(range.hasLowerBound());
 assertEquals(6, (int) range.lowerEndpoint());
 assertEquals(CLOSED, range.lowerBoundType());
 assertUnboundedAbove(range);
 assertFalse(range.isEmpty());
 assertEquals("[6..+\u221e)", range.toString());
 reserializeAndAssert(range);
}

代码示例来源:origin: google/guava

public void testAtMost() {
 Range<Integer> range = Range.atMost(4);
 assertTrue(range.contains(Integer.MIN_VALUE));
 assertTrue(range.contains(4));
 assertFalse(range.contains(5));
 assertUnboundedBelow(range);
 assertTrue(range.hasUpperBound());
 assertEquals(4, (int) range.upperEndpoint());
 assertEquals(CLOSED, range.upperBoundType());
 assertFalse(range.isEmpty());
 assertEquals("(-\u221e..4]", range.toString());
 reserializeAndAssert(range);
}

代码示例来源:origin: google/guava

public void testLessThan() {
 Range<Integer> range = Range.lessThan(5);
 assertTrue(range.contains(Integer.MIN_VALUE));
 assertTrue(range.contains(4));
 assertFalse(range.contains(5));
 assertUnboundedBelow(range);
 assertTrue(range.hasUpperBound());
 assertEquals(5, (int) range.upperEndpoint());
 assertEquals(OPEN, range.upperBoundType());
 assertFalse(range.isEmpty());
 assertEquals("(-\u221e..5)", range.toString());
 reserializeAndAssert(range);
}

代码示例来源:origin: google/guava

public void testSingleton() {
 Range<Integer> range = Range.closed(4, 4);
 assertFalse(range.contains(3));
 assertTrue(range.contains(4));
 assertFalse(range.contains(5));
 assertTrue(range.hasLowerBound());
 assertEquals(4, (int) range.lowerEndpoint());
 assertEquals(CLOSED, range.lowerBoundType());
 assertTrue(range.hasUpperBound());
 assertEquals(4, (int) range.upperEndpoint());
 assertEquals(CLOSED, range.upperBoundType());
 assertFalse(range.isEmpty());
 assertEquals("[4..4]", range.toString());
 reserializeAndAssert(range);
}

代码示例来源:origin: google/guava

public void testSpanTwoRanges() {
 for (Range<Integer> range1 : RANGES) {
  for (Range<Integer> range2 : RANGES) {
   if (!range1.isConnected(range2) || range1.intersection(range2).isEmpty()) {
    RangeMap<Integer, Integer> rangemap =
      ImmutableRangeMap.<Integer, Integer>builder().put(range1, 1).put(range2, 2).build();
    assertEquals(range1.span(range2), rangemap.span());
   }
  }
 }
}

代码示例来源:origin: google/guava

public void testEmpty1() {
 Range<Integer> range = Range.closedOpen(4, 4);
 assertFalse(range.contains(3));
 assertFalse(range.contains(4));
 assertFalse(range.contains(5));
 assertTrue(range.hasLowerBound());
 assertEquals(4, (int) range.lowerEndpoint());
 assertEquals(CLOSED, range.lowerBoundType());
 assertTrue(range.hasUpperBound());
 assertEquals(4, (int) range.upperEndpoint());
 assertEquals(OPEN, range.upperBoundType());
 assertTrue(range.isEmpty());
 assertEquals("[4..4)", range.toString());
 reserializeAndAssert(range);
}

代码示例来源:origin: google/guava

public void testClosed() {
 Range<Integer> range = Range.closed(5, 7);
 checkContains(range);
 assertTrue(range.hasLowerBound());
 assertEquals(5, (int) range.lowerEndpoint());
 assertEquals(CLOSED, range.lowerBoundType());
 assertTrue(range.hasUpperBound());
 assertEquals(7, (int) range.upperEndpoint());
 assertEquals(CLOSED, range.upperBoundType());
 assertFalse(range.isEmpty());
 assertEquals("[5..7]", range.toString());
 reserializeAndAssert(range);
}

代码示例来源:origin: google/guava

public void testOpen() {
 Range<Integer> range = Range.open(4, 8);
 checkContains(range);
 assertTrue(range.hasLowerBound());
 assertEquals(4, (int) range.lowerEndpoint());
 assertEquals(OPEN, range.lowerBoundType());
 assertTrue(range.hasUpperBound());
 assertEquals(8, (int) range.upperEndpoint());
 assertEquals(OPEN, range.upperBoundType());
 assertFalse(range.isEmpty());
 assertEquals("(4..8)", range.toString());
 reserializeAndAssert(range);
}

代码示例来源: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/guava

public void testOpenClosed() {
 Range<Integer> range = Range.openClosed(4, 7);
 checkContains(range);
 assertTrue(range.hasLowerBound());
 assertEquals(4, (int) range.lowerEndpoint());
 assertEquals(OPEN, range.lowerBoundType());
 assertTrue(range.hasUpperBound());
 assertEquals(7, (int) range.upperEndpoint());
 assertEquals(CLOSED, range.upperBoundType());
 assertFalse(range.isEmpty());
 assertEquals("(4..7]", range.toString());
 reserializeAndAssert(range);
}

相关文章