过滤特定 cookie 的 cookie 数组并获取 Java 中特定 cookie 的值
Filter Array of cookies for a specific cookie and get value of a specific cookie in Java
我正在解决遗留模块中的一些问题并遇到这段代码,它从可以 return 为空的请求对象获取 cookie 数组。该代码查找特定的 cookie,如果存在则 returns cookie 值,否则 returns null。下面是现有的代码
final Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
final Cookie cookie = cookies[i];
if ("random cookie".equals(cookie.getName())) {
return cookie.getValue();
}
}
return null;
}
我把它重构成这样
final Cookie[] cookies = request.getCookies();
if (cookies != null) {
Optional<Cookie> cookie = Arrays.stream(cookies).
filter(e -> "random cookie".equals(e.getName())).
findAny();
return cookie.isPresent() ? cookie.get().getValue() : null;
}
return null;
以上可以用更好的方式重构吗?
我会将其重构为 returns Optional<String>
:
的方法
public Optional<String> getCookieValue(String name) {
final Cookie[] cookies = request.getCookies();
if(cookies == null) return Optional.empty();
return Arrays.stream(cookies)
.filter(e -> name.equals(e.getName()))
.findAny().map(Cookie::getValue);
}
然后此方法的调用者将根据他们打算如何使用结果来执行以下任一操作:
getCookieValue("random cookie").ifPresent(e -> { ... });
getCookieValue("random cookie").orElse(null);
....
....
return Optional<String>
避免处理 无效 并让此方法的用户决定在 "no value case" 中做什么。这对于 API 等的用户来说也更好读......
但是,如果您希望坚持使用当前的方法签名,那么您至少可以将其改进为:
final Cookie[] cookies = request.getCookies();
if(cookies == null) return null; // avoids if blocks
return Arrays.stream(cookies)
.filter(e -> "random cookie".equals(e.getName()))
.findAny()
.map(Cookie::getValue)
.orElse(null);
最好使方法 return Optional<String>
- 但如果你不能那么这个怎么样:
final Cookie[] maybeCookies = request.getCookies();
return Optional.ofNullable(maybeCookies)
.flatMap(cookies -> Arrays.stream(cookies)
//Find the cookie if we can.
.filter(e->"random cookie".equals(cookie.getName()))
.findAny()
)
//If we have a matching cookie, return its value.
.map(e->e.getValue())
//otherwise return null to retain original behaviour
.orElse(null);
我正在解决遗留模块中的一些问题并遇到这段代码,它从可以 return 为空的请求对象获取 cookie 数组。该代码查找特定的 cookie,如果存在则 returns cookie 值,否则 returns null。下面是现有的代码
final Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
final Cookie cookie = cookies[i];
if ("random cookie".equals(cookie.getName())) {
return cookie.getValue();
}
}
return null;
}
我把它重构成这样
final Cookie[] cookies = request.getCookies();
if (cookies != null) {
Optional<Cookie> cookie = Arrays.stream(cookies).
filter(e -> "random cookie".equals(e.getName())).
findAny();
return cookie.isPresent() ? cookie.get().getValue() : null;
}
return null;
以上可以用更好的方式重构吗?
我会将其重构为 returns Optional<String>
:
public Optional<String> getCookieValue(String name) {
final Cookie[] cookies = request.getCookies();
if(cookies == null) return Optional.empty();
return Arrays.stream(cookies)
.filter(e -> name.equals(e.getName()))
.findAny().map(Cookie::getValue);
}
然后此方法的调用者将根据他们打算如何使用结果来执行以下任一操作:
getCookieValue("random cookie").ifPresent(e -> { ... });
getCookieValue("random cookie").orElse(null);
....
....
return Optional<String>
避免处理 无效 并让此方法的用户决定在 "no value case" 中做什么。这对于 API 等的用户来说也更好读......
但是,如果您希望坚持使用当前的方法签名,那么您至少可以将其改进为:
final Cookie[] cookies = request.getCookies();
if(cookies == null) return null; // avoids if blocks
return Arrays.stream(cookies)
.filter(e -> "random cookie".equals(e.getName()))
.findAny()
.map(Cookie::getValue)
.orElse(null);
最好使方法 return Optional<String>
- 但如果你不能那么这个怎么样:
final Cookie[] maybeCookies = request.getCookies();
return Optional.ofNullable(maybeCookies)
.flatMap(cookies -> Arrays.stream(cookies)
//Find the cookie if we can.
.filter(e->"random cookie".equals(cookie.getName()))
.findAny()
)
//If we have a matching cookie, return its value.
.map(e->e.getValue())
//otherwise return null to retain original behaviour
.orElse(null);