Skip to content

Commit

Permalink
Add Optional isEmpty(). Update test matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
aNNiMON committed Jul 25, 2018
1 parent 2a8fd76 commit aef50c1
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 9 deletions.
10 changes: 10 additions & 0 deletions stream/src/main/java/com/annimon/stream/Optional.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ public boolean isPresent() {
return value != null;
}

/**
* Checks the value is not present.
*
* @return {@code true} if a value is not present, {@code false} otherwise
* @since 1.2.1
*/
public boolean isEmpty() {
return value == null;
}

/**
* Invokes consumer function with value if present.
*
Expand Down
10 changes: 10 additions & 0 deletions stream/src/main/java/com/annimon/stream/OptionalBoolean.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ public boolean isPresent() {
return isPresent;
}

/**
* Checks the value is not present.
*
* @return {@code true} if a value is not present, {@code false} otherwise
* @since 1.2.1
*/
public boolean isEmpty() {
return !isPresent;
}

/**
* Invokes consumer function with value if present, otherwise does nothing.
*
Expand Down
10 changes: 10 additions & 0 deletions stream/src/main/java/com/annimon/stream/OptionalDouble.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ public boolean isPresent() {
return isPresent;
}

/**
* Checks the value is not present.
*
* @return {@code true} if a value is not present, {@code false} otherwise
* @since 1.2.1
*/
public boolean isEmpty() {
return !isPresent;
}

/**
* Invokes consumer function with value if present, otherwise does nothing.
*
Expand Down
10 changes: 10 additions & 0 deletions stream/src/main/java/com/annimon/stream/OptionalInt.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ public boolean isPresent() {
return isPresent;
}

/**
* Checks the value is not present.
*
* @return {@code true} if a value is not present, {@code false} otherwise
* @since 1.2.1
*/
public boolean isEmpty() {
return !isPresent;
}

/**
* Invokes consumer function with value if present, otherwise does nothing.
*
Expand Down
10 changes: 10 additions & 0 deletions stream/src/main/java/com/annimon/stream/OptionalLong.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ public boolean isPresent() {
return isPresent;
}

/**
* Checks the value is not present.
*
* @return {@code true} if a value is not present, {@code false} otherwise
* @since 1.2.1
*/
public boolean isEmpty() {
return !isPresent;
}

/**
* Invokes consumer function with value if present, otherwise does nothing.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static class IsEmptyMatcher extends TypeSafeDiagnosingMatcher<OptionalDou
@Override
protected boolean matchesSafely(OptionalDouble optional, Description mismatchDescription) {
mismatchDescription.appendText("OptionalDouble was present");
return !optional.isPresent();
return optional.isEmpty();
}

@Override
Expand All @@ -64,7 +64,7 @@ public HasValueMatcher(Matcher<? super Double> matcher) {

@Override
protected boolean matchesSafely(OptionalDouble optional, Description mismatchDescription) {
if (!optional.isPresent()) {
if (optional.isEmpty()) {
mismatchDescription.appendText("OptionalDouble was empty");
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static class IsEmptyMatcher extends TypeSafeDiagnosingMatcher<OptionalInt
@Override
protected boolean matchesSafely(OptionalInt optional, Description mismatchDescription) {
mismatchDescription.appendText("OptionalInt was present");
return !optional.isPresent();
return optional.isEmpty();
}

@Override
Expand All @@ -64,7 +64,7 @@ public HasValueMatcher(Matcher<? super Integer> matcher) {

@Override
protected boolean matchesSafely(OptionalInt optional, Description mismatchDescription) {
if (!optional.isPresent()) {
if (optional.isEmpty()) {
mismatchDescription.appendText("OptionalInt was empty");
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static class IsEmptyMatcher extends TypeSafeDiagnosingMatcher<OptionalLon
@Override
protected boolean matchesSafely(OptionalLong optional, Description mismatchDescription) {
mismatchDescription.appendText("OptionalLong was present");
return !optional.isPresent();
return optional.isEmpty();
}

@Override
Expand All @@ -64,7 +64,7 @@ public HasValueMatcher(Matcher<? super Long> matcher) {

@Override
protected boolean matchesSafely(OptionalLong optional, Description mismatchDescription) {
if (!optional.isPresent()) {
if (optional.isEmpty()) {
mismatchDescription.appendText("OptionalLong was empty");
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static class IsEmptyMatcher extends TypeSafeDiagnosingMatcher<Optional<?>
@Override
protected boolean matchesSafely(Optional<?> optional, Description mismatchDescription) {
mismatchDescription.appendText("Optional was present");
return !optional.isPresent();
return optional.isEmpty();
}

@Override
Expand All @@ -64,7 +64,7 @@ public HasValueMatcher(Matcher<? super T> matcher) {

@Override
protected boolean matchesSafely(Optional<T> optional, Description mismatchDescription) {
if (!optional.isPresent()) {
if (optional.isEmpty()) {
mismatchDescription.appendText("Optional was empty");
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static class EmptyOptionalMatcher<T> implements ArgumentMatcher<Optional<

@Override
public boolean matches(Optional<T> argument) {
return argument != null && !argument.isPresent();
return argument != null && argument.isEmpty();
}

@Override
Expand Down

0 comments on commit aef50c1

Please sign in to comment.