创建map同时赋值
1.创建map同时赋值
依赖于hutool工具类
- key是Predicate接口,处理参数类型为QuerySlowLogRecordParam的接口
- value是BiConsumer接口,处理参数类型为
private Map<Predicate<QuerySlowLogRecordParam>, BiConsumer<BoolQueryBuilder,QuerySlowLogRecordParam>> queryConditions = MapUtil.builder(new LinkedHashMap<Predicate<QuerySlowLogRecordParam>, BiConsumer<BoolQueryBuilder,QuerySlowLogRecordParam>>())
.put(param -> CollectionUtil.isNotEmpty(param.getResourceIds()), (boolQueryBuilder, param) -> boolQueryBuilder.must(QueryBuilders.termsQuery("resourceId", param.getResourceIds())))
.put(param -> !Objects.isNull(param.getLatency()), (boolQueryBuilder, param) -> boolQueryBuilder.must(QueryBuilders.rangeQuery("latency").gt(param.getLatency())))
.put(param -> Objects.isNull(param.getAlarmFrom()) || Objects.isNull(param.getAlarmTo()), (boolQueryBuilder, param) -> {
Long to = System.currentTimeMillis();
Long from = to - 15 * 60 * 1000;
param.setAlarmFrom(from);
param.setAlarmTo(to);
})
.put(param -> !Objects.isNull(param.getQueryId()), (boolQueryBuilder, param) -> boolQueryBuilder.must(QueryBuilders.termQuery("id", param.getQueryId())))
.build();
2.创建map同时赋值
依赖于guava
private static final Map<String, String> DEFAULT_HEADERS = ImmutableMap.of(
"token", AppHealthConfig.getToken(),
"Content-Type", "application/json"
);
原创:做时间的朋友