MCPcopy Create free account
hub / github.com/OpenEndedGroup/Field2 / BoxChildHelper

Class BoxChildHelper

src/main/java/fieldbox/boxes/BoxChildHelper.java:20–316  ·  view source on GitHub ↗

Class to provide more useful access to children and parent lists inside Field

Source from the content-addressed store, hash-verified

18/**
19 * Class to provide more useful access to children and parent lists inside Field
20 */
21@SafeToToString
22public class BoxChildHelper implements fieldlinker.AsMap, Collection<Box>, ObjectToHTML.MasqueradesAs, HandlesCompletion {
23
24 private final ArrayList<Box> c;
25 LinkedHashMap<String, Box> t = new LinkedHashMap<>();
26 LinkedHashMap<String, List<Box>> tl = new LinkedHashMap<>();
27
28 public BoxChildHelper(Collection<Box> c) {
29 this.c = new ArrayList<>(c);
30 c.stream().forEach(x -> t.put(x.properties.get(Box.name), x));
31 c.stream().forEach(x -> tl.computeIfAbsent(x.properties.get(Box.name), (k) -> new ArrayList<>()).add(x));
32 }
33
34 @Override
35 public boolean asMap_isProperty(String p) {
36 return t.containsKey(p) || p.equals("all") || p.equals("first") || c.stream().map(x -> x.asMap_get(p)).filter(x -> x != null).findAny().isPresent();
37 }
38
39 @Override
40 public Object asMap_call(Object a, Object b) {
41 return null;
42 }
43
44 @Override
45 public Object asMap_get(String p) {
46 if (p.equals("all")) {
47 return new All();
48 }
49
50 if (p.equals("first")) {
51 if (c.size() == 0) return null;
52 return c.get(0);
53 }
54
55 if (t.containsKey(p)) return t.get(p);
56
57
58 Stream<Object> o = c.stream().map(x -> x.asMap_get(p)).filter(x -> x != null);
59 Optional val = null;
60
61 Dict.Prop c = new Dict.Prop(p).findCanon();
62 if (c != null) {
63 BinaryOperator sr = c.getAttributes().get(Dict.streamReducer);
64 if (sr != null)
65 val = o.reduce(sr);
66 else {
67 Function<Collection, Optional<Object>> cr = c.getAttributes().get(Dict.collectionReducer);
68 if (cr != null) {
69 List<Object> q = o.collect(Collectors.toList());
70 if (q.size() > 0)
71 val = cr.apply(q);
72 }
73 }
74 }
75
76 if (val != null && val.isPresent())
77 return val.get();

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected