/** * {@inheritDoc} * * <p>This method returns a serializable {@code ImmutableSortedSet}. * * <p>The {@link SortedSet#subSet} documentation states that a subset of a subset throws an {@link * IllegalArgumentException} if passed a {@code fromElement} smaller than an earlier {@code * fromElement}. However, this method doesn't throw an exception in that situation, but instead * keeps the original {@code fromElement}. Similarly, this method keeps the original {@code * toElement}, instead of throwing an exception, if passed a {@code toElement} greater than an * earlier {@code toElement}. */ @Override public ImmutableSortedSet<E> subSet(E fromElement, E toElement) { return subSet(fromElement, true, toElement, false); }
@Override protected SortedSet<String> create(String[] elements) { List<String> list = Lists.newArrayList(elements); list.add("\0"); list.add("zzz"); return ImmutableSortedSet.copyOf(list).subSet("\0\0", "zzy"); } }
/** * {@inheritDoc} * * <p>This method returns a serializable {@code ImmutableSortedSet}. * * <p>The {@link SortedSet#subSet} documentation states that a subset of a subset throws an {@link * IllegalArgumentException} if passed a {@code fromElement} smaller than an earlier {@code * fromElement}. However, this method doesn't throw an exception in that situation, but instead * keeps the original {@code fromElement}. Similarly, this method keeps the original {@code * toElement}, instead of throwing an exception, if passed a {@code toElement} greater than an * earlier {@code toElement}. */ @Override public ImmutableSortedSet<E> subSet(E fromElement, E toElement) { return subSet(fromElement, true, toElement, false); }
@Override ImmutableSortedSet<E> subSetImpl( E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) { return forward.subSet(toElement, toInclusive, fromElement, fromInclusive).descendingSet(); }
@Override ImmutableSortedSet<E> subSetImpl( E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) { return forward.subSet(toElement, toInclusive, fromElement, fromInclusive).descendingSet(); }
@Override ImmutableSortedSet<E> subSetImpl( E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) { return forward.subSet(toElement, toInclusive, fromElement, fromInclusive).descendingSet(); }
public void testAsSetSubSet() { ImmutableSortedSet<Integer> expectedSet = ImmutableSortedSet.of(2, 3, 4, 8, 9, 16, 17); ImmutableSortedSet<Integer> asSet = RANGE_SET_ONE.asSet(DiscreteDomain.integers()); for (int i = 0; i <= 20; i++) { for (int j = i + 1; j <= 20; j++) { assertEquals(expectedSet.subSet(i, false, j, false), asSet.subSet(i, false, j, false)); assertEquals(expectedSet.subSet(i, true, j, false), asSet.subSet(i, true, j, false)); assertEquals(expectedSet.subSet(i, false, j, true), asSet.subSet(i, false, j, true)); assertEquals(expectedSet.subSet(i, true, j, true), asSet.subSet(i, true, j, true)); } } }
public void testSubSetExclusiveExclusive() { String[] strings = NUMBER_NAMES.toArray(new String[0]); ImmutableSortedSet<String> set = ImmutableSortedSet.copyOf(strings); Arrays.sort(strings); for (int i = 0; i < strings.length; i++) { for (int j = i; j < strings.length; j++) { assertThat(set.subSet(strings[i], false, strings[j], false)) .containsExactlyElementsIn(sortedNumberNames(Math.min(i + 1, j), j)) .inOrder(); } } }
public void testSubSet() { ImmutableSortedSet<Integer> set = ContiguousSet.create(Range.closed(1, 3), integers()); assertThat(set.subSet(1, 4)).containsExactly(1, 2, 3).inOrder(); assertThat(set.subSet(2, 4)).containsExactly(2, 3).inOrder(); assertThat(set.subSet(3, 4)).containsExactly(3).inOrder(); assertThat(set.subSet(3, 3)).isEmpty(); assertThat(set.subSet(2, 3)).containsExactly(2).inOrder(); assertThat(set.subSet(1, 3)).containsExactly(1, 2).inOrder(); assertThat(set.subSet(1, 2)).containsExactly(1).inOrder(); assertThat(set.subSet(2, 2)).isEmpty(); assertThat(set.subSet(Integer.MIN_VALUE, Integer.MAX_VALUE)).containsExactly(1, 2, 3).inOrder(); assertThat(set.subSet(1, true, 3, true)).containsExactly(1, 2, 3).inOrder(); assertThat(set.subSet(1, false, 3, true)).containsExactly(2, 3).inOrder(); assertThat(set.subSet(1, true, 3, false)).containsExactly(1, 2).inOrder(); assertThat(set.subSet(1, false, 3, false)).containsExactly(2).inOrder(); }
public void testSubSetInclusiveExclusive() { String[] strings = NUMBER_NAMES.toArray(new String[0]); ImmutableSortedSet<String> set = ImmutableSortedSet.copyOf(strings); Arrays.sort(strings); for (int i = 0; i < strings.length; i++) { for (int j = i; j < strings.length; j++) { assertThat(set.subSet(strings[i], true, strings[j], false)) .containsExactlyElementsIn(sortedNumberNames(i, j)) .inOrder(); } } }
public void testSubSetExclusiveInclusive() { String[] strings = NUMBER_NAMES.toArray(new String[0]); ImmutableSortedSet<String> set = ImmutableSortedSet.copyOf(strings); Arrays.sort(strings); for (int i = 0; i < strings.length; i++) { for (int j = i; j < strings.length; j++) { assertThat(set.subSet(strings[i], false, strings[j], true)) .containsExactlyElementsIn(sortedNumberNames(i + 1, j + 1)) .inOrder(); } } }
public void testSubSetInclusiveInclusive() { String[] strings = NUMBER_NAMES.toArray(new String[0]); ImmutableSortedSet<String> set = ImmutableSortedSet.copyOf(strings); Arrays.sort(strings); for (int i = 0; i < strings.length; i++) { for (int j = i; j < strings.length; j++) { assertThat(set.subSet(strings[i], true, strings[j], true)) .containsExactlyElementsIn(sortedNumberNames(i, j + 1)) .inOrder(); } } }
public void testSubSet_outOfOrder() { ImmutableSortedSet<Integer> set = ContiguousSet.create(Range.closed(1, 3), integers()); try { set.subSet(3, 2); fail(); } catch (IllegalArgumentException expected) { } }
public void testSubsetAsList() { ImmutableSet<String> set = ImmutableSortedSet.of("a", "e", "i", "o", "u").subSet("c", "r"); ImmutableList<String> list = set.asList(); assertEquals(ImmutableList.of("e", "i", "o"), list); assertEquals(list, ImmutableList.copyOf(set)); }
@Override protected List<String> create(String[] elements) { Comparator<String> comparator = createExplicitComparator(elements); ImmutableSortedSet.Builder<String> builder = ImmutableSortedSet.orderedBy(comparator); builder.add(BEFORE_FIRST); builder.add(elements); builder.add(AFTER_LAST); return builder.build().subSet(BEFORE_FIRST_2, AFTER_LAST).asList(); } }
@Override protected List<String> create(String[] elements) { Comparator<String> comparator = createExplicitComparator(elements); ImmutableSortedSet.Builder<String> builder = ImmutableSortedSet.orderedBy(comparator); builder.add(BEFORE_FIRST); builder.add(BEFORE_FIRST_2); builder.add(elements); builder.add(AFTER_LAST); builder.add(AFTER_LAST_2); return builder .build() .subSet(BEFORE_FIRST_2, AFTER_LAST_2) .asList() .subList(1, elements.length + 1); } }
@GwtIncompatible // SerializableTester, ImmutableSortedAsList public void testSubsetAsListReturnTypeAndSerialization() { ImmutableSet<String> set = ImmutableSortedSet.of("a", "e", "i", "o", "u").subSet("c", "r"); ImmutableList<String> list = set.asList(); assertTrue(list instanceof ImmutableSortedAsList); ImmutableList<String> copy = SerializableTester.reserializeAndAssert(list); assertTrue(copy instanceof ImmutableSortedAsList); }
@Override protected SortedSet<String> create(String[] elements) { List<String> list = Lists.newArrayList(elements); list.add("\0"); list.add("zzz"); return ImmutableSortedSet.copyOf(list).subSet("\0\0", "zzy"); } }
@Override ImmutableSortedSet<E> subSetImpl( E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) { return forward.subSet(toElement, toInclusive, fromElement, fromInclusive).descendingSet(); }
@Override protected List<String> create(String[] elements) { Comparator<String> comparator = createExplicitComparator(elements); ImmutableSortedSet.Builder<String> builder = ImmutableSortedSet.orderedBy(comparator); builder.add(BEFORE_FIRST); builder.add(elements); builder.add(AFTER_LAST); return builder.build().subSet(BEFORE_FIRST_2, AFTER_LAST).asList(); } }