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

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

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

Range.intersection介绍

[英]Returns the maximal range #encloses by both this range and connectedRange, if such a range exists.

For example, the intersection of [1..5] and (3..7) is (3..5]. The resulting range may be empty; for example, [1..5) intersected with [5..7)yields the empty range [5..5).

The intersection exists if and only if the two ranges are #isConnected.

The intersection operation is commutative, associative and idempotent, and its identity element is Range#all).
[中]返回此范围和connectedRange(如果存在)所包围的最大范围。
例如,[1..5]和(3..7)的交集是(3..5)。结果范围可能是空的;例如,[1..5]与[5..7)交集产生空范围[5..5]。
当且仅当两个范围#断开时,相交才存在。
交集运算是交换的、结合的、幂等的,其单位元是Range#all)。

代码示例

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

@Override
public Range<C> get(int index) {
 checkElementIndex(index, length);
 if (index == 0 || index == length - 1) {
  return ranges.get(index + fromIndex).intersection(range);
 } else {
  return ranges.get(index + fromIndex);
 }
}

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

@Override
public Range<K> get(int index) {
 checkElementIndex(index, len);
 if (index == 0 || index == len - 1) {
  return ranges.get(index + off).intersection(range);
 } else {
  return ranges.get(index + off);
 }
}

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

@Override
 protected Entry<Cut<C>, Range<C>> computeNext() {
  if (!completeRangeItr.hasNext()) {
   return endOfData();
  }
  Range<C> nextRange = completeRangeItr.next();
  if (upperBoundOnLowerBounds.isLessThan(nextRange.lowerBound)) {
   return endOfData();
  } else {
   nextRange = nextRange.intersection(restriction);
   return Maps.immutableEntry(nextRange.lowerBound, nextRange);
  }
 }
};

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

@Override
public boolean intersects(Range<C> otherRange) {
 int ceilingIndex =
   SortedLists.binarySearch(
     ranges,
     Range.<C>lowerBoundFn(),
     otherRange.lowerBound,
     Ordering.natural(),
     ANY_PRESENT,
     NEXT_HIGHER);
 if (ceilingIndex < ranges.size()
   && ranges.get(ceilingIndex).isConnected(otherRange)
   && !ranges.get(ceilingIndex).intersection(otherRange).isEmpty()) {
  return true;
 }
 return ceilingIndex > 0
   && ranges.get(ceilingIndex - 1).isConnected(otherRange)
   && !ranges.get(ceilingIndex - 1).intersection(otherRange).isEmpty();
}

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

@Override
 protected Entry<Cut<C>, Range<C>> computeNext() {
  if (!completeRangeItr.hasNext()) {
   return endOfData();
  }
  Range<C> nextRange = completeRangeItr.next();
  if (restriction.lowerBound.compareTo(nextRange.upperBound) >= 0) {
   return endOfData();
  }
  nextRange = nextRange.intersection(restriction);
  if (lowerBoundWindow.contains(nextRange.lowerBound)) {
   return Maps.immutableEntry(nextRange.lowerBound, nextRange);
  } else {
   return endOfData();
  }
 }
};

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

private NavigableMap<Cut<C>, Range<C>> subMap(Range<Cut<C>> subWindow) {
 if (!complementLowerBoundWindow.isConnected(subWindow)) {
  return ImmutableSortedMap.of();
 } else {
  subWindow = subWindow.intersection(complementLowerBoundWindow);
  return new ComplementRangesByLowerBound<C>(positiveRangesByLowerBound, subWindow);
 }
}

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

private NavigableMap<Cut<C>, Range<C>> subMap(Range<Cut<C>> window) {
 if (window.isConnected(upperBoundWindow)) {
  return new RangesByUpperBound<C>(rangesByLowerBound, window.intersection(upperBoundWindow));
 } else {
  return ImmutableSortedMap.of();
 }
}

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

@Override
 public ImmutableRangeMap<K, V> subRangeMap(Range<K> subRange) {
  if (range.isConnected(subRange)) {
   return outer.subRangeMap(subRange.intersection(range));
  } else {
   return ImmutableRangeMap.of();
  }
 }
};

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

private ContiguousSet<C> intersectionInCurrentDomain(Range<C> other) {
 return range.isConnected(other)
   ? ContiguousSet.create(range.intersection(other), domain)
   : new EmptyContiguousSet<C>(domain);
}

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

private NavigableMap<Cut<C>, Range<C>> subMap(Range<Cut<C>> window) {
 if (!window.isConnected(lowerBoundWindow)) {
  return ImmutableSortedMap.of();
 } else {
  return new SubRangeSetRangesByLowerBound<C>(
    lowerBoundWindow.intersection(window), restriction, rangesByLowerBound);
 }
}

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

public void testIntersection_empty() {
 Range<Integer> range = Range.closedOpen(3, 3);
 assertEquals(range, range.intersection(range));
 try {
  range.intersection(Range.open(3, 5));
  fail();
 } catch (IllegalArgumentException expected) {
 }
 try {
  range.intersection(Range.closed(0, 2));
  fail();
 } catch (IllegalArgumentException expected) {
 }
}

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

