4
4
5
5
import java .util .*;
6
6
import java .util .concurrent .ConcurrentLinkedQueue ;
7
+ import java .util .function .Function ;
7
8
import java .util .stream .Collectors ;
8
9
9
10
import static whelk .util .DocumentUtil .getAtPath ;
10
11
11
12
public class FacetTree {
12
13
13
14
private final JsonLd jsonLd ;
14
- private List <String > observationsAsTypeKeys = new ArrayList <>();
15
+ private Map <String , Map < String , Object >> keyToObservation = new HashMap <>();
15
16
16
17
public FacetTree (JsonLd jsonLd ) {
17
18
this .jsonLd = jsonLd ;
@@ -22,39 +23,36 @@ public List<Map<String, Object>> sortObservationsAsTree(List<Map<String, Object>
22
23
Queue <Map <String , Object >> queue = new ConcurrentLinkedQueue <>();
23
24
Set <String > intermediateClasses = new HashSet <>();
24
25
25
- observationsAsTypeKeys = observations .stream ()
26
- .map (o -> jsonLd .toTermKey (get (o , List .of ("object" , "@id" ), "" )))
27
- .collect (Collectors .toList ());
26
+ keyToObservation = observations .stream ()
27
+ .collect (Collectors .toMap (o -> jsonLd .toTermKey (get (o , List .of ("object" , "@id" ), "" )), Function .identity ()));
28
28
29
- List <String > rootCandidates = observationsAsTypeKeys .stream ().filter (this ::isRootNode ).toList ();
30
- String rootKey ;
31
- Map <String , Object > root ;
29
+ List <String > rootCandidates = keyToObservation .keySet ().stream ().filter (this ::isRootNode ).toList ();
30
+ String rootKey = "" ;
32
31
33
32
if (rootCandidates .size () == 1 ) {
34
- root = observations .stream ()
35
- .filter (o -> jsonLd .toTermKey (get (o , List .of ("object" , "@id" ), "" )).equals (rootCandidates .getFirst ()))
36
- .findFirst ()
37
- .orElse (null );
38
- rootKey = jsonLd .toTermKey (get (root , List .of ("object" , "@id" ), "" ));
33
+ rootKey = rootCandidates .getFirst ();
34
+ var root = keyToObservation .get (rootKey );
39
35
tree .add (root );
40
36
queue .add (root );
41
37
} else {
42
- rootKey = getAbsentRoot (observationsAsTypeKeys .getFirst ());
43
- //TODO: should not be null
44
- if (rootKey != null ) {
45
- observationsAsTypeKeys .add (rootKey );
46
- root = createFakeObservation (rootKey );
47
- observations .add (root );
48
- tree .add (root );
49
- queue .add (root );
38
+ Optional <String > first = keyToObservation .keySet ().stream ().findFirst ();
39
+ if (first .isPresent ()) {
40
+ Optional <String > rootKeyOpt = getAbsentRoot (first .get ());
41
+ if (rootKeyOpt .isPresent ()) {
42
+ rootKey = rootKeyOpt .get ();
43
+ var root = createFakeObservation (rootKey );
44
+ observations .add (root );
45
+ tree .add (root );
46
+ queue .add (root );
47
+ }
50
48
}
51
49
}
52
50
53
- observationsAsTypeKeys . forEach ( typeKey -> {
51
+ for ( String typeKey : keyToObservation . keySet ()) {
54
52
if (!typeKey .equals (rootKey )) {
55
53
intermediateClasses .addAll (getIntermediateClassesFor (typeKey ));
56
54
}
57
- });
55
+ }
58
56
59
57
observations .addAll (intermediateClasses .stream ().map (this ::createFakeObservation ).toList ());
60
58
@@ -93,27 +91,27 @@ private List<String> getAbsentSuperClasses(String typeKey) {
93
91
List <String > allSuperClasses = jsonLd .getSuperClasses (typeKey );
94
92
95
93
return allSuperClasses .stream ()
96
- .takeWhile (s -> !observationsAsTypeKeys . contains (s ))
94
+ .takeWhile (s -> !keyToObservation . containsKey (s ))
97
95
.toList ();
98
96
}
99
97
100
- private String getAbsentRoot (String typeKey ) {
98
+ private Optional < String > getAbsentRoot (String typeKey ) {
101
99
List <String > allSuperClasses = jsonLd .getSuperClasses (typeKey );
102
100
return allSuperClasses .stream ()
103
101
.filter (this ::subClassesContainsAllObservations )
104
- .findFirst (). orElse ( null ) ;
102
+ .findFirst ();
105
103
}
106
104
107
105
private boolean subClassesContainsAllObservations (String c ) {
108
106
Set <String > subClasses = jsonLd .getSubClasses (c );
109
- return subClasses .containsAll (observationsAsTypeKeys );
107
+ return subClasses .containsAll (keyToObservation . keySet () );
110
108
}
111
109
112
110
private boolean hasParentInObservations (String typeKey ) {
113
111
List <String > allSuperClasses = jsonLd .getSuperClasses (typeKey );
114
112
115
113
return allSuperClasses .stream ()
116
- .anyMatch (s -> observationsAsTypeKeys . contains (s ));
114
+ .anyMatch (s -> keyToObservation . containsKey (s ));
117
115
}
118
116
119
117
private boolean isRootNode (String typeKey ) {
0 commit comments