/
FilterThingPredicateVisitor.java
72 lines (58 loc) · 2.44 KB
/
FilterThingPredicateVisitor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* Copyright (c) 2017 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* Contributors:
* Bosch Software Innovations GmbH - initial contribution
*/
package org.eclipse.ditto.model.query.things;
import java.util.function.Function;
import java.util.function.Predicate;
import org.eclipse.ditto.model.things.Thing;
import org.eclipse.ditto.model.query.model.expression.FilterFieldExpression;
import org.eclipse.ditto.model.query.model.expression.visitors.FilterFieldExpressionVisitor;
/**
* FilterFieldExpressionVisitor for Java {@link Predicate}s of {@link Thing}s.
*/
public final class FilterThingPredicateVisitor implements FilterFieldExpressionVisitor<Predicate<Thing>> {
private final Function<String, Predicate<Thing>> predicateFunction;
private FilterThingPredicateVisitor(final Function<String, Predicate<Thing>> predicateFunction) {
this.predicateFunction = predicateFunction;
}
public static Predicate<Thing> apply(final FilterFieldExpression expression,
final Function<String, Predicate<Thing>> predicateFunction) {
return expression.acceptFilterVisitor(new FilterThingPredicateVisitor(predicateFunction));
}
@Override
public Predicate<Thing> visitAttribute(final String key) {
return predicateFunction.apply("/attributes/" + key);
}
@Override
public Predicate<Thing> visitFeatureIdProperty(final String featureId, final String property) {
return predicateFunction.apply("/features/" + featureId + "/properties/" + property);
}
@Override
public Predicate<Thing> visitFeatureProperty(final String property) {
return thing -> thing.getFeatures()
.map(features -> features.stream()
.anyMatch(feature -> feature.getProperty(property).isPresent())
)
.orElse(false);
}
@Override
public Predicate<Thing> visitSimple(final String fieldName) {
return predicateFunction.apply(fieldName);
}
@Override
public Predicate<Thing> visitAcl() {
return predicateFunction.apply("/acl");
}
@Override
public Predicate<Thing> visitGlobalReads() {
return thing -> true; // TODO what about them?
}
}