virajjasani commented on a change in pull request #673: HBASE-23093 : Avoid
Optional Anti-Pattern where possible
URL: https://github.com/apache/hbase/pull/673#discussion_r333509427
##########
File path:
hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsRegionServerWrapperImpl.java
##########
@@ -275,7 +276,7 @@ public int getFlushQueueSize() {
@Override
public long getBlockCacheCount() {
- return this.blockCache.map(BlockCache::getBlockCount).orElse(0L);
+ return this.blockCache != null ? this.blockCache.getBlockCount() : 0L;
Review comment:
Advantages with this patch:
1. we can avoid using Optional field (minor impact since we are not
serializing this class)
2. we can unwrap only one time during initialization and then use simple
null check. Otherwise Optional.map() anyways does Optional.of for all of these
getter methods:
```
public<U> Optional<U> map(Function<? super T, ? extends U> mapper) {
Objects.requireNonNull(mapper);
if (!isPresent())
return empty();
else {
return Optional.ofNullable(mapper.apply(value));
}
}
```
Hence, isn't it better to have null check rather than going into this mapper
function to apply within Optional.of path?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services