/** * Returns a nonempty contiguous set containing all {@code int} values from {@code lower} * (inclusive) to {@code upper} (inclusive). (These are the same values contained in {@code * Range.closed(lower, upper)}.) * * @throws IllegalArgumentException if {@code lower} is greater than {@code upper} * @since 23.0 */ @Beta public static ContiguousSet<Integer> closed(int lower, int upper) { return create(Range.closed(lower, upper), DiscreteDomain.integers()); }
/** * Returns a nonempty contiguous set containing all {@code long} values from {@code lower} * (inclusive) to {@code upper} (inclusive). (These are the same values contained in {@code * Range.closed(lower, upper)}.) * * @throws IllegalArgumentException if {@code lower} is greater than {@code upper} * @since 23.0 */ @Beta public static ContiguousSet<Long> closed(long lower, long upper) { return create(Range.closed(lower, upper), DiscreteDomain.longs()); }
/** * Returns a contiguous set containing all {@code int} values from {@code lower} (inclusive) to * {@code upper} (exclusive). If the endpoints are equal, an empty set is returned. (These are the * same values contained in {@code Range.closedOpen(lower, upper)}.) * * @throws IllegalArgumentException if {@code lower} is greater than {@code upper} * @since 23.0 */ @Beta public static ContiguousSet<Integer> closedOpen(int lower, int upper) { return create(Range.closedOpen(lower, upper), DiscreteDomain.integers()); }
/** * Returns a contiguous set containing all {@code long} values from {@code lower} (inclusive) to * {@code upper} (exclusive). If the endpoints are equal, an empty set is returned. (These are the * same values contained in {@code Range.closedOpen(lower, upper)}.) * * @throws IllegalArgumentException if {@code lower} is greater than {@code upper} * @since 23.0 */ @Beta public static ContiguousSet<Long> closedOpen(long lower, long upper) { return create(Range.closedOpen(lower, upper), DiscreteDomain.longs()); }
private ContiguousSet<C> intersectionInCurrentDomain(Range<C> other) { return range.isConnected(other) ? ContiguousSet.create(range.intersection(other), domain) : new EmptyContiguousSet<C>(domain); }
public void testCreate_noMin() { Range<Integer> range = Range.lessThan(0); try { ContiguousSet.create(range, RangeTest.UNBOUNDED_DOMAIN); fail(); } catch (IllegalArgumentException expected) { } }
public void testCreate_noMax() { Range<Integer> range = Range.greaterThan(0); try { ContiguousSet.create(range, RangeTest.UNBOUNDED_DOMAIN); fail(); } catch (IllegalArgumentException expected) { } }
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]))); }
public void testSubSet_tooLarge() { assertThat(ContiguousSet.create(Range.closed(1, 3), integers()).subSet(4, 6)).isEmpty(); }
public void testIntersection() { ContiguousSet<Integer> set = ContiguousSet.create(Range.closed(1, 3), integers()); assertEquals( ImmutableSet.of(1, 2, 3), ContiguousSet.create(Range.open(-1, 4), integers()).intersection(set)); assertEquals( ImmutableSet.of(1, 2, 3), set.intersection(ContiguousSet.create(Range.open(-1, 4), integers()))); assertEquals( ImmutableSet.of(3), set.intersection(ContiguousSet.create(Range.closed(3, 5), integers()))); }
public void testSubSet_outOfOrder() { ImmutableSortedSet<Integer> set = ContiguousSet.create(Range.closed(1, 3), integers()); try { set.subSet(3, 2); fail(); } catch (IllegalArgumentException expected) { } }
@SuppressWarnings("unchecked") // varargs! public void testCartesianProductTooBig() { Set<Integer> set = ContiguousSet.create(Range.closed(0, 10000), DiscreteDomain.integers()); try { Sets.cartesianProduct(set, set, set, set, set); fail("Expected IAE"); } catch (IllegalArgumentException expected) { } }
public void testCreate_empty() { assertEquals(ImmutableSet.of(), ContiguousSet.create(Range.closedOpen(1, 1), integers())); assertEquals(ImmutableSet.of(), ContiguousSet.closedOpen(1, 1)); assertEquals(ImmutableSet.of(), ContiguousSet.create(Range.openClosed(5, 5), integers())); assertEquals( ImmutableSet.of(), ContiguousSet.create(Range.lessThan(Integer.MIN_VALUE), integers())); assertEquals( ImmutableSet.of(), ContiguousSet.create(Range.greaterThan(Integer.MAX_VALUE), integers())); }
public void testFirst() { assertEquals(1, ContiguousSet.create(Range.closed(1, 3), integers()).first().intValue()); assertEquals(1, ContiguousSet.create(Range.open(0, 4), integers()).first().intValue()); assertEquals( Integer.MIN_VALUE, ContiguousSet.create(Range.<Integer>all(), integers()).first().intValue()); }
public void testLast() { assertEquals(3, ContiguousSet.create(Range.closed(1, 3), integers()).last().intValue()); assertEquals(3, ContiguousSet.create(Range.open(0, 4), integers()).last().intValue()); assertEquals( Integer.MAX_VALUE, ContiguousSet.create(Range.<Integer>all(), integers()).last().intValue()); }
public void testContainsAll() { ImmutableSortedSet<Integer> set = ContiguousSet.create(Range.closed(1, 3), integers()); for (Set<Integer> subset : Sets.powerSet(ImmutableSet.of(1, 2, 3))) { assertTrue(set.containsAll(subset)); } for (Set<Integer> subset : Sets.powerSet(ImmutableSet.of(1, 2, 3))) { assertFalse(set.containsAll(Sets.union(subset, ImmutableSet.of(9)))); } assertFalse(set.containsAll((Collection<?>) ImmutableSet.of("blah"))); }
public void testIntersection_empty() { ContiguousSet<Integer> set = ContiguousSet.closed(1, 3); ContiguousSet<Integer> emptySet = ContiguousSet.closedOpen(2, 2); assertEquals(ImmutableSet.of(), set.intersection(emptySet)); assertEquals(ImmutableSet.of(), emptySet.intersection(set)); assertEquals( ImmutableSet.of(), ContiguousSet.create(Range.closed(-5, -1), integers()) .intersection(ContiguousSet.create(Range.open(3, 64), integers()))); }
public void testRange_unboundedRange() { assertEquals( Range.closed(Integer.MIN_VALUE, Integer.MAX_VALUE), ContiguousSet.create(Range.<Integer>all(), integers()).range()); assertEquals( Range.atLeast(Integer.MIN_VALUE), ContiguousSet.create(Range.<Integer>all(), integers()).range(CLOSED, OPEN)); assertEquals( Range.all(), ContiguousSet.create(Range.<Integer>all(), integers()).range(OPEN, OPEN)); assertEquals( Range.atMost(Integer.MAX_VALUE), ContiguousSet.create(Range.<Integer>all(), integers()).range(OPEN, CLOSED)); }
public void testTailSet() { ImmutableSortedSet<Integer> set = ContiguousSet.create(Range.closed(1, 3), integers()); assertThat(set.tailSet(Integer.MIN_VALUE)).containsExactly(1, 2, 3).inOrder(); assertThat(set.tailSet(1)).containsExactly(1, 2, 3).inOrder(); assertThat(set.tailSet(2)).containsExactly(2, 3).inOrder(); assertThat(set.tailSet(3)).containsExactly(3).inOrder(); assertThat(set.tailSet(Integer.MIN_VALUE, false)).containsExactly(1, 2, 3).inOrder(); assertThat(set.tailSet(1, false)).containsExactly(2, 3).inOrder(); assertThat(set.tailSet(2, false)).containsExactly(3).inOrder(); assertThat(set.tailSet(3, false)).isEmpty(); }
public void testHeadSet() { ImmutableSortedSet<Integer> set = ContiguousSet.create(Range.closed(1, 3), integers()); assertThat(set.headSet(1)).isEmpty(); assertThat(set.headSet(2)).containsExactly(1).inOrder(); assertThat(set.headSet(3)).containsExactly(1, 2).inOrder(); assertThat(set.headSet(4)).containsExactly(1, 2, 3).inOrder(); assertThat(set.headSet(Integer.MAX_VALUE)).containsExactly(1, 2, 3).inOrder(); assertThat(set.headSet(1, true)).containsExactly(1).inOrder(); assertThat(set.headSet(2, true)).containsExactly(1, 2).inOrder(); assertThat(set.headSet(3, true)).containsExactly(1, 2, 3).inOrder(); assertThat(set.headSet(4, true)).containsExactly(1, 2, 3).inOrder(); assertThat(set.headSet(Integer.MAX_VALUE, true)).containsExactly(1, 2, 3).inOrder(); }