7

I have three Integer variables, where I am not allowed to change to primitive int and I need to check if at least one of them have a value greater than 0. Is there a shorter / cleaner way to rewrite my code below:

Integer foo = // null or some value
Integer bar = // null or some value
Integer baz = // null or some value

boolean atLeastOnePositive = (foo != null && foo > 0) || (bar != null && bar > 0) || (baz != null && baz > 0)

return atLeastOnePositive;
4
  • I guess that introducing a isPositive method is a bit of an overkill in your situation? Commented Jun 30, 2020 at 9:41
  • So you do expect those to be null occasionally?
    – Amadán
    Commented Jun 30, 2020 at 9:43
  • @ErichKitzmueller Maybe too much but still probably more reader-friendly than what I have so far.
    – nopens
    Commented Jun 30, 2020 at 9:47
  • @Amadán Yes. I will get the values from a method call getFoo() get Bar() .. which might return null.
    – nopens
    Commented Jun 30, 2020 at 9:49

5 Answers 5

9

You can use Stream and do like this:

boolean atLeastOnePositive = Stream.of(foo, bar, baz)
  .anyMatch(value -> value != null && value > 0);

1
  • I am not really familiar with streams yet. It's probably time to change that.Thank you.
    – nopens
    Commented Jun 30, 2020 at 10:15
7

I guess a varargs method would be the cleanest way:

public static boolean atLeastOnePositive(Integer...integers)
{
    for(Integer integer : integers)
    {
        if(integer != null && integer > 0) return true;
    }
    
    return false;
}
1
  • Nice. Had something similar with an array as parameter. But your approach with varargs is definitely better.
    – nopens
    Commented Jun 30, 2020 at 10:15
5

If you don't want to introduce a new method with a generic solution to the problem, your way is perfectly fine and I don't see much room for improvement.

If you do want to introduce a new method, I'd suggest to combine the solutions of maio290 and Iczapski. When using a stream, filters enhance readability:

public static boolean containsAtleastOnePositive(final Integer... values) {
    return Arrays.stream(values).filter(Objects::nonNull)
                                .anyMatch(v -> v > 0);
}
3
  • Nice answer, final is redundant here, no?
    – Ori Marko
    Commented Jun 30, 2020 at 10:00
  • I like your approach. Thank you.
    – nopens
    Commented Jun 30, 2020 at 10:16
  • 1
    @user7294900 I like to declare parameters as final to protect myself from accidentally changing them in the method. This wouldn't affect the caller but I'd work with a wrong reference then.
    – Amadán
    Commented Jul 1, 2020 at 6:15
1

You can use the method below to return true if var is positive and false if var is negative or null.

public boolean isPositive(Integer var) {
        return (0 < (var == null ? 0 : var));
}
2
  • You should rename the method though.
    – maio290
    Commented Jun 30, 2020 at 10:11
  • Thank you for your answer, but for multiple arguments this will become unreadable.
    – nopens
    Commented Jun 30, 2020 at 10:16
0

Another way would be to use Optional<Integer> instead of nullable Integer variable:

final Optional<Integer> foo = Optional.of(-1);
final Optional<Integer> bar = Optional.of(2);
final Optional<Integer> baz = Optional.empty();
final boolean atLeastOnePositive = Stream.of(foo, bar, baz)
    .flatMap(Optional::stream)
    .anyMatch(x -> x > 0);

Not the answer you're looking for? Browse other questions tagged or ask your own question.