-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
ImmutableEnumSetRules
refaster rules
- Loading branch information
1 parent
f2238c7
commit fcf5894
Showing
6 changed files
with
359 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
...one-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRules.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
package tech.picnic.errorprone.refasterrules; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import com.google.common.collect.Sets; | ||
import com.google.errorprone.refaster.Refaster; | ||
import com.google.errorprone.refaster.annotation.AfterTemplate; | ||
import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
import com.google.errorprone.refaster.annotation.Repeated; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.EnumSet; | ||
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; | ||
|
||
/** Refaster rules related to expressions dealing with immutable {@link Enum} collections. */ | ||
@OnlineDocumentation | ||
final class ImmutableEnumSetRules { | ||
private ImmutableEnumSetRules() {} | ||
|
||
/** | ||
* Prefer {@link Sets#immutableEnumSet(Iterable)} for enum collections to take advantage of | ||
* internally used {@link EnumSet}. | ||
*/ | ||
static final class ImmutableEnumSetIterable<T extends Enum<T>> { | ||
@BeforeTemplate | ||
ImmutableSet<T> before(Collection<T> elements) { | ||
return ImmutableSet.copyOf(elements); | ||
} | ||
|
||
@AfterTemplate | ||
ImmutableSet<T> after(Collection<T> elements) { | ||
return Sets.immutableEnumSet(elements); | ||
} | ||
} | ||
|
||
/** | ||
* Prefer {@link Sets#immutableEnumSet(Iterable)} for enum collections to take advantage of | ||
* internally used {@link EnumSet}. | ||
*/ | ||
static final class ImmutableEnumSetIterable1<T extends Enum<T>> { | ||
@BeforeTemplate | ||
ImmutableSet<T> before(T[] elements) { | ||
return ImmutableSet.copyOf(elements); | ||
} | ||
|
||
@AfterTemplate | ||
ImmutableSet<T> after(T[] elements) { | ||
return Sets.immutableEnumSet(Arrays.asList(elements)); | ||
} | ||
} | ||
|
||
/** | ||
* Prefer {@link Sets#immutableEnumSet(Enum, Enum[])} for enum collections to take advantage of | ||
* internally used {@link EnumSet}. | ||
*/ | ||
static final class ImmutableEnumSetOneElement<T extends Enum<T>> { | ||
@BeforeTemplate | ||
@SuppressWarnings("ImmutableEnumSetIterable" /* This is a more specific template. */) | ||
ImmutableSet<T> before(T e1) { | ||
return Refaster.anyOf(ImmutableSet.of(e1), ImmutableSet.copyOf(EnumSet.of(e1))); | ||
} | ||
|
||
@AfterTemplate | ||
@SuppressWarnings("unchecked") | ||
ImmutableSet<T> after(T e1) { | ||
return Sets.immutableEnumSet(e1); | ||
} | ||
} | ||
|
||
/** | ||
* Prefer {@link Sets#immutableEnumSet(Enum, Enum[])} for enum collections to take advantage of | ||
* internally used {@link EnumSet}. | ||
*/ | ||
static final class ImmutableEnumSetTwoElements<T extends Enum<T>> { | ||
@BeforeTemplate | ||
@SuppressWarnings("ImmutableEnumSetIterable" /* This is a more specific template. */) | ||
ImmutableSet<T> before(T e1, T e2) { | ||
return Refaster.anyOf(ImmutableSet.of(e1, e2), ImmutableSet.copyOf(EnumSet.of(e1, e2))); | ||
} | ||
|
||
@AfterTemplate | ||
@SuppressWarnings("unchecked") | ||
ImmutableSet<T> after(T e1, T e2) { | ||
return Sets.immutableEnumSet(e1, e2); | ||
} | ||
} | ||
|
||
/** | ||
* Prefer {@link Sets#immutableEnumSet(Enum, Enum[])} for enum collections to take advantage of | ||
* internally used {@link EnumSet}. | ||
*/ | ||
static final class ImmutableEnumSetThreeElements<T extends Enum<T>> { | ||
@BeforeTemplate | ||
@SuppressWarnings("ImmutableEnumSetIterable" /* This is a more specific template. */) | ||
ImmutableSet<T> before(T e1, T e2, T e3) { | ||
return Refaster.anyOf( | ||
ImmutableSet.of(e1, e2, e3), ImmutableSet.copyOf(EnumSet.of(e1, e2, e3))); | ||
} | ||
|
||
@AfterTemplate | ||
@SuppressWarnings("unchecked") | ||
ImmutableSet<T> after(T e1, T e2, T e3) { | ||
return Sets.immutableEnumSet(e1, e2, e3); | ||
} | ||
} | ||
|
||
/** | ||
* Prefer {@link Sets#immutableEnumSet(Enum, Enum[])} for enum collections to take advantage of | ||
* internally used {@link EnumSet}. | ||
*/ | ||
static final class ImmutableEnumSetFourElements<T extends Enum<T>> { | ||
@BeforeTemplate | ||
@SuppressWarnings("ImmutableEnumSetIterable" /* This is a more specific template. */) | ||
ImmutableSet<T> before(T e1, T e2, T e3, T e4) { | ||
return Refaster.anyOf( | ||
ImmutableSet.of(e1, e2, e3, e4), ImmutableSet.copyOf(EnumSet.of(e1, e2, e3, e4))); | ||
} | ||
|
||
@AfterTemplate | ||
@SuppressWarnings("unchecked") | ||
ImmutableSet<T> after(T e1, T e2, T e3, T e4) { | ||
return Sets.immutableEnumSet(e1, e2, e3, e4); | ||
} | ||
} | ||
|
||
/** | ||
* Prefer {@link Sets#immutableEnumSet(Enum, Enum[])} for enum collections to take advantage of | ||
* internally used {@link EnumSet}. | ||
*/ | ||
static final class ImmutableEnumSetFiveElements<T extends Enum<T>> { | ||
@BeforeTemplate | ||
@SuppressWarnings("ImmutableEnumSetIterable" /* This is a more specific template. */) | ||
ImmutableSet<T> before(T e1, T e2, T e3, T e4, T e5) { | ||
return Refaster.anyOf( | ||
ImmutableSet.of(e1, e2, e3, e4, e5), ImmutableSet.copyOf(EnumSet.of(e1, e2, e3, e4, e5))); | ||
} | ||
|
||
@AfterTemplate | ||
@SuppressWarnings("unchecked") | ||
ImmutableSet<T> after(T e1, T e2, T e3, T e4, T e5) { | ||
return Sets.immutableEnumSet(e1, e2, e3, e4, e5); | ||
} | ||
} | ||
|
||
/** | ||
* Prefer {@link Sets#immutableEnumSet(Enum, Enum[])} for enum collections to take advantage of | ||
* internally used {@link EnumSet}. | ||
*/ | ||
static final class ImmutableEnumSetSixElements<T extends Enum<T>> { | ||
@BeforeTemplate | ||
ImmutableSet<T> before(T e1, T e2, T e3, T e4, T e5, T e6) { | ||
return ImmutableSet.of(e1, e2, e3, e4, e5, e6); | ||
} | ||
|
||
@AfterTemplate | ||
@SuppressWarnings("unchecked") | ||
ImmutableSet<T> after(T e1, T e2, T e3, T e4, T e5, T e6) { | ||
return Sets.immutableEnumSet(e1, e2, e3, e4, e5, e6); | ||
} | ||
} | ||
|
||
/** | ||
* Prefer {@link Sets#immutableEnumSet(Enum, Enum[])} for enum collections to take advantage of | ||
* internally used {@link EnumSet}. | ||
*/ | ||
static final class ImmutableEnumSetVarArgs<T extends Enum<T>> { | ||
@BeforeTemplate | ||
@SuppressWarnings("ImmutableEnumSetIterable" /* This is a more specific template. */) | ||
ImmutableSet<T> before(T e1, @Repeated T elements) { | ||
return ImmutableSet.copyOf(EnumSet.of(e1, Refaster.asVarargs(elements))); | ||
} | ||
|
||
@AfterTemplate | ||
ImmutableSet<T> after(T e1, @Repeated T elements) { | ||
return Sets.immutableEnumSet(e1, Refaster.asVarargs(elements)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...c/test/resources/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRulesTestInput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package tech.picnic.errorprone.refasterrules; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import java.math.RoundingMode; | ||
import java.util.EnumSet; | ||
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; | ||
|
||
final class ImmutableEnumSetRulesTest implements RefasterRuleCollectionTestCase { | ||
@Override | ||
public ImmutableSet<Object> elidedTypesAndStaticImports() { | ||
return ImmutableSet.of(EnumSet.class); | ||
} | ||
|
||
ImmutableSet<ImmutableSet<RoundingMode>> testImmutableEnumSetIterable() { | ||
return ImmutableSet.of( | ||
ImmutableSet.copyOf(EnumSet.range(RoundingMode.UP, RoundingMode.UNNECESSARY)), | ||
ImmutableSet.copyOf(EnumSet.allOf(RoundingMode.class))); | ||
} | ||
|
||
ImmutableSet<ImmutableSet<RoundingMode>> testImmutableEnumSetIterable1() { | ||
return ImmutableSet.of( | ||
ImmutableSet.copyOf(RoundingMode.values()), | ||
ImmutableSet.copyOf(RoundingMode.class.getEnumConstants())); | ||
} | ||
|
||
ImmutableSet<ImmutableSet<RoundingMode>> testImmutableEnumSetOneElement() { | ||
return ImmutableSet.of( | ||
ImmutableSet.of(RoundingMode.UP), ImmutableSet.copyOf(EnumSet.of(RoundingMode.UP))); | ||
} | ||
|
||
ImmutableSet<ImmutableSet<RoundingMode>> testImmutableEnumSetTwoElements() { | ||
return ImmutableSet.of( | ||
ImmutableSet.of(RoundingMode.UP, RoundingMode.DOWN), | ||
ImmutableSet.copyOf(EnumSet.of(RoundingMode.UP, RoundingMode.DOWN))); | ||
} | ||
|
||
ImmutableSet<ImmutableSet<RoundingMode>> testImmutableEnumSetThreeElements() { | ||
return ImmutableSet.of( | ||
ImmutableSet.of(RoundingMode.UP, RoundingMode.DOWN, RoundingMode.CEILING), | ||
ImmutableSet.copyOf(EnumSet.of(RoundingMode.UP, RoundingMode.DOWN, RoundingMode.CEILING))); | ||
} | ||
|
||
ImmutableSet<ImmutableSet<RoundingMode>> testImmutableEnumSetFourElements() { | ||
return ImmutableSet.of( | ||
ImmutableSet.of( | ||
RoundingMode.UP, RoundingMode.DOWN, RoundingMode.CEILING, RoundingMode.FLOOR), | ||
ImmutableSet.copyOf( | ||
EnumSet.of( | ||
RoundingMode.UP, RoundingMode.DOWN, RoundingMode.CEILING, RoundingMode.FLOOR))); | ||
} | ||
|
||
ImmutableSet<ImmutableSet<RoundingMode>> testImmutableEnumSetFiveElements() { | ||
return ImmutableSet.of( | ||
ImmutableSet.of( | ||
RoundingMode.UP, | ||
RoundingMode.DOWN, | ||
RoundingMode.CEILING, | ||
RoundingMode.FLOOR, | ||
RoundingMode.UNNECESSARY), | ||
ImmutableSet.copyOf( | ||
EnumSet.of( | ||
RoundingMode.UP, | ||
RoundingMode.DOWN, | ||
RoundingMode.CEILING, | ||
RoundingMode.FLOOR, | ||
RoundingMode.UNNECESSARY))); | ||
} | ||
|
||
ImmutableSet<RoundingMode> testImmutableEnumSetSixElements() { | ||
return ImmutableSet.of( | ||
RoundingMode.UP, | ||
RoundingMode.DOWN, | ||
RoundingMode.CEILING, | ||
RoundingMode.FLOOR, | ||
RoundingMode.UNNECESSARY, | ||
RoundingMode.HALF_EVEN); | ||
} | ||
|
||
ImmutableSet<RoundingMode> testImmutableEnumSetVarArgs() { | ||
return ImmutableSet.copyOf( | ||
EnumSet.of( | ||
RoundingMode.UP, | ||
RoundingMode.DOWN, | ||
RoundingMode.CEILING, | ||
RoundingMode.FLOOR, | ||
RoundingMode.UNNECESSARY, | ||
RoundingMode.HALF_EVEN)); | ||
} | ||
} |
Oops, something went wrong.