-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[LANG-1733] Add null-safe Consumers.accept() and Functions.apply() #1215
[LANG-1733] Add null-safe Consumers.accept() and Functions.apply() #1215
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@imbf
Please follow the pattern established by the 'Suppliers' class in this package. There is no need for a new class. TY.
@garydgregory Have to admit, that even I don't understand, what you are suggesting? |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #1215 +/- ##
============================================
+ Coverage 92.07% 92.54% +0.46%
- Complexity 7587 7757 +170
============================================
Files 200 200
Lines 15844 15901 +57
Branches 2890 2819 -71
============================================
+ Hits 14589 14715 +126
+ Misses 682 648 -34
+ Partials 573 538 -35 ☔ View full report in Codecov by Sentry. |
Our |
Thanks for your feedback. I moved null handling features to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@imbf
Thank you for your update. Please see my comments.
* @param consumer the consumer to consume. | ||
* @param <T> the type of the argument the consumer accepts. | ||
*/ | ||
public static <T> void acceptIfNotNull(final T object, final Consumer<T> consumer) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- The name can just be
accept
. - Add a Javadoc since tag.
- Add null-check for consumer, see
Suppliers.get()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disagree that the API is noop if the input to the functional object is null. A lot of APIs accept null as valid input, for example to reset a value to its default.
@@ -21,7 +21,7 @@ | |||
import java.util.function.Function; | |||
|
|||
/** | |||
* Provides {@link Consumer} instances. | |||
* Provides {@link Consumer} instances and utilities for working with {@link Consumer}. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary edit.
* @param function the function to apply. | ||
* @param <T> the type of the argument the function applies. | ||
* @param <R> the type of the result the function returns. | ||
* @return the value the function returns If the object is null; null otherwise. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- The name can just be
apply
. - Add a Javadoc since tag.
- Add null-check for
function
, seeSuppliers.get()
Consumers.acceptIfNotNull(builder, sb -> sb.append("-bae")); | ||
assertEquals("jay-bae", builder.toString()); | ||
|
||
assertDoesNotThrow(() -> Consumers.acceptIfNotNull((String) null, string -> fail())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for assertDoesNotThrow
@Test | ||
public void testAcceptIfNotNull() { | ||
StringBuilder builder = new StringBuilder("jay"); | ||
Consumers.acceptIfNotNull(builder, sb -> sb.append("-bae")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't use your name as test data, just use data like foo and bar like you did in the other test.
d419f9b
to
2b689da
Compare
Thank you for your feedback. I applied most of feedbacks. but the name suggestion is not good for me. |
Well, you can change the names now, and save me the work, or I'll change them after I review the PR and it is merged... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@imbf
Please see my additional comments.
Are there call sites within Commons Lang where it would be beneficial to use these new APIs? If the potential call sites are already null-safe concerning the functional objects, then those would not be candidates, but, there may be others.
* @param consumer the consumer to consume. | ||
* @param <T> the type of the argument the consumer accepts. | ||
*/ | ||
public static <T> void acceptIfNotNull(final T object, final Consumer<T> consumer) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disagree that the API is noop if the input to the functional object is null. A lot of APIs accept null as valid input, for example to reset a value to its default.
* @since 3.15.0 | ||
*/ | ||
public static <T, R> R applyIfNotNull(final T object, final Function<T, R> function) { | ||
return object != null && function != null ? function.apply(object) : null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disagree that the API is noop if the input to the functional object is null. A lot of APIs accept null as valid input, for example to reset a value to its default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sounds great. null can also be a valid input.
Thank you.
Initially, I added apis that only work when the object is not null. In this case, there were many useful call sites in this project. (e.g. This is not my intent. If you want to apply a functional interface regardless of whether the object is null or not, making other methods is better than fixing existing methods. What do you think about my opinion? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @imbf
Thank you for your update.
I added one comment.
*/ | ||
@Test | ||
public void testAccept() { | ||
final StringBuilder builder = new StringBuilder("foo"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't use mock testing here, it makes the test too obtuse; this is not a hard feature to test IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your feedback.
I modified my code to not use mock testing.
@imbf |
JIRA Ticket: LANG-1733
Suggestion: Introduce null handling features working with with the java.util.function package, or more generally, with Java 8 lambdas.