Skip to content

Commit

Permalink
Remove Sets.complementOf from guava-gwt.
Browse files Browse the repository at this point in the history
Previously, it was present but failed at runtime under J2CL. And soon, J2CL may drop support for the reflective enum-related APIs that it depends on.

`guava-gwt` is used by both GWT and J2CL users, so we have to keep our code buildable against both.

If this CL causes trouble for you as a GWT user, let us know, and we can investigate options further. In the worst case, you can copy the implementation as needed, since `complementOf` is a static method.

RELNOTES=`collect`: Removed `Sets.complementOf` from `guava-gwt` to prepare for the removal of reflective enum-related APIs from J2CL. If this causes you problems as a GWT user, let us know.
PiperOrigin-RevId: 535589407
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed May 26, 2023
1 parent c3a155d commit 09db2c2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -578,48 +578,56 @@ public void testNewCOWASFromIterable() {
verifySetContents(set, SOME_COLLECTION);
}

@GwtIncompatible // complementOf
public void testComplementOfEnumSet() {
Set<SomeEnum> units = EnumSet.of(SomeEnum.B, SomeEnum.D);
EnumSet<SomeEnum> otherUnits = Sets.complementOf(units);
verifySetContents(otherUnits, EnumSet.of(SomeEnum.A, SomeEnum.C));
}

@GwtIncompatible // complementOf
public void testComplementOfEnumSetWithType() {
Set<SomeEnum> units = EnumSet.of(SomeEnum.B, SomeEnum.D);
EnumSet<SomeEnum> otherUnits = Sets.complementOf(units, SomeEnum.class);
verifySetContents(otherUnits, EnumSet.of(SomeEnum.A, SomeEnum.C));
}

@GwtIncompatible // complementOf
public void testComplementOfRegularSet() {
Set<SomeEnum> units = Sets.newHashSet(SomeEnum.B, SomeEnum.D);
EnumSet<SomeEnum> otherUnits = Sets.complementOf(units);
verifySetContents(otherUnits, EnumSet.of(SomeEnum.A, SomeEnum.C));
}

@GwtIncompatible // complementOf
public void testComplementOfRegularSetWithType() {
Set<SomeEnum> units = Sets.newHashSet(SomeEnum.B, SomeEnum.D);
EnumSet<SomeEnum> otherUnits = Sets.complementOf(units, SomeEnum.class);
verifySetContents(otherUnits, EnumSet.of(SomeEnum.A, SomeEnum.C));
}

@GwtIncompatible // complementOf
public void testComplementOfEmptySet() {
Set<SomeEnum> noUnits = Collections.emptySet();
EnumSet<SomeEnum> allUnits = Sets.complementOf(noUnits, SomeEnum.class);
verifySetContents(EnumSet.allOf(SomeEnum.class), allUnits);
}

@GwtIncompatible // complementOf
public void testComplementOfFullSet() {
Set<SomeEnum> allUnits = Sets.newHashSet(SomeEnum.values());
EnumSet<SomeEnum> noUnits = Sets.complementOf(allUnits, SomeEnum.class);
verifySetContents(noUnits, EnumSet.noneOf(SomeEnum.class));
}

@GwtIncompatible // complementOf
public void testComplementOfEmptyEnumSetWithoutType() {
Set<SomeEnum> noUnits = EnumSet.noneOf(SomeEnum.class);
EnumSet<SomeEnum> allUnits = Sets.complementOf(noUnits);
verifySetContents(allUnits, EnumSet.allOf(SomeEnum.class));
}

