Java Code Examples for java.util.NavigableSet#headSet()

The following examples show how to use java.util.NavigableSet#headSet() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConcurrentSkipListSubSetTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * headSet returns set with keys in requested range
 */
public void testDescendingHeadSetContents() {
    NavigableSet set = dset5();
    SortedSet sm = set.headSet(m4);
    assertTrue(sm.contains(m1));
    assertTrue(sm.contains(m2));
    assertTrue(sm.contains(m3));
    assertFalse(sm.contains(m4));
    assertFalse(sm.contains(m5));
    Iterator i = sm.iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(m1, k);
    k = (Integer)(i.next());
    assertEquals(m2, k);
    k = (Integer)(i.next());
    assertEquals(m3, k);
    assertFalse(i.hasNext());
    sm.clear();
    assertTrue(sm.isEmpty());
    assertEquals(2, set.size());
    assertEquals(m4, set.first());
}
 
Example 2
Source File: EmptyNavigableSet.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testheadSetRanges(String description, NavigableSet navigableSet) {
    NavigableSet subSet = navigableSet.headSet(BigInteger.ONE, true);

    // same subset
    subSet.headSet(BigInteger.ONE, true);

    // slightly smaller
    NavigableSet ns = subSet.headSet(BigInteger.ONE, false);

    // slight exapansion
    assertThrows(() -> {
        ns.headSet(BigInteger.ONE, true);
    },
        IllegalArgumentException.class,
        description + ": Expansion should not be allowed");

    // much smaller
    subSet.headSet(isDescending(subSet) ? BigInteger.TEN : BigInteger.ZERO, true);
}
 
Example 3
Source File: EmptyNavigableSet.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testheadSetRanges(String description, NavigableSet navigableSet) {
    NavigableSet subSet = navigableSet.headSet(BigInteger.ONE, true);

    // same subset
    subSet.headSet(BigInteger.ONE, true);

    // slightly smaller
    NavigableSet ns = subSet.headSet(BigInteger.ONE, false);

    // slight exapansion
    assertThrows(() -> {
        ns.headSet(BigInteger.ONE, true);
    },
        IllegalArgumentException.class,
        description + ": Expansion should not be allowed");

    // much smaller
    subSet.headSet(isDescending(subSet) ? BigInteger.TEN : BigInteger.ZERO, true);
}
 
Example 4
Source File: ConcurrentSkipListSubSetJUnitTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * headSet returns set with keys in requested range
 */
public void testDescendingHeadSetContents() {
    NavigableSet set = dset5();
    SortedSet sm = set.headSet(m4);
    assertTrue(sm.contains(m1));
    assertTrue(sm.contains(m2));
    assertTrue(sm.contains(m3));
    assertFalse(sm.contains(m4));
    assertFalse(sm.contains(m5));
    Iterator i = sm.iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(m1, k);
    k = (Integer)(i.next());
    assertEquals(m2, k);
    k = (Integer)(i.next());
    assertEquals(m3, k);
    assertFalse(i.hasNext());
    sm.clear();
    assertTrue(sm.isEmpty());
    assertEquals(2, set.size());
    assertEquals(m4, set.first());
}
 
Example 5
Source File: ConcurrentSkipListSubSetJUnitTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * headSet returns set with keys in requested range
 */
public void testHeadSetContents() {
    NavigableSet set = set5();
    SortedSet sm = set.headSet(four);
    assertTrue(sm.contains(one));
    assertTrue(sm.contains(two));
    assertTrue(sm.contains(three));
    assertFalse(sm.contains(four));
    assertFalse(sm.contains(five));
    Iterator i = sm.iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(one, k);
    k = (Integer)(i.next());
    assertEquals(two, k);
    k = (Integer)(i.next());
    assertEquals(three, k);
    assertFalse(i.hasNext());
    sm.clear();
    assertTrue(sm.isEmpty());
    assertEquals(2, set.size());
    assertEquals(four, set.first());
}
 
