mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
!1142 修复ofTry调用map后再调用ifFail失效问题,添加toEasyStream方法
Merge pull request !1142 from 孔皮皮/v6-dev-opt
This commit is contained in:
commit
58e8a90407
@ -73,8 +73,7 @@ public class Opt<T> {
|
|||||||
* @return 一个包裹里元素可能为空的 {@code Opt}
|
* @return 一个包裹里元素可能为空的 {@code Opt}
|
||||||
*/
|
*/
|
||||||
public static <T> Opt<T> ofNullable(final T value) {
|
public static <T> Opt<T> ofNullable(final T value) {
|
||||||
return value == null ? empty()
|
return value == null ? empty() : new Opt<>(value);
|
||||||
: new Opt<>(value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -108,7 +107,7 @@ public class Opt<T> {
|
|||||||
*/
|
*/
|
||||||
public static <T> Opt<T> ofTry(final SerSupplier<T> supplier) {
|
public static <T> Opt<T> ofTry(final SerSupplier<T> supplier) {
|
||||||
try {
|
try {
|
||||||
return Opt.ofNullable(supplier.getting());
|
return ofNullable(supplier.getting());
|
||||||
} catch (final Throwable e) {
|
} catch (final Throwable e) {
|
||||||
final Opt<T> empty = new Opt<>(null);
|
final Opt<T> empty = new Opt<>(null);
|
||||||
empty.throwable = e;
|
empty.throwable = e;
|
||||||
@ -276,7 +275,7 @@ public class Opt<T> {
|
|||||||
*/
|
*/
|
||||||
public Opt<T> filter(final Predicate<? super T> predicate) {
|
public Opt<T> filter(final Predicate<? super T> predicate) {
|
||||||
Objects.requireNonNull(predicate);
|
Objects.requireNonNull(predicate);
|
||||||
if (isEmpty()) {
|
if (isEmpty() || isFail()) {
|
||||||
return this;
|
return this;
|
||||||
} else {
|
} else {
|
||||||
return predicate.test(value) ? this : empty();
|
return predicate.test(value) ? this : empty();
|
||||||
@ -293,9 +292,12 @@ public class Opt<T> {
|
|||||||
* 如果不存在,返回一个空的{@code Opt}
|
* 如果不存在,返回一个空的{@code Opt}
|
||||||
* @throws NullPointerException 如果给定的操作为 {@code null},抛出 {@code NPE}
|
* @throws NullPointerException 如果给定的操作为 {@code null},抛出 {@code NPE}
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public <U> Opt<U> map(final Function<? super T, ? extends U> mapper) {
|
public <U> Opt<U> map(final Function<? super T, ? extends U> mapper) {
|
||||||
Objects.requireNonNull(mapper);
|
Objects.requireNonNull(mapper);
|
||||||
if (isEmpty()) {
|
if (isFail()) {
|
||||||
|
return (Opt<U>) this;
|
||||||
|
} else if (isEmpty()) {
|
||||||
return empty();
|
return empty();
|
||||||
} else {
|
} else {
|
||||||
return Opt.ofNullable(mapper.apply(value));
|
return Opt.ofNullable(mapper.apply(value));
|
||||||
@ -313,9 +315,12 @@ public class Opt<T> {
|
|||||||
* 如果不存在,返回一个空的{@code Opt}
|
* 如果不存在,返回一个空的{@code Opt}
|
||||||
* @throws NullPointerException 如果给定的操作为 {@code null}或者给定的操作执行结果为 {@code null},抛出 {@code NPE}
|
* @throws NullPointerException 如果给定的操作为 {@code null}或者给定的操作执行结果为 {@code null},抛出 {@code NPE}
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public <U> Opt<U> flatMap(final Function<? super T, ? extends Opt<? extends U>> mapper) {
|
public <U> Opt<U> flatMap(final Function<? super T, ? extends Opt<? extends U>> mapper) {
|
||||||
Objects.requireNonNull(mapper);
|
Objects.requireNonNull(mapper);
|
||||||
if (isEmpty()) {
|
if (isFail()) {
|
||||||
|
return (Opt<U>) this;
|
||||||
|
} else if (isEmpty()) {
|
||||||
return empty();
|
return empty();
|
||||||
} else {
|
} else {
|
||||||
@SuppressWarnings("unchecked") final Opt<U> r = (Opt<U>) mapper.apply(value);
|
@SuppressWarnings("unchecked") final Opt<U> r = (Opt<U>) mapper.apply(value);
|
||||||
@ -336,9 +341,12 @@ public class Opt<T> {
|
|||||||
* @see Optional#flatMap(Function)
|
* @see Optional#flatMap(Function)
|
||||||
* @since 5.7.16
|
* @since 5.7.16
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public <U> Opt<U> flattedMap(final Function<? super T, ? extends Optional<? extends U>> mapper) {
|
public <U> Opt<U> flattedMap(final Function<? super T, ? extends Optional<? extends U>> mapper) {
|
||||||
Objects.requireNonNull(mapper);
|
Objects.requireNonNull(mapper);
|
||||||
if (isEmpty()) {
|
if (isFail()) {
|
||||||
|
return (Opt<U>) this;
|
||||||
|
} else if (isEmpty()) {
|
||||||
return empty();
|
return empty();
|
||||||
} else {
|
} else {
|
||||||
return ofNullable(mapper.apply(value).orElse(null));
|
return ofNullable(mapper.apply(value).orElse(null));
|
||||||
@ -456,7 +464,7 @@ public class Opt<T> {
|
|||||||
* @throws NullPointerException 如果之不存在,并且传入的操作为空,则抛出 {@code NPE}
|
* @throws NullPointerException 如果之不存在,并且传入的操作为空,则抛出 {@code NPE}
|
||||||
*/
|
*/
|
||||||
public Opt<T> orElseOpt(final Supplier<? extends T> supplier) {
|
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
|
* @since 5.7.16
|
||||||
*/
|
*/
|
||||||
public Optional<T> toOptional() {
|
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
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return StrUtil.toStringOrNull(this.value);
|
return StrUtil.toStringOrNull(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,17 +12,24 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.core.lang;
|
package org.dromara.hutool.core.lang;
|
||||||
|
|
||||||
import org.dromara.hutool.core.collection.CollUtil;
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
import org.dromara.hutool.core.collection.CollUtil;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Disabled;
|
import org.junit.jupiter.api.Disabled;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import javax.management.monitor.MonitorSettingException;
|
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;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -234,4 +241,25 @@ public class OptTest {
|
|||||||
.ifFail(Console::log, NullPointerException.class, MonitorSettingException.class)
|
.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});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user