!1142 修复ofTry调用map后再调用ifFail失效问题,添加toEasyStream方法

Merge pull request !1142 from 孔皮皮/v6-dev-opt
This commit is contained in:
Looly 2023-12-28 13:24:23 +00:00 committed by Gitee
commit 58e8a90407
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 57 additions and 12 deletions

View File

@ -73,8 +73,7 @@ public class Opt<T> {
* @return 一个包裹里元素可能为空的 {@code Opt}
*/
public static <T> Opt<T> ofNullable(final T value) {
return value == null ? empty()
: new Opt<>(value);
return value == null ? empty() : new Opt<>(value);
}
/**
@ -108,7 +107,7 @@ public class Opt<T> {
*/
public static <T> Opt<T> ofTry(final SerSupplier<T> supplier) {
try {
return Opt.ofNullable(supplier.getting());
return ofNullable(supplier.getting());
} catch (final Throwable e) {
final Opt<T> empty = new Opt<>(null);
empty.throwable = e;
@ -276,7 +275,7 @@ public class Opt<T> {
*/
public Opt<T> filter(final Predicate<? super T> predicate) {
Objects.requireNonNull(predicate);
if (isEmpty()) {
if (isEmpty() || isFail()) {
return this;
} else {
return predicate.test(value) ? this : empty();
@ -293,9 +292,12 @@ public class Opt<T> {
* 如果不存在返回一个空的{@code Opt}
* @throws NullPointerException 如果给定的操作为 {@code null}抛出 {@code NPE}
*/
@SuppressWarnings("unchecked")
public <U> Opt<U> map(final Function<? super T, ? extends U> mapper) {
Objects.requireNonNull(mapper);
if (isEmpty()) {
if (isFail()) {
return (Opt<U>) this;
} else if (isEmpty()) {
return empty();
} else {
return Opt.ofNullable(mapper.apply(value));
@ -313,9 +315,12 @@ public class Opt<T> {
* 如果不存在返回一个空的{@code Opt}
* @throws NullPointerException 如果给定的操作为 {@code null}或者给定的操作执行结果为 {@code null}抛出 {@code NPE}
*/
@SuppressWarnings("unchecked")
public <U> Opt<U> flatMap(final Function<? super T, ? extends Opt<? extends U>> mapper) {
Objects.requireNonNull(mapper);
if (isEmpty()) {
if (isFail()) {
return (Opt<U>) this;
} else if (isEmpty()) {
return empty();
} else {
@SuppressWarnings("unchecked") final Opt<U> r = (Opt<U>) mapper.apply(value);
@ -336,9 +341,12 @@ public class Opt<T> {
* @see Optional#flatMap(Function)
* @since 5.7.16
*/
@SuppressWarnings("unchecked")
public <U> Opt<U> flattedMap(final Function<? super T, ? extends Optional<? extends U>> mapper) {
Objects.requireNonNull(mapper);
if (isEmpty()) {
if (isFail()) {
return (Opt<U>) this;
} else if (isEmpty()) {
return empty();
} else {
return ofNullable(mapper.apply(value).orElse(null));
@ -456,7 +464,7 @@ public class Opt<T> {
* @throws NullPointerException 如果之不存在并且传入的操作为空则抛出 {@code NPE}
*/
public Opt<T> orElseOpt(final Supplier<? extends T> supplier) {
return or(() -> Opt.ofNullable(supplier.get()));
return or(() -> ofNullable(supplier.get()));
}
/**
@ -510,7 +518,16 @@ public class Opt<T> {
* @since 5.7.16
*/
public Optional<T> toOptional() {
return Optional.ofNullable(this.value);
return Optional.ofNullable(value);
}
/**
* 转换为 {@link EasyStream}对象
*
* @return {@link EasyStream}对象
*/
public EasyStream<T> toEasyStream() {
return EasyStream.of(value);
}
/**
@ -558,6 +575,6 @@ public class Opt<T> {
*/
@Override
public String toString() {
return StrUtil.toStringOrNull(this.value);
return StrUtil.toStringOrNull(value);
}
}

View File

@ -12,17 +12,24 @@
package org.dromara.hutool.core.lang;
import org.dromara.hutool.core.collection.CollUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.dromara.hutool.core.collection.CollUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import javax.management.monitor.MonitorSettingException;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.stream.Stream;
/**
@ -234,4 +241,25 @@ public class OptTest {
.ifFail(Console::log, NullPointerException.class, MonitorSettingException.class)
;
}
@SuppressWarnings({"NumericOverflow", "divzero"})
@Test
@Disabled
void testFail1() {
final Integer i = Opt.ofTry(() -> 1 / 0)
.map(e -> 666)
.ifFail(Console::log)
.orElseGet(() -> 1);
Assertions.assertEquals(i, 1);
}
@SuppressWarnings({"NumericOverflow", "divzero"})
@Test
@Disabled
void testToEasyStream() {
final List<Integer> list = Opt.ofTry(() -> 1).toEasyStream().toList();
Assertions.assertArrayEquals(list.toArray(), new Integer[]{1});
}
}