这个在java 8中使用Optionals的例子是正确的吗?你会怎么改写它?
作者:互联网
我已经开始使用Java的8个Optionals.我想分享这个方法,它是“代码味道”的例子,我想用java 8和optionlas以及功能(声明)样式重写它,所以我有兴趣看到你的意见.我们考虑一下这个方法:
public boolean isTokenValid(String userAgent, Optional<String> apiKey) {
LOGGER.info(String.format("userAgent : %s, Key ?: %s", userAgent, apiKey.isPresent()));
if ("ALFA".equalsIgnoreCase(userAgent)){
return (apiKey != null && apiKey.isPresent()
&& ispropertyExists(ALFA_TYPE, apiKey.get()));
}
else {
return (apiKey != null && apiKey.isPresent()
&& ispropertyExists(BETA_TYPE, apiKey.get()));
}
}
其中“ispropertyExists”返回布尔类型,“ALFA_TYPE”和“OMEGA_TYPE”是枚举常量.
所以下面是我重写这种方法的方式,旨在提高可读性和实践功能性思维方式.我添加了评论,解释了我的想法和原因,所以,如果你认为你能够改进它,我感谢你的意见和你的方法的例子.
/**
* We should not pass Optionals as a parameters to the methods. We
* should use Optionals only for return value when we are not sure if value will
* be presented at the end of the calculations or not.
*/
public boolean isTokenValid(String userAgent, String apiKey) {
LOGGER.info(String.format("userAgent : %s, Key ?: %s", userAgent, apiKey));
/**
* If apiKey is null then it is incorrect. And execution will stop after
* Optional.ofNullable(apiKey), since monad will be having null value. If apiKey
* is not null then we still want to filter out empty strings. If after filter
* there will be no value, then execution will stop.
* If we have some value for apiKey then it is ok and we map the monad to the
* userAgent value to proceed the chain of calls on monad.
*/
Optional<String> userAgentOptional = Optional.ofNullable(apiKey).filter(StringUtils::isNotBlank)
.map(ak -> userAgent);
/**
* We map "userAgent" value to boolean (if it is a alfa or not). Then
* we map that boolean to boolean value which represents security check in db
* itself.
*/
Optional<Boolean> isValid = userAgentOptional.map(ua -> "ALFA".equalsIgnoreCase(ua))
.map(isAlfa -> isAlfa ? ispropertyExists(ALFA_TYPE, apiKey)
: ispropertyExists(BETA_TYPE, apiKey));
/**
* And all in all we get value from our optional boolean. If "somehow" it is
* ended up to be empty, then we retrun "false", if it is not empty, then the
* value will itself be returned.
*/
return isValid.orElse(false);
}
谢谢.
解决方法:
我将所有操作组合在一个链式语句中,并返回结果,避免不必要的可选变量.
return Optional.ofNullable(apiKey)
.filter(StringUtils::isNotBlank)
.map(ak -> userAgent)
.map("ALFA"::equalsIgnoreCase)
.map(isAlfa -> isAlfa ? ALFA_TYPE : BETA_TYPE)
.map(type -> ispropertyExists(type, apiKey))
.orElse(false);
标签:java,java-8,java-stream,monads,optional 来源: https://codeday.me/bug/20190724/1521070.html