mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
commit
92afc0b757
@ -122,7 +122,7 @@ public class Opt<T> {
|
|||||||
try {
|
try {
|
||||||
return Opt.ofNullable(supplier.call());
|
return Opt.ofNullable(supplier.call());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
final Opt<T> empty = Opt.empty();
|
final Opt<T> empty = new Opt<>(null);
|
||||||
empty.exception = e;
|
empty.exception = e;
|
||||||
return empty;
|
return empty;
|
||||||
}
|
}
|
||||||
|
@ -187,6 +187,21 @@ public class OptTest {
|
|||||||
Assert.assertEquals(indexOut, indexOutSituation);
|
Assert.assertEquals(indexOut, indexOutSituation);
|
||||||
Assert.assertEquals("hutool", npe);
|
Assert.assertEquals("hutool", npe);
|
||||||
Assert.assertEquals("hutool", indexOut);
|
Assert.assertEquals("hutool", indexOut);
|
||||||
|
|
||||||
|
// 多线程下情况测试
|
||||||
|
Stream.iterate(0, i -> ++i).limit(20000).parallel().forEach(i -> {
|
||||||
|
Opt<Object> opt = Opt.ofTry(() -> {
|
||||||
|
if (i % 2 == 0) {
|
||||||
|
throw new IllegalStateException(i + "");
|
||||||
|
} else {
|
||||||
|
throw new NullPointerException(i + "");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Assert.assertTrue(
|
||||||
|
(i % 2 == 0 && opt.getException() instanceof IllegalStateException) ||
|
||||||
|
(i % 2 != 0 && opt.getException() instanceof NullPointerException)
|
||||||
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
|
Loading…
x
Reference in New Issue
Block a user