@GwtIncompatible // complementOf
public void testComplementOfEmptySetWithoutTypeDoesntWork() {
Set<SomeEnum> set = Collections.emptySet();
try {
Expand Down
3 changes: 3 additions & 0 deletions android/guava/src/com/google/common/collect/Sets.java
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ public static <E extends Comparable> TreeSet<E> newTreeSet(Iterable<? extends E>
* contains no elements
*/
@J2ktIncompatible
@GwtIncompatible
public static <E extends Enum<E>> EnumSet<E> complementOf(Collection<E> collection) {
if (collection instanceof EnumSet) {
return EnumSet.complementOf((EnumSet<E>) collection);
Expand All @@ -486,6 +487,7 @@ public static <E extends Enum<E>> EnumSet<E> complementOf(Collection<E> collecti
* @return a new, modifiable {@code EnumSet} initially containing all the values of the enum not
* present in the given collection
*/
@GwtIncompatible
public static <E extends Enum<E>> EnumSet<E> complementOf(
Collection<E> collection, Class<E> type) {
checkNotNull(collection);
Expand All @@ -494,6 +496,7 @@ public static <E extends Enum<E>> EnumSet<E> complementOf(
: makeComplementByHand(collection, type);
}

@GwtIncompatible
private static <E extends Enum<E>> EnumSet<E> makeComplementByHand(
Collection<E> collection, Class<E> type) {
EnumSet<E> result = EnumSet.allOf(type);
Expand Down
8 changes: 8 additions & 0 deletions guava-tests/test/com/google/common/collect/SetsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -609,48 +609,56 @@ public void testNewCOWASFromIterable() {
verifySetContents(set, SOME_COLLECTION);
}

@GwtIncompatible // complementOf
public void testComplementOfEnumSet() {
Set<SomeEnum> units = EnumSet.of(SomeEnum.B, SomeEnum.D);
EnumSet<SomeEnum> otherUnits = Sets.complementOf(units);
verifySetContents(otherUnits, EnumSet.of(SomeEnum.A, SomeEnum.C));
}

@GwtIncompatible // complementOf
public void testComplementOfEnumSetWithType() {
Set<SomeEnum> units = EnumSet.of(SomeEnum.B, SomeEnum.D);
EnumSet<SomeEnum> otherUnits = Sets.complementOf(units, SomeEnum.class);
verifySetContents(otherUnits, EnumSet.of(SomeEnum.A, SomeEnum.C));
}

@GwtIncompatible // complementOf
public void testComplementOfRegularSet() {
Set<SomeEnum> units = Sets.newHashSet(SomeEnum.B, SomeEnum.D);
EnumSet<SomeEnum> otherUnits = Sets.complementOf(units);
verifySetContents(otherUnits, EnumSet.of(SomeEnum.A, SomeEnum.C));
}

@GwtIncompatible // complementOf
public void testComplementOfRegularSetWithType() {
Set<SomeEnum> units = Sets.newHashSet(SomeEnum.B, SomeEnum.D);
EnumSet<SomeEnum> otherUnits = Sets.complementOf(units, SomeEnum.class);
verifySetContents(otherUnits, EnumSet.of(SomeEnum.A, SomeEnum.C));
}

@GwtIncompatible // complementOf
public void testComplementOfEmptySet() {
Set<SomeEnum> noUnits = Collections.emptySet();
EnumSet<SomeEnum> allUnits = Sets.complementOf(noUnits, SomeEnum.class);
verifySetContents(EnumSet.allOf(SomeEnum.class), allUnits);
}

@GwtIncompatible // complementOf
public void testComplementOfFullSet() {
Set<SomeEnum> allUnits = Sets.newHashSet(SomeEnum.values());
EnumSet<SomeEnum> noUnits = Sets.complementOf(allUnits, SomeEnum.class);
verifySetContents(noUnits, EnumSet.noneOf(SomeEnum.class));
}

@GwtIncompatible // complementOf
public void testComplementOfEmptyEnumSetWithoutType() {
Set<SomeEnum> noUnits = EnumSet.noneOf(SomeEnum.class);
EnumSet<SomeEnum> allUnits = Sets.complementOf(noUnits);
verifySetContents(allUnits, EnumSet.allOf(SomeEnum.class));
}

@GwtIncompatible // complementOf
public void testComplementOfEmptySetWithoutTypeDoesntWork() {
Set<SomeEnum> set = Collections.emptySet();
try {
Expand Down
3 changes: 3 additions & 0 deletions guava/src/com/google/common/collect/Sets.java
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ public static <E extends Comparable> TreeSet<E> newTreeSet(Iterable<? extends E>
* contains no elements
*/
@J2ktIncompatible
@GwtIncompatible
public static <E extends Enum<E>> EnumSet<E> complementOf(Collection<E> collection) {
if (collection instanceof EnumSet) {
return EnumSet.complementOf((EnumSet<E>) collection);
Expand All @@ -500,6 +501,7 @@ public static <E extends Enum<E>> EnumSet<E> complementOf(Collection<E> collecti
* @return a new, modifiable {@code EnumSet} initially containing all the values of the enum not
* present in the given collection
*/
@GwtIncompatible
public static <E extends Enum<E>> EnumSet<E> complementOf(
Collection<E> collection, Class<E> type) {
checkNotNull(collection);
Expand All @@ -508,6 +510,7 @@ public static <E extends Enum<E>> EnumSet<E> complementOf(
: makeComplementByHand(collection, type);
}

@GwtIncompatible
private static <E extends Enum<E>> EnumSet<E> makeComplementByHand(
Collection<E> collection, Class<E> type) {
EnumSet<E> result = EnumSet.allOf(type);
Expand Down

0 comments on commit 09db2c2

Please sign in to comment.