Skip to content

Commit

Permalink
Extend PrimitiveRules Refaster rule collection (#608)
Browse files Browse the repository at this point in the history
Assorted methods and constants exposed by Guava's
`com.google.common.primitives.*` types are now replaced with their JDK
equivalents.

While there, also update the parameter types of some existing `@AfterTemplate`
methods, resolving an inconsistency flagged by @knutwannheden.
  • Loading branch information
Stephan202 authored May 3, 2023
1 parent 4b69fe9 commit 52245c6
Show file tree
Hide file tree
Showing 3 changed files with 530 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package tech.picnic.errorprone.refasterrules;

import com.google.common.primitives.Booleans;
import com.google.common.primitives.Bytes;
import com.google.common.primitives.Chars;
import com.google.common.primitives.Doubles;
import com.google.common.primitives.Floats;
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
import com.google.common.primitives.Shorts;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation;
Expand All @@ -19,7 +26,7 @@ boolean before(double a, double b) {
}

@AfterTemplate
boolean after(long a, long b) {
boolean after(double a, double b) {
return a < b;
}
}
Expand All @@ -33,7 +40,7 @@ boolean before(double a, double b) {
}

@AfterTemplate
boolean after(long a, long b) {
boolean after(double a, double b) {
return a <= b;
}
}
Expand All @@ -47,7 +54,7 @@ boolean before(double a, double b) {
}

@AfterTemplate
boolean after(long a, long b) {
boolean after(double a, double b) {
return a > b;
}
}
Expand All @@ -61,21 +68,320 @@ boolean before(double a, double b) {
}

@AfterTemplate
boolean after(long a, long b) {
boolean after(double a, double b) {
return a >= b;
}
}

/** Prefer {@link Math#toIntExact(long)} over the Guava alternative. */
static final class LongToIntExact {
@BeforeTemplate
int before(long a) {
return Ints.checkedCast(a);
int before(long l) {
return Ints.checkedCast(l);
}

@AfterTemplate
int after(long a) {
return Math.toIntExact(a);
int after(long l) {
return Math.toIntExact(l);
}
}

/** Prefer {@link Boolean#hashCode(boolean)} over the Guava alternative. */
static final class BooleanHashCode {
@BeforeTemplate
int before(boolean b) {
return Booleans.hashCode(b);
}

@AfterTemplate
int after(boolean b) {
return Boolean.hashCode(b);
}
}

/** Prefer {@link Byte#hashCode(byte)} over the Guava alternative. */
static final class ByteHashCode {
@BeforeTemplate
int before(byte b) {
return Bytes.hashCode(b);
}

@AfterTemplate
int after(byte b) {
return Byte.hashCode(b);
}
}

/** Prefer {@link Character#hashCode(char)} over the Guava alternative. */
static final class CharacterHashCode {
@BeforeTemplate
int before(char c) {
return Chars.hashCode(c);
}

@AfterTemplate
int after(char c) {
return Character.hashCode(c);
}
}

/** Prefer {@link Short#hashCode(short)} over the Guava alternative. */
static final class ShortHashCode {
@BeforeTemplate
int before(short s) {
return Shorts.hashCode(s);
}

@AfterTemplate
int after(short s) {
return Short.hashCode(s);
}
}

/** Prefer {@link Integer#hashCode(int)} over the Guava alternative. */
static final class IntegerHashCode {
@BeforeTemplate
int before(int i) {
return Ints.hashCode(i);
}

@AfterTemplate
int after(int i) {
return Integer.hashCode(i);
}
}

/** Prefer {@link Long#hashCode(long)} over the Guava alternative. */
static final class LongHashCode {
@BeforeTemplate
int before(long l) {
return Longs.hashCode(l);
}

@AfterTemplate
int after(long l) {
return Long.hashCode(l);
}
}

/** Prefer {@link Float#hashCode(float)} over the Guava alternative. */
static final class FloatHashCode {
@BeforeTemplate
int before(float f) {
return Floats.hashCode(f);
}

@AfterTemplate
int after(float f) {
return Float.hashCode(f);
}
}

/** Prefer {@link Double#hashCode(double)} over the Guava alternative. */
static final class DoubleHashCode {
@BeforeTemplate
int before(double d) {
return Doubles.hashCode(d);
}

@AfterTemplate
int after(double d) {
return Double.hashCode(d);
}
}

/** Prefer {@link Boolean#compare(boolean, boolean)} over the Guava alternative. */
static final class BooleanCompare {
@BeforeTemplate
int before(boolean a, boolean b) {
return Booleans.compare(a, b);
}

@AfterTemplate
int after(boolean a, boolean b) {
return Boolean.compare(a, b);
}
}

/** Prefer {@link Character#compare(char, char)} over the Guava alternative. */
static final class CharacterCompare {
@BeforeTemplate
int before(char a, char b) {
return Chars.compare(a, b);
}

@AfterTemplate
int after(char a, char b) {
return Character.compare(a, b);
}
}

/** Prefer {@link Short#compare(short, short)} over the Guava alternative. */
static final class ShortCompare {
@BeforeTemplate
int before(short a, short b) {
return Shorts.compare(a, b);
}

@AfterTemplate
int after(short a, short b) {
return Short.compare(a, b);
}
}

/** Prefer {@link Integer#compare(int, int)} over the Guava alternative. */
static final class IntegerCompare {
@BeforeTemplate
int before(int a, int b) {
return Ints.compare(a, b);
}

@AfterTemplate
int after(int a, int b) {
return Integer.compare(a, b);
}
}

/** Prefer {@link Long#compare(long, long)} over the Guava alternative. */
static final class LongCompare {
@BeforeTemplate
int before(long a, long b) {
return Longs.compare(a, b);
}

@AfterTemplate
int after(long a, long b) {
return Long.compare(a, b);
}
}

/** Prefer {@link Float#compare(float, float)} over the Guava alternative. */
static final class FloatCompare {
@BeforeTemplate
int before(float a, float b) {
return Floats.compare(a, b);
}

@AfterTemplate
int after(float a, float b) {
return Float.compare(a, b);
}
}

/** Prefer {@link Double#compare(double, double)} over the Guava alternative. */
static final class DoubleCompare {
@BeforeTemplate
int before(double a, double b) {
return Doubles.compare(a, b);
}

@AfterTemplate
int after(double a, double b) {
return Double.compare(a, b);
}
}

/** Prefer {@link Character#BYTES} over the Guava alternative. */
static final class CharacterBytes {
@BeforeTemplate
int before() {
return Chars.BYTES;
}

@AfterTemplate
int after() {
return Character.BYTES;
}
}

/** Prefer {@link Short#BYTES} over the Guava alternative. */
static final class ShortBytes {
@BeforeTemplate
int before() {
return Shorts.BYTES;
}

@AfterTemplate
int after() {
return Short.BYTES;
}
}

/** Prefer {@link Integer#BYTES} over the Guava alternative. */
static final class IntegerBytes {
@BeforeTemplate
int before() {
return Ints.BYTES;
}

@AfterTemplate
int after() {
return Integer.BYTES;
}
}

/** Prefer {@link Long#BYTES} over the Guava alternative. */
static final class LongBytes {
@BeforeTemplate
int before() {
return Longs.BYTES;
}

@AfterTemplate
int after() {
return Long.BYTES;
}
}

/** Prefer {@link Float#BYTES} over the Guava alternative. */
static final class FloatBytes {
@BeforeTemplate
int before() {
return Floats.BYTES;
}

@AfterTemplate
int after() {
return Float.BYTES;
}
}

/** Prefer {@link Double#BYTES} over the Guava alternative. */
static final class DoubleBytes {
@BeforeTemplate
int before() {
return Doubles.BYTES;
}

@AfterTemplate
int after() {
return Double.BYTES;
}
}

/** Prefer {@link Float#isFinite(float)} over the Guava alternative. */
static final class FloatIsFinite {
@BeforeTemplate
boolean before(float f) {
return Floats.isFinite(f);
}

@AfterTemplate
boolean after(float f) {
return Float.isFinite(f);
}
}

/** Prefer {@link Double#isFinite(double)} over the Guava alternative. */
static final class DoubleIsFinite {
@BeforeTemplate
boolean before(double d) {
return Doubles.isFinite(d);
}

@AfterTemplate
boolean after(double d) {
return Double.isFinite(d);
}
}
}
Loading

0 comments on commit 52245c6

Please sign in to comment.