From aef50c1758da36610cc82dcbbc63e925659eb57e Mon Sep 17 00:00:00 2001 From: Victor Date: Wed, 25 Jul 2018 12:02:27 +0300 Subject: [PATCH] Add Optional isEmpty(). Update test matchers --- stream/src/main/java/com/annimon/stream/Optional.java | 10 ++++++++++ .../main/java/com/annimon/stream/OptionalBoolean.java | 10 ++++++++++ .../main/java/com/annimon/stream/OptionalDouble.java | 10 ++++++++++ .../src/main/java/com/annimon/stream/OptionalInt.java | 10 ++++++++++ .../src/main/java/com/annimon/stream/OptionalLong.java | 10 ++++++++++ .../stream/test/hamcrest/OptionalDoubleMatcher.java | 4 ++-- .../stream/test/hamcrest/OptionalIntMatcher.java | 4 ++-- .../stream/test/hamcrest/OptionalLongMatcher.java | 4 ++-- .../annimon/stream/test/hamcrest/OptionalMatcher.java | 4 ++-- .../annimon/stream/test/mockito/OptionalMatcher.java | 2 +- 10 files changed, 59 insertions(+), 9 deletions(-) diff --git a/stream/src/main/java/com/annimon/stream/Optional.java b/stream/src/main/java/com/annimon/stream/Optional.java index 219ae562..4315d60b 100644 --- a/stream/src/main/java/com/annimon/stream/Optional.java +++ b/stream/src/main/java/com/annimon/stream/Optional.java @@ -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. * diff --git a/stream/src/main/java/com/annimon/stream/OptionalBoolean.java b/stream/src/main/java/com/annimon/stream/OptionalBoolean.java index 3a14c327..56b377c6 100644 --- a/stream/src/main/java/com/annimon/stream/OptionalBoolean.java +++ b/stream/src/main/java/com/annimon/stream/OptionalBoolean.java @@ -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. * diff --git a/stream/src/main/java/com/annimon/stream/OptionalDouble.java b/stream/src/main/java/com/annimon/stream/OptionalDouble.java index 9a0c233c..64380783 100644 --- a/stream/src/main/java/com/annimon/stream/OptionalDouble.java +++ b/stream/src/main/java/com/annimon/stream/OptionalDouble.java @@ -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. * diff --git a/stream/src/main/java/com/annimon/stream/OptionalInt.java b/stream/src/main/java/com/annimon/stream/OptionalInt.java index 159bd03f..6eb72f3e 100644 --- a/stream/src/main/java/com/annimon/stream/OptionalInt.java +++ b/stream/src/main/java/com/annimon/stream/OptionalInt.java @@ -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. * diff --git a/stream/src/main/java/com/annimon/stream/OptionalLong.java b/stream/src/main/java/com/annimon/stream/OptionalLong.java index 965730a1..f0185c5e 100644 --- a/stream/src/main/java/com/annimon/stream/OptionalLong.java +++ b/stream/src/main/java/com/annimon/stream/OptionalLong.java @@ -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. * diff --git a/streamTest/src/main/java/com/annimon/stream/test/hamcrest/OptionalDoubleMatcher.java b/streamTest/src/main/java/com/annimon/stream/test/hamcrest/OptionalDoubleMatcher.java index 9f683d64..32752670 100644 --- a/streamTest/src/main/java/com/annimon/stream/test/hamcrest/OptionalDoubleMatcher.java +++ b/streamTest/src/main/java/com/annimon/stream/test/hamcrest/OptionalDoubleMatcher.java @@ -45,7 +45,7 @@ public static class IsEmptyMatcher extends TypeSafeDiagnosingMatcher matcher) { @Override protected boolean matchesSafely(OptionalDouble optional, Description mismatchDescription) { - if (!optional.isPresent()) { + if (optional.isEmpty()) { mismatchDescription.appendText("OptionalDouble was empty"); return false; } diff --git a/streamTest/src/main/java/com/annimon/stream/test/hamcrest/OptionalIntMatcher.java b/streamTest/src/main/java/com/annimon/stream/test/hamcrest/OptionalIntMatcher.java index 301332b0..225ce351 100644 --- a/streamTest/src/main/java/com/annimon/stream/test/hamcrest/OptionalIntMatcher.java +++ b/streamTest/src/main/java/com/annimon/stream/test/hamcrest/OptionalIntMatcher.java @@ -45,7 +45,7 @@ public static class IsEmptyMatcher extends TypeSafeDiagnosingMatcher matcher) { @Override protected boolean matchesSafely(OptionalInt optional, Description mismatchDescription) { - if (!optional.isPresent()) { + if (optional.isEmpty()) { mismatchDescription.appendText("OptionalInt was empty"); return false; } diff --git a/streamTest/src/main/java/com/annimon/stream/test/hamcrest/OptionalLongMatcher.java b/streamTest/src/main/java/com/annimon/stream/test/hamcrest/OptionalLongMatcher.java index ebb98d1e..dfe95abe 100644 --- a/streamTest/src/main/java/com/annimon/stream/test/hamcrest/OptionalLongMatcher.java +++ b/streamTest/src/main/java/com/annimon/stream/test/hamcrest/OptionalLongMatcher.java @@ -45,7 +45,7 @@ public static class IsEmptyMatcher extends TypeSafeDiagnosingMatcher matcher) { @Override protected boolean matchesSafely(OptionalLong optional, Description mismatchDescription) { - if (!optional.isPresent()) { + if (optional.isEmpty()) { mismatchDescription.appendText("OptionalLong was empty"); return false; } diff --git a/streamTest/src/main/java/com/annimon/stream/test/hamcrest/OptionalMatcher.java b/streamTest/src/main/java/com/annimon/stream/test/hamcrest/OptionalMatcher.java index 6f0a39d5..c27e60d3 100644 --- a/streamTest/src/main/java/com/annimon/stream/test/hamcrest/OptionalMatcher.java +++ b/streamTest/src/main/java/com/annimon/stream/test/hamcrest/OptionalMatcher.java @@ -45,7 +45,7 @@ public static class IsEmptyMatcher extends TypeSafeDiagnosingMatcher @Override protected boolean matchesSafely(Optional optional, Description mismatchDescription) { mismatchDescription.appendText("Optional was present"); - return !optional.isPresent(); + return optional.isEmpty(); } @Override @@ -64,7 +64,7 @@ public HasValueMatcher(Matcher matcher) { @Override protected boolean matchesSafely(Optional optional, Description mismatchDescription) { - if (!optional.isPresent()) { + if (optional.isEmpty()) { mismatchDescription.appendText("Optional was empty"); return false; } diff --git a/streamTest/src/main/java/com/annimon/stream/test/mockito/OptionalMatcher.java b/streamTest/src/main/java/com/annimon/stream/test/mockito/OptionalMatcher.java index 3c24b554..dabcf2db 100644 --- a/streamTest/src/main/java/com/annimon/stream/test/mockito/OptionalMatcher.java +++ b/streamTest/src/main/java/com/annimon/stream/test/mockito/OptionalMatcher.java @@ -41,7 +41,7 @@ public static class EmptyOptionalMatcher implements ArgumentMatcher argument) { - return argument != null && !argument.isPresent(); + return argument != null && argument.isEmpty(); } @Override