Skip to content

Commit

Permalink
Also cover checkPositionIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 committed Oct 15, 2022
1 parent f92b38f commit 0d6a33f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkElementIndex;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkPositionIndex;
import static com.google.common.base.Preconditions.checkState;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;

Expand Down Expand Up @@ -101,7 +102,43 @@ void after(T object, String message) {
}
}

// XXX: Also suggest `checkPositionIndex` usage.
/**
* Prefer {@link Preconditions#checkPositionIndex(int, int)} over less descriptive or more verbose
* alternatives.
*/
static final class CheckPositionIndex {
@BeforeTemplate
void before(int index, int size) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException();
}
}

@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(int index, int size) {
checkPositionIndex(index, size);
}
}

/**
* Prefer {@link Preconditions#checkPositionIndex(int, int, String)} over less descriptive or more
* verbose alternatives.
*/
static final class CheckPositionIndexWithMessage {
@BeforeTemplate
void before(int index, int size, String message) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException(message);
}
}

@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(int index, int size, String message) {
checkPositionIndex(index, size, message);
}
}

/** Prefer {@link Preconditions#checkState(boolean)} over more verbose alternatives. */
static final class CheckState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ void testCheckNotNullWithMessage() {
}
}

void testCheckPositionIndex() {
if (1 < 0 || 1 > 2) {
throw new IndexOutOfBoundsException();
}
}

void testCheckPositionIndexWithMessage() {
if (1 < 0 || 1 > 2) {
throw new IndexOutOfBoundsException("My position");
}
}

void testCheckState() {
if ("foo".isEmpty()) {
throw new IllegalStateException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkElementIndex;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkPositionIndex;
import static com.google.common.base.Preconditions.checkState;

import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase;
Expand All @@ -28,6 +29,14 @@ void testCheckNotNullWithMessage() {
checkNotNull("foo", "The string is null");
}

void testCheckPositionIndex() {
checkPositionIndex(1, 2);
}

void testCheckPositionIndexWithMessage() {
checkPositionIndex(1, 2, "My position");
}

void testCheckState() {
checkState(!"foo".isEmpty());
}
Expand Down

0 comments on commit 0d6a33f

Please sign in to comment.