Skip to content

Commit

Permalink
Introducing floor(E) and ceiling(E) method in GWT emulated ImmutableS…
Browse files Browse the repository at this point in the history
…ortedSet package.

RELNOTES=`com.google.common.collect.ImmutableSortedSet`: Added `ceiling` and `floor` methods for GWT emulated ImmutableSortedSet

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=303898439
  • Loading branch information
ssmita authored and nick-someone committed Mar 31, 2020
1 parent 9f3bae5 commit 7e0fe90
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 4 deletions.
Expand Up @@ -1005,6 +1005,44 @@ public void testTailSetExclusive() {
}
}

public void testFloor_emptySet() {
ImmutableSortedSet<String> set = ImmutableSortedSet.copyOf(new String[] {});
assertThat(set.floor("f")).isNull();
}

public void testFloor_elementPresent() {
ImmutableSortedSet<String> set =
ImmutableSortedSet.copyOf(new String[] {"e", "a", "e", "f", "b", "i", "d", "a", "c", "k"});
assertThat(set.floor("f")).isEqualTo("f");
assertThat(set.floor("j")).isEqualTo("i");
assertThat(set.floor("q")).isEqualTo("k");
}

public void testFloor_elementAbsent() {
ImmutableSortedSet<String> set =
ImmutableSortedSet.copyOf(new String[] {"e", "e", "f", "b", "i", "d", "c", "k"});
assertThat(set.floor("a")).isNull();
}

public void testCeiling_emptySet() {
ImmutableSortedSet<String> set = ImmutableSortedSet.copyOf(new String[] {});
assertThat(set.ceiling("f")).isNull();
}

public void testCeiling_elementPresent() {
ImmutableSortedSet<String> set =
ImmutableSortedSet.copyOf(new String[] {"e", "e", "f", "f", "i", "d", "c", "k", "p", "c"});
assertThat(set.ceiling("f")).isEqualTo("f");
assertThat(set.ceiling("h")).isEqualTo("i");
assertThat(set.ceiling("a")).isEqualTo("c");
}

public void testCeiling_elementAbsent() {
ImmutableSortedSet<String> set =
ImmutableSortedSet.copyOf(new String[] {"e", "a", "e", "f", "b", "i", "d", "a", "c", "k"});
assertThat(set.ceiling("l")).isNull();
}

