Skip to content

Commit

Permalink
Suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 committed Sep 10, 2022
1 parent 3a02aad commit 478902e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,32 @@
final class NullTemplates {
private NullTemplates() {}

/** Prefer the {@code ==} operator over {@link Objects#isNull(Object)}. */
static final class IsNull {
@BeforeTemplate
boolean before(@Nullable Object object) {
return Objects.isNull(object);
}

@AfterTemplate
boolean after(@Nullable Object object) {
return object == null;
}
}

/** Prefer the {@code !=} operator over {@link Objects#nonNull(Object)}. */
static final class IsNotNull {
@BeforeTemplate
boolean before(@Nullable Object object) {
return Objects.nonNull(object);
}

@AfterTemplate
boolean after(@Nullable Object object) {
return object != null;
}
}

/** Prefer {@link Objects#requireNonNullElse(Object, Object)} over the Guava alternative. */
// XXX: This rule is not valid in case `second` is `@Nullable`: in that case the Guava variant
// will return `null`, while the JDK variant will throw an NPE.
Expand Down Expand Up @@ -56,34 +82,4 @@ Predicate<T> after() {
return Objects::nonNull;
}
}

/**
* Prefer the {@code ==} operator over {@link java.util.Objects#isNull(Object)} for null checks.
*/
static final class IsNull {
@BeforeTemplate
boolean before(@Nullable Object object) {
return Objects.isNull(object);
}

@AfterTemplate
boolean after(@Nullable Object object) {
return object == null;
}
}

/**
* Prefer the {@code !=} operator over {@link java.util.Objects#isNull(Object)} for null checks.
*/
static final class IsNotNull {
@BeforeTemplate
boolean before(@Nullable Object object) {
return Objects.nonNull(object);
}

@AfterTemplate
boolean after(@Nullable Object object) {
return object != null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ public ImmutableSet<?> elidedTypesAndStaticImports() {
return ImmutableSet.of(MoreObjects.class);
}

boolean testIsNull() {
return Objects.isNull("foo");
}

boolean testIsNotNull() {
return Objects.nonNull("foo");
}

String testRequireNonNullElse() {
return MoreObjects.firstNonNull("foo", "bar");
}
Expand All @@ -23,12 +31,4 @@ long testIsNullFunction() {
long testNonNullFunction() {
return Stream.of("foo").filter(s -> s != null).count();
}

boolean testIsNull() {
return Objects.isNull("foo");
}

boolean testIsNotNull() {
return Objects.nonNull("foo");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ public ImmutableSet<?> elidedTypesAndStaticImports() {
return ImmutableSet.of(MoreObjects.class);
}

boolean testIsNull() {
return "foo" == null;
}

boolean testIsNotNull() {
return "foo" != null;
}

String testRequireNonNullElse() {
return requireNonNullElse("foo", "bar");
}
Expand All @@ -25,12 +33,4 @@ long testIsNullFunction() {
long testNonNullFunction() {
return Stream.of("foo").filter(Objects::nonNull).count();
}

boolean testIsNull() {
return "foo" == null;
}

boolean testIsNotNull() {
return "foo" != null;
}
}

0 comments on commit 478902e

Please sign in to comment.