ken
ken

Reputation: 471

Testing Java Optional

I'm looking into Java Optional and testing some usecases beyond the standard ones.

ok, let's take this example:

public void increment(Integer value) {
        if (currentValue != null && endValue != null && currentValue < (endValue - value)) {
            currentValue+=value;
        }
    }

currentValue and endValue are Integer.

Can the example above be converted in Java8 using Optional?

I was thinking something like that:

public void increment(Integer value) {     
        currentValue.filter(a-> a < (endValue.get()-value)).map(???);
    }

where currentValue and endValue are Optional<Integer>

I'm actually stuck with the .map Function.

I would appreciate any suggestion, thanks

Upvotes: 4

Views: 567

Answers (2)

holi-java
holi-java

Reputation: 30696

How about this?

currentValue = currentValue.filter(it -> endValue.isPresent())
                           .filter(it -> it < endValue.get() - value)
                           .map(it -> Optional.of(it + value))
                           .orElse(currentValue);

OR move the value into left side is more simpler than above.

currentValue = currentValue.map(it -> it + value)
                           .filter(it -> endValue.isPresent())
                           .filter(result -> result < endValue.get() )
                           .map(Optional::of)
                           .orElse(currentValue);

OR

currentValue = currentValue.filter(it -> it < endValue.map(end -> end - value)
                                                      .orElse(Integer.MIN_VALUE))
                           .map(it -> Optional.of(it + value))
                           .orElse(currentValue);

OR move the value into left side is more simpler than above.

currentValue=currentValue.map(it -> it + value)
                         .filter(result->result<endValue.orElse(Integer.MIN_VALUE))
                         .map(Optional::of)
                         .orElse(currentValue);

OR using Optional#flatMap instead:

currentValue = currentValue.flatMap(it ->
    endValue.filter(end -> it < end - value)
            .map(end -> Optional.of(it + value))
            .orElse(currentValue)
);

OR move the value into the left side then can using ternary-operator simplified:

currentValue = currentValue.map(it -> it + value).flatMap(result ->
    endValue.filter(end -> result < end)
            .isPresent() ? Optional.of(result) : currentValue
);

Upvotes: 6

Sweeper
Sweeper

Reputation: 273475

Note that I think you code is fine without optionals. If you insist that you want to use the optionals, keep reading.

You just want to add value in the map function, right? Then do this in the map call:

x -> x + value

Since map returns an Optional<Integer>, you should assign the result back to currentValue:

currenValue = currentValue.filter(
    a -> a < endValue.get() - value).map(x -> x + value);

However, this will crash if endValue does not have a value.

I used this little trick here to make this work:

currenValue = currentValue.filter(
    a -> a < endValue.orElse(Integer.MIN_VALUE + value) - value).map(x -> x + value).orElse(currentValue);

The idea is that if endValue is empty, it is replaced with Integer.MIN_VALUE + value. Integer.MIN_VALUE + value - value is equal to Integer.MIN_VALUE, which no number can be smaller than. a < Integer.MIN_VALUE must fail.

Upvotes: 1

Related Questions