测试Java可选

测试Java可选,java,java-8,optional,Java,Java 8,Optional,我正在研究Java Optional并测试一些超出标准用例的用例 好,让我们举个例子: public void increment(Integer value) { if (currentValue != null && endValue != null && currentValue < (endValue - value)) { currentValue+=value; } } 其中cur

我正在研究Java Optional并测试一些超出标准用例的用例

好,让我们举个例子:

public void increment(Integer value) {
        if (currentValue != null && endValue != null && currentValue < (endValue - value)) {
            currentValue+=value;
        }
    }
其中currentValue和endValue是可选的

实际上,我一直在使用
.map
函数


如果您有任何建议,我将不胜感激,谢谢。

请注意,我认为您的代码没有选项也可以。如果你坚持要使用选项,请继续阅读

您只想在
map
函数中添加
value
,对吗?然后在
map
调用中执行此操作:

x -> x + value
由于
映射
返回一个
可选
,因此应将结果分配回
当前值

currenValue = currentValue.filter(
    a -> a < endValue.get() - value).map(x -> x + value);
其思想是,如果
endValue
为空,则将其替换为
Integer.MIN\u VALUE+VALUE
Integer.MIN\u VALUE+VALUE-VALUE
等于
Integer.MIN\u VALUE
,任何数字都不能小于该值<代码>一个这个怎么样

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

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


@尤金:是的,先生。但是第一种方法比第二种方法可读。如果
current+value>end
,这些代码片段将
currentValue
设置为空,而不是将其保持不变@holi java是的,OP问到,你是对的,你的答案非常好,因为你展示了几种使用可选选项的方法。但这一次,我认为祈使句更具可读性。@Federicoperaltachaffner是的,我同意你的看法。流控制可以更充分地执行这样的操作,您可以编写
…ifPresent(v->currentValue=Optional.of(v))
如果对象的属性(
currentValue
)可以是/变成
null
,则该对象在其操作(
increment
)中会保持不可操作状态,这看起来很有问题。正如Sweeper在中所说,如果没有
Optional
/
Optionant
,您的代码就可以了。
currentValue = currentValue.filter(it -> endValue.isPresent())
                           .filter(it -> it < endValue.get() - value)
                           .map(it -> Optional.of(it + value))
                           .orElse(currentValue);
currentValue = currentValue.map(it -> it + value)
                           .filter(it -> endValue.isPresent())
                           .filter(result -> result < endValue.get() )
                           .map(Optional::of)
                           .orElse(currentValue);
currentValue = currentValue.filter(it -> it < endValue.map(end -> end - value)
                                                      .orElse(Integer.MIN_VALUE))
                           .map(it -> Optional.of(it + value))
                           .orElse(currentValue);
currentValue=currentValue.map(it -> it + value)
                         .filter(result->result<endValue.orElse(Integer.MIN_VALUE))
                         .map(Optional::of)
                         .orElse(currentValue);
currentValue = currentValue.flatMap(it ->
    endValue.filter(end -> it < end - value)
            .map(end -> Optional.of(it + value))
            .orElse(currentValue)
);
currentValue = currentValue.map(it -> it + value).flatMap(result ->
    endValue.filter(end -> result < end)
            .isPresent() ? Optional.of(result) : currentValue
);