use of org.openremote.model.attribute.MetaMap in project openremote by openremote.
the class AssetQueryPredicate method asPredicate.
@SuppressWarnings("unchecked")
public static Predicate<AssetState<?>> asPredicate(Supplier<Long> currentMillisProducer, LogicGroup<AttributePredicate> condition) {
if (groupIsEmpty(condition)) {
return as -> true;
}
LogicGroup.Operator operator = condition.operator == null ? LogicGroup.Operator.AND : condition.operator;
List<Predicate<AssetState<?>>> assetStatePredicates = new ArrayList<>();
if (condition.getItems().size() > 0) {
condition.getItems().stream().forEach(p -> {
assetStatePredicates.add((Predicate<AssetState<?>>) (Predicate) asPredicate(currentMillisProducer, p));
AtomicReference<Predicate<AssetState<?>>> metaPredicate = new AtomicReference<>(nameValueHolder -> true);
AtomicReference<Predicate<AssetState<?>>> oldValuePredicate = new AtomicReference<>(value -> true);
if (p.meta != null) {
final Predicate<NameValueHolder<?>> innerMetaPredicate = Arrays.stream(p.meta).map(metaPred -> asPredicate(currentMillisProducer, metaPred)).reduce(x -> true, Predicate::and);
metaPredicate.set(assetState -> {
MetaMap metaItems = ((MetaHolder) assetState).getMeta();
return metaItems.stream().anyMatch(metaItem -> innerMetaPredicate.test(assetState));
});
assetStatePredicates.add(metaPredicate.get());
}
if (p.previousValue != null) {
Predicate<Object> innerOldValuePredicate = p.previousValue.asPredicate(currentMillisProducer);
oldValuePredicate.set(nameValueHolder -> innerOldValuePredicate.test((nameValueHolder).getOldValue()));
assetStatePredicates.add(oldValuePredicate.get());
}
});
}
if (condition.groups != null && condition.groups.size() > 0) {
assetStatePredicates.addAll(condition.groups.stream().map(c -> asPredicate(currentMillisProducer, c)).collect(Collectors.toList()));
}
return asPredicate(assetStatePredicates, operator);
}
Aggregations