tech.picnic.errorprone.refasterrules.ImmutableMultisetRules Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of error-prone-contrib Show documentation
Show all versions of error-prone-contrib Show documentation
Extra Error Prone plugins by Picnic.
package tech.picnic.errorprone.refasterrules;
import static com.google.common.collect.ImmutableMultiset.toImmutableMultiset;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import com.google.common.collect.ImmutableMultiset;
import com.google.common.collect.Streams;
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.UseImportPolicy;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.stream.Stream;
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation;
/** Refaster rules related to expressions dealing with {@link ImmutableMultiset}s. */
@OnlineDocumentation
final class ImmutableMultisetRules {
private ImmutableMultisetRules() {}
/** Prefer {@link ImmutableMultiset#builder()} over the associated constructor. */
// XXX: This drops generic type information, sometimes leading to non-compilable code. See
// https://github.com/google/error-prone/pull/2706.
static final class ImmutableMultisetBuilder {
@BeforeTemplate
ImmutableMultiset.Builder before() {
return new ImmutableMultiset.Builder<>();
}
@AfterTemplate
ImmutableMultiset.Builder after() {
return ImmutableMultiset.builder();
}
}
/** Prefer {@link ImmutableMultiset#of()} over more contrived alternatives. */
static final class EmptyImmutableMultiset {
@BeforeTemplate
ImmutableMultiset before() {
return Refaster.anyOf(
ImmutableMultiset.builder().build(), Stream.empty().collect(toImmutableMultiset()));
}
@AfterTemplate
ImmutableMultiset after() {
return ImmutableMultiset.of();
}
}
/**
* Prefer {@link ImmutableMultiset#copyOf(Iterable)} and variants over more contrived
* alternatives.
*/
static final class IterableToImmutableMultiset {
@BeforeTemplate
ImmutableMultiset before(T[] iterable) {
return Refaster.anyOf(
ImmutableMultiset.builder().add(iterable).build(),
Arrays.stream(iterable).collect(toImmutableMultiset()));
}
@BeforeTemplate
ImmutableMultiset before(Iterator iterable) {
return Refaster.anyOf(
ImmutableMultiset.builder().addAll(iterable).build(),
Streams.stream(iterable).collect(toImmutableMultiset()));
}
@BeforeTemplate
ImmutableMultiset before(Iterable iterable) {
return Refaster.anyOf(
ImmutableMultiset.builder().addAll(iterable).build(),
Streams.stream(iterable).collect(toImmutableMultiset()));
}
@BeforeTemplate
ImmutableMultiset before(Collection iterable) {
return iterable.stream().collect(toImmutableMultiset());
}
@AfterTemplate
ImmutableMultiset after(Iterable iterable) {
return ImmutableMultiset.copyOf(iterable);
}
}
/** Prefer {@link ImmutableMultiset#toImmutableMultiset()} over less idiomatic alternatives. */
static final class StreamToImmutableMultiset {
@BeforeTemplate
ImmutableMultiset before(Stream stream) {
return ImmutableMultiset.copyOf(stream.iterator());
}
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ImmutableMultiset after(Stream stream) {
return stream.collect(toImmutableMultiset());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy