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

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

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

Range.isConnected介绍

[英]Returns true if there exists a (possibly empty) range which is #encloses by both this range and other.

For example,

  • [2, 4) and [5, 7) are not connected
  • [2, 4) and [3, 5) are connected, because both enclose [3, 4)
  • [2, 4) and [4, 6) are connected, because both enclose the empty range [4, 4)

Note that this range and other have a well-defined #span and #intersection (as a single, possibly-empty range) if and only if this method returns true.

The connectedness relation is both reflexive and symmetric, but does not form an Equivalence as it is not transitive.

Note that certain discrete ranges are not considered connected, even though there are no elements "between them." For example, [3, 5] is not considered connected to [6,. In these cases, it may be desirable for both input ranges to be preprocessed with #canonical(DiscreteDomain) before testing for connectedness.
[中]如果存在一个#由该范围和其他范围包围的范围(可能为空),则返回true。
例如
*[2,4]和[5,7]未连接
*[2,4]和[3,5]是连接的,因为它们都包含[3,4]
*[2,4)和[4,6]是连接的,因为它们都包含空范围[4,4]
请注意,当且仅当此方法返回true时,此范围和其他范围具有定义良好的#跨度和#交点(作为单个,可能为空的范围)。
连通性关系既是自反的又是对称的,但由于它不是传递的,所以不构成等价关系。
请注意,某些离散范围不被视为是连通的,即使“它们之间”没有元素例如,[3,5]被认为与[6]无关。在这些情况下,在测试连通性之前,可能需要使用#规范(离散域)对两个输入范围进行预处理。

代码示例

代码示例来源: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 {@code ImmutableRangeMap} containing the associations previously added to this
  * builder.
  *
  * @throws IllegalArgumentException if any two ranges inserted into this builder overlap
  */
 public ImmutableRangeMap<K, V> build() {
  Collections.sort(entries, Range.<K>rangeLexOrdering().onKeys());
  ImmutableList.Builder<Range<K>> rangesBuilder = new ImmutableList.Builder<>(entries.size());
  ImmutableList.Builder<V> valuesBuilder = new ImmutableList.Builder<V>(entries.size());
  for (int i = 0; i < entries.size(); i++) {
   Range<K> range = entries.get(i).getKey();
   if (i > 0) {
    Range<K> prevRange = entries.get(i - 1).getKey();
    if (range.isConnected(prevRange) && !range.intersection(prevRange).isEmpty()) {
     throw new IllegalArgumentException(
       "Overlapping ranges: range " + prevRange + " overlaps with entry " + range);
    }
   }
   rangesBuilder.add(range);
   valuesBuilder.add(entries.get(i).getValue());
  }
  return new ImmutableRangeMap<>(rangesBuilder.build(), valuesBuilder.build());
 }
}

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

/** Returns the range that spans the given range and entry, if the entry can be coalesced. */
private static <K extends Comparable, V> Range<K> coalesce(
  Range<K> range, V value, @Nullable Entry<Cut<K>, RangeMapEntry<K, V>> entry) {
 if (entry != null
   && entry.getValue().getKey().isConnected(range)
   && entry.getValue().getValue().equals(value)) {
  return range.span(entry.getValue().getKey());
 }
 return range;
}

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

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

@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(upperBoundWindow)) {
  return new RangesByUpperBound<C>(rangesByLowerBound, window.intersection(upperBoundWindow));
 } else {
  return ImmutableSortedMap.of();
 }
}

代码示例来源: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 testIsConnected() {
 assertTrue(Range.closed(3, 5).isConnected(Range.open(5, 6)));
 assertTrue(Range.closed(3, 5).isConnected(Range.openClosed(5, 5)));
 assertTrue(Range.open(3, 5).isConnected(Range.closed(5, 6)));
 assertTrue(Range.closed(3, 7).isConnected(Range.open(6, 8)));
 assertTrue(Range.open(3, 7).isConnected(Range.closed(5, 6)));
 assertFalse(Range.closed(3, 5).isConnected(Range.closed(7, 8)));
 assertFalse(Range.closed(3, 5).isConnected(Range.closedOpen(7, 7)));
}

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

/** Returns a view of the intersection of this range set with the given range. */
@Override
public ImmutableRangeSet<C> subRangeSet(Range<C> range) {
 if (!isEmpty()) {
  Range<C> span = span();
  if (range.encloses(span)) {
   return this;
  } else if (range.isConnected(span)) {
   return new ImmutableRangeSet<C>(intersectRanges(range));
  }
 }
 return of();
}

代码示例来源: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 testSubRangeMap() {
 for (Range<Integer> range1 : RANGES) {
  for (Range<Integer> range2 : RANGES) {
   if (!range1.isConnected(range2) || range1.intersection(range2).isEmpty()) {
    for (Range<Integer> subRange : RANGES) {
     ImmutableRangeMap<Integer, Integer> rangeMap =
       ImmutableRangeMap.<Integer, Integer>builder().put(range1, 1).put(range2, 2).build();
     ImmutableRangeMap.Builder<Integer, Integer> expectedBuilder =
       ImmutableRangeMap.builder();
     for (Entry<Range<Integer>, Integer> entry : rangeMap.asMapOfRanges().entrySet()) {
      if (entry.getKey().isConnected(subRange)
        && !entry.getKey().intersection(subRange).isEmpty()) {
       expectedBuilder.put(entry.getKey().intersection(subRange), entry.getValue());
      }
     }
     ImmutableRangeMap<Integer, Integer> expected = expectedBuilder.build();
     assertEquals(expected, rangeMap.subRangeMap(subRange));
    }
   }
  }
 }
}

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

@AndroidIncompatible // slow
public void testAsMapOfRanges() {
 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();
    ImmutableMap<Range<Integer>, Integer> expectedAsMap =
      ImmutableMap.of(range1, 1, range2, 2);
    ImmutableMap<Range<Integer>, Integer> asMap = rangeMap.asMapOfRanges();
    ImmutableMap<Range<Integer>, Integer> descendingMap = rangeMap.asDescendingMapOfRanges();
    assertEquals(expectedAsMap, asMap);
    assertEquals(expectedAsMap, descendingMap);
    SerializableTester.reserializeAndAssert(asMap);
    SerializableTester.reserializeAndAssert(descendingMap);
    assertEquals(
      ImmutableList.copyOf(asMap.entrySet()).reverse(),
      ImmutableList.copyOf(descendingMap.entrySet()));
    for (Range<Integer> query : RANGES) {
     assertEquals(expectedAsMap.get(query), asMap.get(query));
    }
   }
  }
 }
}

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

private static void doPairTest(Range<Integer> a, Range<Integer> b) {
 TreeRangeSet<Integer> rangeSet = TreeRangeSet.create();
 rangeSet.add(a);
 rangeSet.add(b);
 if (a.isEmpty() && b.isEmpty()) {
  assertThat(rangeSet.asRanges()).isEmpty();
 } else if (a.isEmpty()) {
  assertThat(rangeSet.asRanges()).contains(b);
 } else if (b.isEmpty()) {
  assertThat(rangeSet.asRanges()).contains(a);
 } else if (a.isConnected(b)) {
  assertThat(rangeSet.asRanges()).containsExactly(a.span(b));
 } else {
  if (a.lowerEndpoint() < b.lowerEndpoint()) {
   assertThat(rangeSet.asRanges()).containsExactly(a, b).inOrder();
  } else {
   assertThat(rangeSet.asRanges()).containsExactly(b, a).inOrder();
  }
 }
}

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

相关文章