Refine search
@Override public ContiguousSet<C> intersection(ContiguousSet<C> other) { checkNotNull(other); checkArgument(this.domain.equals(other.domain)); if (other.isEmpty()) { return other; } else { C lowerEndpoint = Ordering.natural().max(this.first(), other.first()); C upperEndpoint = Ordering.natural().min(this.last(), other.last()); return (lowerEndpoint.compareTo(upperEndpoint) <= 0) ? ContiguousSet.create(Range.closed(lowerEndpoint, upperEndpoint), domain) : new EmptyContiguousSet<C>(domain); } }
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)); }
/** @since 12.0 */ @GwtIncompatible // NavigableSet @Override public ContiguousSet<C> subSet( C fromElement, boolean fromInclusive, C toElement, boolean toInclusive) { checkNotNull(fromElement); checkNotNull(toElement); checkArgument(comparator().compare(fromElement, toElement) <= 0); return subSetImpl(fromElement, fromInclusive, toElement, toInclusive); }
@Override public ContiguousSet<C> subSet(C fromElement, C toElement) { checkNotNull(fromElement); checkNotNull(toElement); checkArgument(comparator().compare(fromElement, toElement) <= 0); return subSetImpl(fromElement, true, toElement, false); }
/** * 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()); }
public long size() { long size = 0L; for (Range<Long> range : rangeSet.asRanges()) { range = ContiguousSet.create(range, DiscreteDomain.longs()).range(); size += (range.upperEndpoint() - range.lowerEndpoint() + 1); } return size; }
@GwtIncompatible // SerializableTester public void testSerialization() { ContiguousSet<Integer> empty = ContiguousSet.create(Range.closedOpen(1, 1), integers()); assertTrue(empty instanceof EmptyContiguousSet); reserializeAndAssert(empty); ContiguousSet<Integer> regular = ContiguousSet.create(Range.closed(1, 3), integers()); assertTrue(regular instanceof RegularContiguousSet); reserializeAndAssert(regular); /* * Make sure that we're using RegularContiguousSet.SerializedForm and not * ImmutableSet.SerializedForm, which would be enormous. */ ContiguousSet<Integer> enormous = ContiguousSet.create(Range.<Integer>all(), integers()); assertTrue(enormous instanceof RegularContiguousSet); // We can't use reserializeAndAssert because it calls hashCode, which is enormously slow. ContiguousSet<Integer> enormousReserialized = reserialize(enormous); assertEquals(enormous, enormousReserialized); }
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 testRange() { assertEquals(Range.closed(1, 3), ContiguousSet.create(Range.closed(1, 3), integers()).range()); assertEquals(Range.closed(1, 3), ContiguousSet.closed(1, 3).range()); assertEquals( Range.closed(1, 3), ContiguousSet.create(Range.closedOpen(1, 4), integers()).range()); assertEquals(Range.closed(1, 3), ContiguousSet.closedOpen(1, 4).range()); assertEquals(Range.closed(1, 3), ContiguousSet.create(Range.open(0, 4), integers()).range()); assertEquals( Range.closed(1, 3), ContiguousSet.create(Range.openClosed(0, 3), integers()).range()); ContiguousSet.create(Range.closed(1, 3), integers()).range(OPEN, CLOSED)); ContiguousSet.create(Range.closedOpen(1, 4), integers()).range(OPEN, CLOSED)); ContiguousSet.create(Range.open(0, 4), integers()).range(OPEN, CLOSED)); ContiguousSet.create(Range.openClosed(0, 3), integers()).range(OPEN, CLOSED)); Range.open(0, 4), ContiguousSet.create(Range.closed(1, 3), integers()).range(OPEN, OPEN)); ContiguousSet.create(Range.closedOpen(1, 4), integers()).range(OPEN, OPEN)); assertEquals( Range.open(0, 4), ContiguousSet.create(Range.open(0, 4), integers()).range(OPEN, OPEN)); ContiguousSet.create(Range.openClosed(0, 3), integers()).range(OPEN, OPEN)); ContiguousSet.create(Range.closed(1, 3), integers()).range(CLOSED, OPEN)); ContiguousSet.create(Range.closedOpen(1, 4), integers()).range(CLOSED, OPEN));
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 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()))); }
/** * 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()); }
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 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())); }
/** * 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()); }
protected final ContiguousSet<Integer> checkedCreate(SortedSet<Integer> elementsSet) { List<Integer> elements = newArrayList(elementsSet); /* * A ContiguousSet can't have holes. If a test demands a hole, it should be changed so that it * doesn't need one, or it should be suppressed for ContiguousSet. */ for (int i = 0; i < elements.size() - 1; i++) { assertEquals(elements.get(i) + 1, (int) elements.get(i + 1)); } Range<Integer> range = elements.isEmpty() ? Range.closedOpen(0, 0) : Range.encloseAll(elements); return ContiguousSet.create(range, 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()); }
/** @since 12.0 */ @GwtIncompatible // NavigableSet @Override public ContiguousSet<C> headSet(C toElement, boolean inclusive) { return headSetImpl(checkNotNull(toElement), inclusive); }
private ContiguousSet<C> intersectionInCurrentDomain(Range<C> other) { return range.isConnected(other) ? ContiguousSet.create(range.intersection(other), domain) : new EmptyContiguousSet<C>(domain); }