public void testIntersection_singleton() {
 Range<Integer> range = Range.closed(3, 3);
 assertEquals(range, range.intersection(range));
 assertEquals(range, range.intersection(Range.atMost(4)));
 assertEquals(range, range.intersection(Range.atMost(3)));
 assertEquals(range, range.intersection(Range.atLeast(3)));
 assertEquals(range, range.intersection(Range.atLeast(2)));
 assertEquals(Range.closedOpen(3, 3), range.intersection(Range.lessThan(3)));
 assertEquals(Range.openClosed(3, 3), range.intersection(Range.greaterThan(3)));
 try {
  range.intersection(Range.atLeast(4));
  fail();
 } catch (IllegalArgumentException expected) {
 }
 try {
  range.intersection(Range.atMost(2));
  fail();
 } catch (IllegalArgumentException expected) {
 }
}

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

public void testIntersection_deFactoEmpty() {
 Range<Integer> range = Range.open(3, 4);
 assertEquals(range, range.intersection(range));
 assertEquals(Range.openClosed(3, 3), range.intersection(Range.atMost(3)));
 assertEquals(Range.closedOpen(4, 4), range.intersection(Range.atLeast(4)));
 try {
  range.intersection(Range.lessThan(3));
  fail();
 } catch (IllegalArgumentException expected) {
 }
 try {
  range.intersection(Range.greaterThan(4));
  fail();
 } catch (IllegalArgumentException expected) {
 }
 range = Range.closed(3, 4);
 assertEquals(Range.openClosed(4, 4), range.intersection(Range.greaterThan(4)));
}

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

private RangeSet<Integer> expectedSubRangeSet(
  RangeSet<Integer> rangeSet, Range<Integer> subRange) {
 RangeSet<Integer> expected = TreeRangeSet.create();
 for (Range<Integer> range : rangeSet.asRanges()) {
  if (range.isConnected(subRange)) {
   expected.add(range.intersection(subRange));
  }
 }
 return expected;
}

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

public void testGetEntry() {
 for (Range<Integer> range1 : RANGES) {
  for (Range<Integer> range2 : RANGES) {
   if (!range1.isConnected(range2) || range1.intersection(range2).isEmpty()) {
    ImmutableRangeMap<Integer, Integer> rangeMap =
      ImmutableRangeMap.<Integer, Integer>builder().put(range1, 1).put(range2, 2).build();
    for (int i = MIN_BOUND; i <= MAX_BOUND; i++) {
     Entry<Range<Integer>, Integer> expectedEntry = null;
     if (range1.contains(i)) {
      expectedEntry = Maps.immutableEntry(range1, 1);
     } else if (range2.contains(i)) {
      expectedEntry = Maps.immutableEntry(range2, 2);
     }
     assertEquals(expectedEntry, rangeMap.getEntry(i));
    }
   }
  }
 }
}

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

public void testGet() {
 for (Range<Integer> range1 : RANGES) {
  for (Range<Integer> range2 : RANGES) {
   if (!range1.isConnected(range2) || range1.intersection(range2).isEmpty()) {
    ImmutableRangeMap<Integer, Integer> rangeMap =
      ImmutableRangeMap.<Integer, Integer>builder().put(range1, 1).put(range2, 2).build();
    for (int i = MIN_BOUND; i <= MAX_BOUND; i++) {
     Integer expectedValue = null;
     if (range1.contains(i)) {
      expectedValue = 1;
     } else if (range2.contains(i)) {
      expectedValue = 2;
     }
     assertEquals(expectedValue, rangeMap.get(i));
    }
   }
  }
 }
}

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

public void testIntersects(RangeSet<Integer> rangeSet) {
 for (Range<Integer> query : QUERY_RANGES) {
  boolean expectIntersect = false;
  for (Range<Integer> expectedRange : rangeSet.asRanges()) {
   if (expectedRange.isConnected(query) && !expectedRange.intersection(query).isEmpty()) {
    expectIntersect = true;
    break;
   }
  }
  assertEquals(
    rangeSet + " was incorrect on intersects(" + query + ")",
    expectIntersect,
    rangeSet.intersects(query));
 }
}

代码示例来源: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 testOverlapRejection() {
 for (Range<Integer> range1 : RANGES) {
  for (Range<Integer> range2 : RANGES) {
   boolean expectRejection =
     range1.isConnected(range2) && !range1.intersection(range2).isEmpty();
   ImmutableRangeMap.Builder<Integer, Integer> builder = ImmutableRangeMap.builder();
   builder.put(range1, 1).put(range2, 2);
   try {
    ImmutableRangeMap<Integer, Integer> unused = builder.build();
    assertFalse(expectRejection);
   } catch (IllegalArgumentException e) {
    assertTrue(expectRejection);
   }
  }
 }
}

相关文章