Sign in
Sign up
Explore
Enterprise
Education
Search
Help
Terms of use
About Us
Explore
Enterprise
Education
Gitee Premium
Gitee AI
AI teammates
Sign in
Sign up
Fetch the repository succeeded.
Open Source
>
Development Lib
>
Common Toolkit
&&
Donate
Please sign in before you donate.
Cancel
Sign in
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
Watch
Unwatch
Watching
Releases Only
Ignoring
4K
Star
24.3K
Fork
8.3K
GVP
chinabugotech
/
hutool
Code
Issues
850
Pull Requests
1
Insights
Pipelines
Service
JavaDoc
Quality Analysis
Jenkins for Gitee
Tencent CloudBase
Tencent Cloud Serverless
悬镜安全
Aliyun SAE
Codeblitz
SBOM
Don’t show this again
426
关于即将被废弃的OptionalBean,改为Opt
Merged
阿超:v5-dev
chinabugotech:v5-dev
阿超
create on 2021-10-01 00:33
Clone/Download
HTTPS
SSH
Copy
Email Patch
Diff file
#### 说明 - 老实话,`Optional`一直是我非常常用的一个类,使用函数式编程,将原本多行代码,缩略成一行,极大简洁了代码 - 但在经常使用中,发现有时候原生的`Optional`并不能完全满足我的需求,在`Hutool`中找,发现确实有一个`OptionalBean`,但其已经被废弃了,因此才有了这个类 - 通过对`JDK16`的学习,研究了`Optional`的新特性,并已经在开发中经常使用基于`Optional`复制后拓展的类 - 讲老实话,经常使用`Stream`,有些方法的返回值为`Optional`,我本是想直接在`Optional`进行继承并拓展,无奈它是一个`final`类 - 我想,就这样眼睁睁看着`Hutool`中的`OptionalBean`被废弃,不如让它梅开二度,在`Hutool`的新版本迎来第二春! ### 修改描述(包括说明bug修复或者添加新特性) 1. [bug修复] balabala…… 2. [新特性] - 对原生Optional的使用区别不大,学习成本低 - 注释全都汉化并带理解(累瘫) - 相比与Optional的新特性都包含在测试用例中 - ofBlankAble函数基于ofNullable的逻辑下,额外进行了空字符串判断 ``` // ofBlankAble相对于ofNullable考虑了字符串为空串的情况 String hutool = OptionalBean.ofBlankAble("").orElse("hutool"); Assert.assertEquals("hutool", hutool); ``` - 原版Optional有区别的是,get不会抛出NoSuchElementException - 如果想使用原版Optional中的get这样,获取一个一定不为空的值,则应该使用orElseThrow ``` // 和原版Optional有区别的是,get不会抛出NoSuchElementException // 如果想使用原版Optional中的get这样,获取一个一定不为空的值,则应该使用orElseThrow Object opt = OptionalBean.ofNullable(null).get(); Assert.assertNull(opt); ``` - 将jdk11 Optional中的新函数isEmpty,直接照搬了过来 ``` // 这是jdk11 Optional中的新函数,直接照搬了过来 // 判断包裹内元素是否为空,注意并没有判断空字符串的情况 boolean isEmpty = OptionalBean.empty().isEmpty(); Assert.assertTrue(isEmpty); ``` - 将jdk9 Optional中的新函数ifPresentOrElse,直接照搬了过来 ``` // 这是jdk9中的新函数,直接照搬了过来 // 存在就打印对应的值,不存在则用{@code System.err.println}打印另一句字符串 OptionalBean.ofNullable("Hello Hutool!").ifPresentOrElse(System.out::println, () -> System.err.println("Ops!Something is wrong!")); OptionalBean.empty().ifPresentOrElse(System.out::println, () -> System.err.println("Ops!Something is wrong!")); ``` - 新增了peek函数,相当于ifPresent的链式调用(个人常用) ``` User user = new User(); // 相当于ifPresent的链式调用 OptionalBean.ofNullable("hutool").peek(user::setUsername).peek(user::setNickname); Assert.assertEquals("hutool", user.getNickname()); Assert.assertEquals("hutool", user.getUsername()); // 注意,传入的lambda中,对包裹内的元素执行赋值操作并不会影响到原来的元素 String name = OptionalBean.ofNullable("hutool").peek(username -> username = "123").peek(username -> username = "456").get(); Assert.assertEquals("hutool", name); ``` - 将jdk9 Optional中的新函数or,直接照搬了过来 ``` // 这是jdk9 Optional中的新函数,直接照搬了过来 // 给一个替代的Opt String str = OptionalBean.<String>ofNullable(null).or(() -> OptionalBean.ofNullable("Hello hutool!")).map(String::toUpperCase).orElseThrow(); Assert.assertEquals("HELLO HUTOOL!", str); User user = User.builder().username("hutool").build(); OptionalBean<User> userOptionalBean = OptionalBean.of(user); // 获取昵称,获取不到则获取用户名 String name = userOptionalBean.map(User::getNickname).or(() -> userOptionalBean.map(User::getUsername)).get(); Assert.assertEquals("hutool", name); ``` - 对orElseThrow进行了重载,支持 双冒号+自定义提示语 写法,比原来的 ``` orElseThrow(() -> new IllegalStateException("Ops!Something is wrong!")) ``` 更加优雅,修改后写法为: ``` orElseThrow(IllegalStateException::new, "Ops!Something is wrong!") ``` 测试用例: ``` // 获取一个不可能为空的值,否则抛出NoSuchElementException异常 Object obj = OptionalBean.ofNullable(null).orElseThrow(); // 获取一个不可能为空的值,否则抛出自定义异常 Object assignException = OptionalBean.ofNullable(null).orElseThrow(IllegalStateException::new); // 获取一个不可能为空的值,否则抛出带自定义消息的自定义异常 Object exceptionWithMessage = OptionalBean.empty().orElseThrow(IllegalStateException::new, "Ops!Something is wrong!"); ```
How to merge Pull Request
git checkout v5-dev
git pull https://gitee.com/VampireAchao/hutool.git v5-dev
git push origin v5-dev
Comments
6
Commits
1
Files
2
Checks
Code problems
0
Bulk operation
Expand
Collapse
Reviewer
Code Owner
Reviewer
eli_chow
eli-chow
chinabugotech
chinabugotech
Looly
loolly_admin
No Setting
Min number
0
Tester
eli_chow
eli-chow
chinabugotech
chinabugotech
Looly
loolly_admin
No Setting
Min number
0
Priority
Not specified
Serious
Main
Secondary
Unimportant
Label
Label settings
enhancement
Link Issue
No link issue
Successfully merged pull requests will close issues
Milestone
No related milestones
Participators
(3)
Java
1
https://gitee.com/chinabugotech/hutool.git
git@gitee.com:chinabugotech/hutool.git
chinabugotech
hutool
hutool
Going to Help Center
Search
Git 命令在线学习
如何在 Gitee 导入 GitHub 仓库
Git 仓库基础操作
企业版和社区版功能对比
SSH 公钥设置
如何处理代码冲突
仓库体积过大,如何减小?
如何找回被删除的仓库数据
Gitee 产品配额说明
GitHub仓库快速导入Gitee及同步更新
什么是 Release(发行版)
将 PHP 项目自动发布到 packagist.org
Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register