mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix code
This commit is contained in:
parent
e7370ef77c
commit
a16b0ed025
@ -167,12 +167,14 @@ public class Opt<T> {
|
||||
* }</pre>
|
||||
*
|
||||
* @param action 你想要执行的操作
|
||||
* @return this
|
||||
* @throws NullPointerException 如果包裹里的值存在,但你传入的操作为{@code null}时抛出
|
||||
*/
|
||||
public void ifPresent(Consumer<? super T> action) {
|
||||
if (value != null) {
|
||||
public Opt<T> ifPresent(Consumer<? super T> action) {
|
||||
if (isPresent()) {
|
||||
action.accept(value);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -187,16 +189,16 @@ public class Opt<T> {
|
||||
*
|
||||
* @param action 包裹里的值存在时的操作
|
||||
* @param emptyAction 包裹里的值不存在时的操作
|
||||
* @return this;
|
||||
* @throws NullPointerException 如果包裹里的值存在时,执行的操作为 {@code null}, 或者包裹里的值不存在时的操作为 {@code null},则抛出{@code NPE}
|
||||
*/
|
||||
public Opt<T> ifPresentOrElse(Consumer<? super T> action, VoidFunc0 emptyAction) {
|
||||
if (isPresent()) {
|
||||
action.accept(value);
|
||||
return ofNullable(value);
|
||||
} else {
|
||||
emptyAction.callWithRuntimeException();
|
||||
return empty();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@ -459,10 +461,11 @@ public class Opt<T> {
|
||||
|
||||
/**
|
||||
* 转换为 {@link Optional}对象
|
||||
*
|
||||
* @return {@link Optional}对象
|
||||
* @since 5.7.16
|
||||
*/
|
||||
public Optional<T> toOptional(){
|
||||
public Optional<T> toOptional() {
|
||||
return Optional.ofNullable(this.value);
|
||||
}
|
||||
|
||||
|
@ -149,7 +149,7 @@ public class OptTest {
|
||||
|
||||
@Test
|
||||
public void ofEmptyAbleTest() {
|
||||
// 以前,输入一个CollectionUtil感觉要命,类似前缀的类一大堆,代码补全形同虚设(在项目中起码要输入完CollectionU才能在第一个调出这个函数)
|
||||
// 以前,输入一个CollectionUtil感觉要命,类似前缀的类一大堆,代码补全形同虚设(在项目中起码要输入完CollectionUtil才能在第一个调出这个函数)
|
||||
// 关键它还很常用,判空和判空集合真的太常用了...
|
||||
List<String> past = Opt.ofNullable(Collections.<String>emptyList()).filter(CollectionUtil::isNotEmpty).orElseGet(() -> Collections.singletonList("hutool"));
|
||||
// 现在,一个ofEmptyAble搞定
|
||||
@ -165,6 +165,7 @@ public class OptTest {
|
||||
Assert.assertEquals("HUTOOL", hutool);
|
||||
}
|
||||
|
||||
@SuppressWarnings({"MismatchedQueryAndUpdateOfCollection", "ConstantConditions"})
|
||||
@Test
|
||||
public void execTest() {
|
||||
// 有一些资深的程序员跟我说你这个lambda,双冒号语法糖看不懂...
|
||||
|
Loading…
x
Reference in New Issue
Block a user