Opt get change to getOrNull

This commit is contained in:
Looly
2024-08-20 18:33:07 +08:00
parent 780029c032
commit 9ee9fb8866
13 changed files with 33 additions and 33 deletions

View File

@@ -53,10 +53,10 @@ public class OptTest {
}
@Test
public void getTest() {
public void getOrNullTest() {
// 和原版Optional有区别的是get不会抛出NoSuchElementException
// 如果想使用原版Optional中的get这样获取一个一定不为空的值则应该使用orElseThrow
final Object opt = Opt.ofNullable(null).get();
final Object opt = Opt.ofNullable(null).getOrNull();
Assertions.assertNull(opt);
}
@@ -77,7 +77,7 @@ public class OptTest {
Assertions.assertEquals("hutool", user.getUsername());
// 注意传入的lambda中对包裹内的元素执行赋值操作并不会影响到原来的元素
final String name = Opt.ofNullable("hutool").ifPresent(username -> username = "123").ifPresent(username -> username = "456").get();
final String name = Opt.ofNullable("hutool").ifPresent(username -> username = "123").ifPresent(username -> username = "456").getOrNull();
Assertions.assertEquals("hutool", name);
}
@@ -99,7 +99,7 @@ public class OptTest {
// 这也是为什么我们需要getter和setter而不直接给bean中的属性赋值中的其中一个原因
final String name = Opt.ofNullable("hutool").ifPresents(
username -> username = "123", username -> username = "456",
n -> Assertions.assertEquals("hutool", n)).get();
n -> Assertions.assertEquals("hutool", n)).getOrNull();
Assertions.assertEquals("hutool", name);
// 当然以下情况不会抛出NPE但也没什么意义
@@ -119,7 +119,7 @@ public class OptTest {
final User user = User.builder().username("hutool").build();
final Opt<User> userOpt = Opt.of(user);
// 获取昵称,获取不到则获取用户名
final String name = userOpt.map(User::getNickname).or(() -> userOpt.map(User::getUsername)).get();
final String name = userOpt.map(User::getNickname).or(() -> userOpt.map(User::getUsername)).getOrNull();
Assertions.assertEquals("hutool", name);
}

View File

@@ -84,7 +84,7 @@ abstract class BaseMutableTest<V, M extends Mutable<V>> {
@Test
void testToOpt() {
final Mutable<V> mutableObj = getMutable(getValue1());
final V value = mutableObj.toOpt().get();
final V value = mutableObj.toOpt().getOrNull();
Assertions.assertEquals(getValue1(), value);
}

View File

@@ -548,19 +548,19 @@ public class EasyStreamTest {
final Opt<BigDecimal> bigDecimalAvgFullParam = EasyStream.of(1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10)
.map(NumberUtil::toBigDecimal)
.avg(Function.identity(), 2, RoundingMode.HALF_UP);
Assertions.assertEquals(NumberUtil.toBigDecimal(5.96), bigDecimalAvgFullParam.get());
Assertions.assertEquals(NumberUtil.toBigDecimal(5.96), bigDecimalAvgFullParam.getOrNull());
//测试bigDecimal的avg单参
final Opt<BigDecimal> bigDecimalAvgOneParam = EasyStream.of(1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10)
.map(NumberUtil::toBigDecimal)
.avg(Function.identity());
Assertions.assertEquals(NumberUtil.toBigDecimal(5.96), bigDecimalAvgOneParam.get());
Assertions.assertEquals(NumberUtil.toBigDecimal(5.96), bigDecimalAvgOneParam.getOrNull());
//测试bigDecimal的avg双参
final Opt<BigDecimal> bigDecimalAvgTwoParam = EasyStream.of(1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10)
.map(NumberUtil::toBigDecimal)
.avg(Function.identity(), 2);
Assertions.assertEquals(NumberUtil.toBigDecimal(5.96), bigDecimalAvgTwoParam.get());
Assertions.assertEquals(NumberUtil.toBigDecimal(5.96), bigDecimalAvgTwoParam.getOrNull());
//测试bigDecimal的avg 空元素
final Opt<BigDecimal> emptyBigDecimalAvg = EasyStream.of(bigDecimalEmptyList)
@@ -585,7 +585,7 @@ public class EasyStreamTest {
final BigDecimal sum = EasyStream.of(testList).sum(BigDecimalTest::getCount);
Assertions.assertEquals(15, sum.intValue());
final BigDecimal avg = EasyStream.of(testList).avg(BigDecimalTest::getCount).get();
final BigDecimal avg = EasyStream.of(testList).avg(BigDecimalTest::getCount).getOrNull();
Assertions.assertEquals(3, avg.intValue());
}