Skip to content

Commit 7e0fe90

Browse files
ssmitanick-someone
authored andcommitted
Introducing floor(E) and ceiling(E) method in GWT emulated ImmutableSortedSet 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
1 parent 9f3bae5 commit 7e0fe90

File tree

6 files changed

+116
-4
lines changed

6 files changed

+116
-4
lines changed

android/guava-tests/test/com/google/common/collect/ImmutableSortedSetTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,44 @@ public void testTailSetExclusive() {
10051005
}
10061006
}
10071007

1008+
public void testFloor_emptySet() {
1009+
ImmutableSortedSet<String> set = ImmutableSortedSet.copyOf(new String[] {});
1010+
assertThat(set.floor("f")).isNull();
1011+
}
1012+
1013+
public void testFloor_elementPresent() {
1014+
ImmutableSortedSet<String> set =
1015+
ImmutableSortedSet.copyOf(new String[] {"e", "a", "e", "f", "b", "i", "d", "a", "c", "k"});
1016+
assertThat(set.floor("f")).isEqualTo("f");
1017+
assertThat(set.floor("j")).isEqualTo("i");
1018+
assertThat(set.floor("q")).isEqualTo("k");
1019+
}
1020+
1021+
public void testFloor_elementAbsent() {
1022+
ImmutableSortedSet<String> set =
1023+
ImmutableSortedSet.copyOf(new String[] {"e", "e", "f", "b", "i", "d", "c", "k"});
1024+
assertThat(set.floor("a")).isNull();
1025+
}
1026+
1027+
public void testCeiling_emptySet() {
1028+
ImmutableSortedSet<String> set = ImmutableSortedSet.copyOf(new String[] {});
1029+
assertThat(set.ceiling("f")).isNull();
1030+
}
1031+
1032+
public void testCeiling_elementPresent() {
1033+
ImmutableSortedSet<String> set =
1034+
ImmutableSortedSet.copyOf(new String[] {"e", "e", "f", "f", "i", "d", "c", "k", "p", "c"});
1035+
assertThat(set.ceiling("f")).isEqualTo("f");
1036+
assertThat(set.ceiling("h")).isEqualTo("i");
1037+
assertThat(set.ceiling("a")).isEqualTo("c");
1038+
}
1039+
1040+
public void testCeiling_elementAbsent() {
1041+
ImmutableSortedSet<String> set =
1042+
ImmutableSortedSet.copyOf(new String[] {"e", "a", "e", "f", "b", "i", "d", "a", "c", "k"});
1043+
assertThat(set.ceiling("l")).isNull();
1044+
}
1045+
10081046
public void testSubSetExclusiveExclusive() {
10091047
String[] strings = NUMBER_NAMES.toArray(new String[0]);
10101048
ImmutableSortedSet<String> set = ImmutableSortedSet.copyOf(strings);

android/guava/src/com/google/common/collect/ImmutableSortedSet.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,14 +618,12 @@ public E lower(E e) {
618618
}
619619

620620
/** @since 12.0 */
621-
@GwtIncompatible // NavigableSet
622621
@Override
623622
public E floor(E e) {
624623
return Iterators.getNext(headSet(e, true).descendingIterator(), null);
625624
}
626625

627626
/** @since 12.0 */
628-
@GwtIncompatible // NavigableSet
629627
@Override
630628
public E ceiling(E e) {
631629
return Iterables.getFirst(tailSet(e, true), null);

guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ImmutableSortedSet.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,16 @@ E higher(E e) {
321321
return null;
322322
}
323323

324+
public E ceiling(E e) {
325+
ImmutableSortedSet<E> set = tailSet(e, true);
326+
return !set.isEmpty() ? set.first() : null;
327+
}
328+
329+
public E floor(E e) {
330+
ImmutableSortedSet<E> set = headSet(e, true);
331+
return !set.isEmpty() ? set.last() : null;
332+
}
333+
324334
ImmutableSortedSet<E> headSet(E toElement, boolean inclusive) {
325335
checkNotNull(toElement);
326336
if (inclusive) {

guava-gwt/test/com/google/common/collect/ImmutableSortedSetTest_gwt.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,21 @@ public void testBuilderWithNonDuplicateElements() throws Exception {
6868
testCase.testBuilderWithNonDuplicateElements();
6969
}
7070

71+
public void testCeiling_elementAbsent() throws Exception {
72+
com.google.common.collect.ImmutableSortedSetTest testCase = new com.google.common.collect.ImmutableSortedSetTest();
73+
testCase.testCeiling_elementAbsent();
74+
}
75+
76+
public void testCeiling_elementPresent() throws Exception {
77+
com.google.common.collect.ImmutableSortedSetTest testCase = new com.google.common.collect.ImmutableSortedSetTest();
78+
testCase.testCeiling_elementPresent();
79+
}
80+
81+
public void testCeiling_emptySet() throws Exception {
82+
com.google.common.collect.ImmutableSortedSetTest testCase = new com.google.common.collect.ImmutableSortedSetTest();
83+
testCase.testCeiling_emptySet();
84+
}
85+
7186
public void testComplexBuilder() throws Exception {
7287
com.google.common.collect.ImmutableSortedSetTest testCase = new com.google.common.collect.ImmutableSortedSetTest();
7388
testCase.testComplexBuilder();
@@ -453,6 +468,21 @@ public void testExplicit_tailSet() throws Exception {
453468
testCase.testExplicit_tailSet();
454469
}
455470

471+
public void testFloor_elementAbsent() throws Exception {
472+
com.google.common.collect.ImmutableSortedSetTest testCase = new com.google.common.collect.ImmutableSortedSetTest();
473+
testCase.testFloor_elementAbsent();
474+
}
475+
476+
public void testFloor_elementPresent() throws Exception {
477+
com.google.common.collect.ImmutableSortedSetTest testCase = new com.google.common.collect.ImmutableSortedSetTest();
478+
testCase.testFloor_elementPresent();
479+
}
480+
481+
public void testFloor_emptySet() throws Exception {
482+
com.google.common.collect.ImmutableSortedSetTest testCase = new com.google.common.collect.ImmutableSortedSetTest();
483+
testCase.testFloor_emptySet();
484+
}
485+
456486
public void testHeadSetExclusive() throws Exception {
457487
com.google.common.collect.ImmutableSortedSetTest testCase = new com.google.common.collect.ImmutableSortedSetTest();
458488
testCase.testHeadSetExclusive();

guava-tests/test/com/google/common/collect/ImmutableSortedSetTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,44 @@ public void testTailSetExclusive() {
10811081
}
10821082
}
10831083

1084+
public void testFloor_emptySet() {
1085+
ImmutableSortedSet<String> set = ImmutableSortedSet.copyOf(new String[] {});
1086+
assertThat(set.floor("f")).isNull();
1087+
}
1088+
1089+
public void testFloor_elementPresent() {
1090+
ImmutableSortedSet<String> set =
1091+
ImmutableSortedSet.copyOf(new String[] {"e", "a", "e", "f", "b", "i", "d", "a", "c", "k"});
1092+
assertThat(set.floor("f")).isEqualTo("f");
1093+
assertThat(set.floor("j")).isEqualTo("i");
1094+
assertThat(set.floor("q")).isEqualTo("k");
1095+
}
1096+
1097+
public void testFloor_elementAbsent() {
1098+
ImmutableSortedSet<String> set =
1099+
ImmutableSortedSet.copyOf(new String[] {"e", "e", "f", "b", "i", "d", "c", "k"});
1100+
assertThat(set.floor("a")).isNull();
1101+
}
1102+
1103+
public void testCeiling_emptySet() {
1104+
ImmutableSortedSet<String> set = ImmutableSortedSet.copyOf(new String[] {});
1105+
assertThat(set.ceiling("f")).isNull();
1106+
}
1107+
1108+
public void testCeiling_elementPresent() {
1109+
ImmutableSortedSet<String> set =
1110+
ImmutableSortedSet.copyOf(new String[] {"e", "e", "f", "f", "i", "d", "c", "k", "p", "c"});
1111+
assertThat(set.ceiling("f")).isEqualTo("f");
1112+
assertThat(set.ceiling("h")).isEqualTo("i");
1113+
assertThat(set.ceiling("a")).isEqualTo("c");
1114+
}
1115+
1116+
public void testCeiling_elementAbsent() {
1117+
ImmutableSortedSet<String> set =
1118+
ImmutableSortedSet.copyOf(new String[] {"e", "a", "e", "f", "b", "i", "d", "a", "c", "k"});
1119+
assertThat(set.ceiling("l")).isNull();
1120+
}
1121+
10841122
public void testSubSetExclusiveExclusive() {
10851123
String[] strings = NUMBER_NAMES.toArray(new String[0]);
10861124
ImmutableSortedSet<String> set = ImmutableSortedSet.copyOf(strings);

guava/src/com/google/common/collect/ImmutableSortedSet.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -693,14 +693,12 @@ public E lower(E e) {
693693
}
694694

695695
/** @since 12.0 */
696-
@GwtIncompatible // NavigableSet
697696
@Override
698697
public E floor(E e) {
699698
return Iterators.getNext(headSet(e, true).descendingIterator(), null);
700699
}
701700

702701
/** @since 12.0 */
703-
@GwtIncompatible // NavigableSet
704702
@Override
705703
public E ceiling(E e) {
706704
return Iterables.getFirst(tailSet(e, true), null);

0 commit comments

Comments
 (0)