public void testSubSetExclusiveExclusive() {
String[] strings = NUMBER_NAMES.toArray(new String[0]);
ImmutableSortedSet<String> set = ImmutableSortedSet.copyOf(strings);
Expand Down
Expand Up @@ -618,14 +618,12 @@ public E lower(E e) {
}

/** @since 12.0 */
@GwtIncompatible // NavigableSet
@Override
public E floor(E e) {
return Iterators.getNext(headSet(e, true).descendingIterator(), null);
}

/** @since 12.0 */
@GwtIncompatible // NavigableSet
@Override
public E ceiling(E e) {
return Iterables.getFirst(tailSet(e, true), null);
Expand Down
Expand Up @@ -321,6 +321,16 @@ E higher(E e) {
return null;
}

public E ceiling(E e) {
ImmutableSortedSet<E> set = tailSet(e, true);
return !set.isEmpty() ? set.first() : null;
}

public E floor(E e) {
ImmutableSortedSet<E> set = headSet(e, true);
return !set.isEmpty() ? set.last() : null;
}

ImmutableSortedSet<E> headSet(E toElement, boolean inclusive) {
checkNotNull(toElement);
if (inclusive) {
Expand Down
Expand Up @@ -68,6 +68,21 @@ public void testBuilderWithNonDuplicateElements() throws Exception {
testCase.testBuilderWithNonDuplicateElements();
}

public void testCeiling_elementAbsent() throws Exception {
com.google.common.collect.ImmutableSortedSetTest testCase = new com.google.common.collect.ImmutableSortedSetTest();
testCase.testCeiling_elementAbsent();
}

public void testCeiling_elementPresent() throws Exception {
com.google.common.collect.ImmutableSortedSetTest testCase = new com.google.common.collect.ImmutableSortedSetTest();
testCase.testCeiling_elementPresent();
}

public void testCeiling_emptySet() throws Exception {
com.google.common.collect.ImmutableSortedSetTest testCase = new com.google.common.collect.ImmutableSortedSetTest();
testCase.testCeiling_emptySet();
}

public void testComplexBuilder() throws Exception {
com.google.common.collect.ImmutableSortedSetTest testCase = new com.google.common.collect.ImmutableSortedSetTest();
testCase.testComplexBuilder();
Expand Down Expand Up @@ -453,6 +468,21 @@ public void testExplicit_tailSet() throws Exception {
testCase.testExplicit_tailSet();
}

public void testFloor_elementAbsent() throws Exception {
com.google.common.collect.ImmutableSortedSetTest testCase = new com.google.common.collect.ImmutableSortedSetTest();
testCase.testFloor_elementAbsent();
}

public void testFloor_elementPresent() throws Exception {
com.google.common.collect.ImmutableSortedSetTest testCase = new com.google.common.collect.ImmutableSortedSetTest();
testCase.testFloor_elementPresent();
}

public void testFloor_emptySet() throws Exception {
com.google.common.collect.ImmutableSortedSetTest testCase = new com.google.common.collect.ImmutableSortedSetTest();
testCase.testFloor_emptySet();
}

public void testHeadSetExclusive() throws Exception {
com.google.common.collect.ImmutableSortedSetTest testCase = new com.google.common.collect.ImmutableSortedSetTest();
testCase.testHeadSetExclusive();
Expand Down
Expand Up @@ -1081,6 +1081,44 @@ public void testTailSetExclusive() {
}
}

public void testFloor_emptySet() {
ImmutableSortedSet<String> set = ImmutableSortedSet.copyOf(new String[] {});
assertThat(set.floor("f")).isNull();
}

public void testFloor_elementPresent() {
ImmutableSortedSet<String> set =
ImmutableSortedSet.copyOf(new String[] {"e", "a", "e", "f", "b", "i", "d", "a", "c", "k"});
assertThat(set.floor("f")).isEqualTo("f");
assertThat(set.floor("j")).isEqualTo("i");
assertThat(set.floor("q")).isEqualTo("k");
}

public void testFloor_elementAbsent() {
ImmutableSortedSet<String> set =
ImmutableSortedSet.copyOf(new String[] {"e", "e", "f", "b", "i", "d", "c", "k"});
assertThat(set.floor("a")).isNull();
}

public void testCeiling_emptySet() {
ImmutableSortedSet<String> set = ImmutableSortedSet.copyOf(new String[] {});
assertThat(set.ceiling("f")).isNull();
}

public void testCeiling_elementPresent() {
ImmutableSortedSet<String> set =
ImmutableSortedSet.copyOf(new String[] {"e", "e", "f", "f", "i", "d", "c", "k", "p", "c"});
assertThat(set.ceiling("f")).isEqualTo("f");
assertThat(set.ceiling("h")).isEqualTo("i");
assertThat(set.ceiling("a")).isEqualTo("c");
}

public void testCeiling_elementAbsent() {
ImmutableSortedSet<String> set =
ImmutableSortedSet.copyOf(new String[] {"e", "a", "e", "f", "b", "i", "d", "a", "c", "k"});
assertThat(set.ceiling("l")).isNull();
}

public void testSubSetExclusiveExclusive() {
String[] strings = NUMBER_NAMES.toArray(new String[0]);
ImmutableSortedSet<String> set = ImmutableSortedSet.copyOf(strings);
Expand Down
2 changes: 0 additions & 2 deletions guava/src/com/google/common/collect/ImmutableSortedSet.java
Expand Up @@ -693,14 +693,12 @@ public E lower(E e) {
}

/** @since 12.0 */
@GwtIncompatible // NavigableSet
@Override
public E floor(E e) {
return Iterators.getNext(headSet(e, true).descendingIterator(), null);
}

/** @since 12.0 */
@GwtIncompatible // NavigableSet
@Override
public E ceiling(E e) {
return Iterables.getFirst(tailSet(e, true), null);
Expand Down

0 comments on commit 7e0fe90

Please sign in to comment.