Example 6
Source File: Sets.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Returns a view of the portion of {@code set} whose elements are contained by {@code range}.
 *
 * <p>This method delegates to the appropriate methods of {@link NavigableSet} (namely
 * {@link NavigableSet#subSet(Object, boolean, Object, boolean) subSet()},
 * {@link NavigableSet#tailSet(Object, boolean) tailSet()}, and
 * {@link NavigableSet#headSet(Object, boolean) headSet()}) to actually construct the view.
 * Consult these methods for a full description of the returned view's behavior.
 *
 * <p><b>Warning:</b> {@code Range}s always represent a range of values using the values' natural
 * ordering. {@code NavigableSet} on the other hand can specify a custom ordering via a
 * {@link Comparator}, which can violate the natural ordering. Using this method (or in general
 * using {@code Range}) with unnaturally-ordered sets can lead to unexpected and undefined
 * behavior.
 *
 * @since 20.0
 */

@Beta
@GwtIncompatible // NavigableSet
public static <K extends Comparable<? super K>> NavigableSet<K> subSet(NavigableSet<K> set, Range<K> range) {
  if (set.comparator() != null && set.comparator() != Ordering.natural()
      && range.hasLowerBound()
      && range.hasUpperBound()) {
    checkArgument(set.comparator().compare(range.lowerEndpoint(), range.upperEndpoint()) <= 0, "set is using a custom comparator which is inconsistent with the natural ordering.");
  }
  if (range.hasLowerBound() && range.hasUpperBound()) {
    return set.subSet(
      range.lowerEndpoint(), range.lowerBoundType() == BoundType.CLOSED,
      range.upperEndpoint(), range.upperBoundType() == BoundType.CLOSED);
  } else if (range.hasLowerBound()) {
    return set.tailSet(range.lowerEndpoint(), range.lowerBoundType() == BoundType.CLOSED);
  } else if (range.hasUpperBound()) {
    return set.headSet(range.upperEndpoint(), range.upperBoundType() == BoundType.CLOSED);
  }
  return checkNotNull(set);
}
 
Example 7
Source File: EmptyNavigableSet.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testheadSetRanges(String description, NavigableSet navigableSet) {
    NavigableSet subSet = navigableSet.headSet(BigInteger.ONE, true);

    // same subset
    subSet.headSet(BigInteger.ONE, true);

    // slightly smaller
    NavigableSet ns = subSet.headSet(BigInteger.ONE, false);

    // slight exapansion
    assertThrows(() -> {
        ns.headSet(BigInteger.ONE, true);
    },
        IllegalArgumentException.class,
        description + ": Expansion should not be allowed");

    // much smaller
    subSet.headSet(isDescending(subSet) ? BigInteger.TEN : BigInteger.ZERO, true);
}
 
Example 8
Source File: EmptyNavigableSet.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testheadSetRanges(String description, NavigableSet navigableSet) {
    NavigableSet subSet = navigableSet.headSet(BigInteger.ONE, true);

    // same subset
    subSet.headSet(BigInteger.ONE, true);

    // slightly smaller
    NavigableSet ns = subSet.headSet(BigInteger.ONE, false);

    // slight exapansion
    assertThrows(() -> {
        ns.headSet(BigInteger.ONE, true);
    },
        IllegalArgumentException.class,
        description + ": Expansion should not be allowed");

    // much smaller
    subSet.headSet(isDescending(subSet) ? BigInteger.TEN : BigInteger.ZERO, true);
}
 
Example 9
Source File: BTreeSet2Test.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * headSet returns set with keys in requested range
 */
public void testHeadSetContents() {
    NavigableSet set = set5();
    SortedSet sm = set.headSet(four);
    assertTrue(sm.contains(one));
    assertTrue(sm.contains(two));
    assertTrue(sm.contains(three));
    assertFalse(sm.contains(four));
    assertFalse(sm.contains(five));
    Iterator i = sm.iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(one, k);
    k = (Integer)(i.next());
    assertEquals(two, k);
    k = (Integer)(i.next());
    assertEquals(three, k);
    assertFalse(i.hasNext());
    sm.clear();
    assertTrue(sm.isEmpty());
    assertEquals(2, set.size());
    assertEquals(four, set.first());
}
 
Example 10
Source File: EmptyNavigableSet.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testheadSetRanges(String description, NavigableSet navigableSet) {
    NavigableSet subSet = navigableSet.headSet(BigInteger.ONE, true);

    // same subset
    subSet.headSet(BigInteger.ONE, true);

    // slightly smaller
    NavigableSet ns = subSet.headSet(BigInteger.ONE, false);

    // slight exapansion
    assertThrows(() -> {
        ns.headSet(BigInteger.ONE, true);
    },
        IllegalArgumentException.class,
        description + ": Expansion should not be allowed");

    // much smaller
    subSet.headSet(isDescending(subSet) ? BigInteger.TEN : BigInteger.ZERO, true);
}
 
Example 11
Source File: Sets.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Returns a view of the portion of {@code set} whose elements are contained by {@code range}.
 *
 * <p>This method delegates to the appropriate methods of {@link NavigableSet} (namely
 * {@link NavigableSet#subSet(Object, boolean, Object, boolean) subSet()},
 * {@link NavigableSet#tailSet(Object, boolean) tailSet()}, and
 * {@link NavigableSet#headSet(Object, boolean) headSet()}) to actually construct the view.
 * Consult these methods for a full description of the returned view's behavior.
 *
 * <p><b>Warning:</b> {@code Range}s always represent a range of values using the values' natural
 * ordering. {@code NavigableSet} on the other hand can specify a custom ordering via a
 * {@link Comparator}, which can violate the natural ordering. Using this method (or in general
 * using {@code Range}) with unnaturally-ordered sets can lead to unexpected and undefined
 * behavior.
 *
 * @since 20.0
 */

@Beta
@GwtIncompatible // NavigableSet
public static <K extends Comparable<? super K>> NavigableSet<K> subSet(NavigableSet<K> set, Range<K> range) {
  if (set.comparator() != null && set.comparator() != Ordering.natural()
      && range.hasLowerBound()
      && range.hasUpperBound()) {
    checkArgument(set.comparator().compare(range.lowerEndpoint(), range.upperEndpoint()) <= 0, "set is using a custom comparator which is inconsistent with the natural ordering.");
  }
  if (range.hasLowerBound() && range.hasUpperBound()) {
    return set.subSet(
      range.lowerEndpoint(),
      range.lowerBoundType() == BoundType.CLOSED,
      range.upperEndpoint(),
      range.upperBoundType() == BoundType.CLOSED);
  } else if (range.hasLowerBound()) {
    return set.tailSet(range.lowerEndpoint(), range.lowerBoundType() == BoundType.CLOSED);
  } else if (range.hasUpperBound()) {
    return set.headSet(range.upperEndpoint(), range.upperBoundType() == BoundType.CLOSED);
  }
  return checkNotNull(set);
}
 
Example 12
Source File: ConcurrentSkipListSubSetJUnitTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * headSet returns set with keys in requested range
 */
public void testHeadSetContents() {
    NavigableSet set = set5();
    SortedSet sm = set.headSet(four);
    assertTrue(sm.contains(one));
    assertTrue(sm.contains(two));
    assertTrue(sm.contains(three));
    assertFalse(sm.contains(four));
    assertFalse(sm.contains(five));
    Iterator i = sm.iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(one, k);
    k = (Integer)(i.next());
    assertEquals(two, k);
    k = (Integer)(i.next());
    assertEquals(three, k);
    assertFalse(i.hasNext());
    sm.clear();
    assertTrue(sm.isEmpty());
    assertEquals(2, set.size());
    assertEquals(four, set.first());
}
 
Example 13
Source File: TreeSubSetTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * headSet returns set with keys in requested range
 */
public void testDescendingHeadSetContents() {
    NavigableSet set = dset5();
    SortedSet sm = set.headSet(m4);
    assertTrue(sm.contains(m1));
    assertTrue(sm.contains(m2));
    assertTrue(sm.contains(m3));
    assertFalse(sm.contains(m4));
    assertFalse(sm.contains(m5));
    Iterator i = sm.iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(m1, k);
    k = (Integer)(i.next());
    assertEquals(m2, k);
    k = (Integer)(i.next());
    assertEquals(m3, k);
    assertFalse(i.hasNext());
    sm.clear();
    assertTrue(sm.isEmpty());
    assertEquals(2, set.size());
    assertEquals(m4, set.first());
}
 
Example 14
Source File: Sets.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Returns a view of the portion of {@code set} whose elements are contained by {@code range}.
 *
 * <p>This method delegates to the appropriate methods of {@link NavigableSet} (namely
 * {@link NavigableSet#subSet(Object, boolean, Object, boolean) subSet()},
 * {@link NavigableSet#tailSet(Object, boolean) tailSet()}, and
 * {@link NavigableSet#headSet(Object, boolean) headSet()}) to actually construct the view.
 * Consult these methods for a full description of the returned view's behavior.
 *
 * <p><b>Warning:</b> {@code Range}s always represent a range of values using the values' natural
 * ordering. {@code NavigableSet} on the other hand can specify a custom ordering via a
 * {@link Comparator}, which can violate the natural ordering. Using this method (or in general
 * using {@code Range}) with unnaturally-ordered sets can lead to unexpected and undefined
 * behavior.
 *
 * @since 20.0
 */
@Beta
@GwtIncompatible // NavigableSet
public static <K extends Comparable<? super K>> NavigableSet<K> subSet(
    NavigableSet<K> set, Range<K> range) {
  if (set.comparator() != null
      && set.comparator() != Ordering.natural()
      && range.hasLowerBound()
      && range.hasUpperBound()) {
    checkArgument(
        set.comparator().compare(range.lowerEndpoint(), range.upperEndpoint()) <= 0,
        "set is using a custom comparator which is inconsistent with the natural ordering.");
  }
  if (range.hasLowerBound() && range.hasUpperBound()) {
    return set.subSet(
        range.lowerEndpoint(),
        range.lowerBoundType() == BoundType.CLOSED,
        range.upperEndpoint(),
        range.upperBoundType() == BoundType.CLOSED);
  } else if (range.hasLowerBound()) {
    return set.tailSet(range.lowerEndpoint(), range.lowerBoundType() == BoundType.CLOSED);
  } else if (range.hasUpperBound()) {
    return set.headSet(range.upperEndpoint(), range.upperBoundType() == BoundType.CLOSED);
  }
  return checkNotNull(set);
}
 
Example 15
Source File: EmptyNavigableSet.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testheadSetRanges(String description, NavigableSet navigableSet) {
    NavigableSet subSet = navigableSet.headSet(BigInteger.ONE, true);

    // same subset
    subSet.headSet(BigInteger.ONE, true);

    // slightly smaller
    NavigableSet ns = subSet.headSet(BigInteger.ONE, false);

    // slight expansion
    assertThrowsIAE(() -> {
        ns.headSet(BigInteger.ONE, true);
    },
        description + ": Expansion should not be allowed");

    // much smaller
    subSet.headSet(isDescending(subSet) ? BigInteger.TEN : BigInteger.ZERO, true);
}
 
Example 16
Source File: EmptyNavigableSet.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testheadSetRanges(String description, NavigableSet navigableSet) {
    NavigableSet subSet = navigableSet.headSet(BigInteger.ONE, true);

    // same subset
    subSet.headSet(BigInteger.ONE, true);

    // slightly smaller
    NavigableSet ns = subSet.headSet(BigInteger.ONE, false);

    // slight exapansion
    assertThrows(() -> {
        ns.headSet(BigInteger.ONE, true);
    },
        IllegalArgumentException.class,
        description + ": Expansion should not be allowed");

    // much smaller
    subSet.headSet(isDescending(subSet) ? BigInteger.TEN : BigInteger.ZERO, true);
}
 
Example 17
Source File: EmptyNavigableSet.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testheadSetRanges(String description, NavigableSet navigableSet) {
    NavigableSet subSet = navigableSet.headSet(BigInteger.ONE, true);

    // same subset
    subSet.headSet(BigInteger.ONE, true);

    // slightly smaller
    NavigableSet ns = subSet.headSet(BigInteger.ONE, false);

    // slight exapansion
    assertThrows(() -> {
        ns.headSet(BigInteger.ONE, true);
    },
        IllegalArgumentException.class,
        description + ": Expansion should not be allowed");

    // much smaller
    subSet.headSet(isDescending(subSet) ? BigInteger.TEN : BigInteger.ZERO, true);
}
 
Example 18
Source File: TreeSubSetTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * headSet returns set with keys in requested range
 */
public void testHeadSetContents() {
    NavigableSet set = set5();
    SortedSet sm = set.headSet(four);
    assertTrue(sm.contains(one));
    assertTrue(sm.contains(two));
    assertTrue(sm.contains(three));
    assertFalse(sm.contains(four));
    assertFalse(sm.contains(five));
    Iterator i = sm.iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(one, k);
    k = (Integer)(i.next());
    assertEquals(two, k);
    k = (Integer)(i.next());
    assertEquals(three, k);
    assertFalse(i.hasNext());
    sm.clear();
    assertTrue(sm.isEmpty());
    assertEquals(2, set.size());
    assertEquals(four, set.first());
}
 
Example 19
Source File: EmptyNavigableSet.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testheadSetRanges(String description, NavigableSet navigableSet) {
    NavigableSet subSet = navigableSet.headSet(BigInteger.ONE, true);

    // same subset
    subSet.headSet(BigInteger.ONE, true);

    // slightly smaller
    NavigableSet ns = subSet.headSet(BigInteger.ONE, false);

    // slight exapansion
    assertThrows(() -> {
        ns.headSet(BigInteger.ONE, true);
    },
        IllegalArgumentException.class,
        description + ": Expansion should not be allowed");

    // much smaller
    subSet.headSet(isDescending(subSet) ? BigInteger.TEN : BigInteger.ZERO, true);
}
 
Example 20
Source File: ConcurrentSkipListSubSetTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * headSet returns set with keys in requested range
 */
public void testHeadSetContents() {
    NavigableSet set = set5();
    SortedSet sm = set.headSet(four);
    assertTrue(sm.contains(one));
    assertTrue(sm.contains(two));
    assertTrue(sm.contains(three));
    assertFalse(sm.contains(four));
    assertFalse(sm.contains(five));
    Iterator i = sm.iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(one, k);
    k = (Integer)(i.next());
    assertEquals(two, k);
    k = (Integer)(i.next());
    assertEquals(three, k);
    assertFalse(i.hasNext());
    sm.clear();
    assertTrue(sm.isEmpty());
    assertEquals(2, set.size());
    assertEquals(four, set.first());
}