-
Notifications
You must be signed in to change notification settings - Fork 5
/
FiltersBuilder.java
54 lines (48 loc) · 2.54 KB
/
FiltersBuilder.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
package org.genericsystem.api.core;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
public class FiltersBuilder {
public static interface FilterGetter extends Function<Object[], Predicate<IGeneric<?>>> {
}
public static final FilterGetter NO_FILTER = params -> (x -> true);
public static final FilterGetter INSTANCES = params -> (x -> params[0].equals(x.getMeta()));
public static final FilterGetter INHERITINGS = params -> (x -> x.getSupers().contains(params[0]));
public static final FilterGetter COMPOSITES = params -> (x -> x.getComponents().contains(params[0]));
public static final FilterGetter HAS_META = params -> (x -> !x.equals(params[0]) && x.getMeta().equals(params[0]));
public static final FilterGetter HAS_SUPER = params -> (x -> x.getSupers().contains(params[0]));
public static final FilterGetter HAS_COMPONENTS = params -> (x -> filter(x.getComponents(), Arrays.asList(params)).test(x));
public static final FilterGetter COMPOSITE_HAS_COMPONENTS = params -> (x -> componentsFilter(params).test(x));
public static final FilterGetter HAS_VALUE = params -> (x -> Objects.equals(x.getValue(), params[0]));
public static final FilterGetter HAS_SUPERS = params -> (x -> params.length == 0 ? x.getSupers().isEmpty() : filter(x.getSupers(), Arrays.asList(params)).test(x));
static Predicate<IGeneric<?>> filter(List<?> ancestors, List<Object> ancestorsReached) {
return attribute -> {
List<?> attributeAncestors = new ArrayList<>(ancestors);
for (Object ancestorsReach : ancestorsReached) {
Object matchedComponent = attributeAncestors.stream().filter(attributeAncestor -> attributeAncestor.equals(ancestorsReach)).findFirst().orElse(null);
if (matchedComponent != null)
attributeAncestors.remove(matchedComponent);
else
return false;
}
return true;
};
}
@SuppressWarnings("unchecked")
static <T extends IGeneric<T>> Predicate<IGeneric<?>> componentsFilter(Object... componentsReached) {
return attribute -> {
List<T> attributeComps = new ArrayList<>(((IGeneric<T>) attribute).getComponents());
for (Object componentReach : componentsReached) {
T matchedComponent = attributeComps.stream().filter(attributeComp -> ((IGeneric<T>) componentsReached[0]).isSpecializationOf(attributeComp) ? true : componentReach.equals(attributeComp)).findFirst().orElse(null);
if (matchedComponent != null)
attributeComps.remove(matchedComponent);
else
return false;
}
return true;
};
}
}