使用Gradle: com.google.guava:guava:16.0.1 jar文件
通过com.google.common.collect.Iterables和com.google.common.base.Predicate功能实现简单条件查询过滤
完整代码:
/**
* query monitor zknodes list* @param currentPageNum
* @param pageSize
* @param nodeName
* @return
*/
@RequestMapping (value = "/list" , method = RequestMethod. GET )
public ResponseEntity<PageableRetVO<List<MonitorNodesParam>>> getMonitorZnodeList(
@RequestParam (value = "currentPageNum" , required = false ) Integer currentPageNum,
@RequestParam (value = "pageSize" , required = false ) Integer pageSize,
@RequestParam (value = "nodeName" , required = false ) String nodeName){
final String nodeNameQry = nodeName;
PageableRetVO<List<MonitorNodesParam>> pageableRetVO = new PageableRetVO<>();
try {
createConnectZk();
List<MonitorNodesParam> znodes = zkFacade .readConfigFromZk();
// 1.filter
List<MonitorNodesParam> filteredList = Lists. newArrayList ();
if (StringUtils. isNotEmpty (nodeNameQry)){
Iterable<MonitorNodesParam> filteredIter = Iterables. filter (znodes, new Predicate<MonitorNodesParam>() {
@Override
public boolean apply(MonitorNodesParam input) {
if (StringUtils. isNotEmpty ( nodeNameQry ) && input.getNodeName().contains( nodeNameQry )) {
return true ;
} else {
return false ;
}
}
});
for (MonitorNodesParam filtered:filteredIter){
filteredList.add(filtered);
}
} else {
filteredList = znodes;
}
// 2.page
int totalCount = filteredList.size();
pageableRetVO.setTotalCount(totalCount);
pageableRetVO.setPageNum((currentPageNum== null ? 0 :currentPageNum) > 0 ? currentPageNum : 1 );
pageableRetVO.setPageSize((pageSize == null ? 0 : pageSize) <= 0 ? 20 : pageSize);
int totalPagesNum = BaseUtils. getPagesNum (filteredList.size(), pageableRetVO.getPageSize());
if (pageableRetVO.getPageNum()<=totalPagesNum){
filteredList = filteredList.subList((pageableRetVO.getPageNum()- 1 ) * pageableRetVO.getPageSize(), Math. min (filteredList.size(),(pageableRetVO.getPageNum())*pageableRetVO.getPageSize()));
pageableRetVO.setList(filteredList);
} else {
pageableRetVO.setList( new ArrayList<MonitorNodesParam>());
}
pageableRetVO.setRetCode( 0 );
} catch (Exception e) {
logger .error(e.getMessage());
pageableRetVO.setRetCode(- 1 );
pageableRetVO.setErrorMsg(e.getMessage());
}
return new ResponseEntity<>(pageableRetVO, HttpStatus. OK );
}