7
7
8
8
package io .vlingo .schemata .codegen .processor .types ;
9
9
10
- import java .util .List ;
11
- import java .util .stream .Collectors ;
12
-
13
10
import io .vlingo .actors .Actor ;
14
11
import io .vlingo .common .Completes ;
15
12
import io .vlingo .schemata .Schemata ;
20
17
import io .vlingo .schemata .codegen .ast .types .TypeDefinition ;
21
18
import io .vlingo .schemata .codegen .processor .Processor ;
22
19
20
+ import java .util .ArrayList ;
21
+ import java .util .List ;
22
+ import java .util .concurrent .CountDownLatch ;
23
+ import java .util .stream .Collectors ;
24
+
23
25
public class TypeResolverProcessor extends Actor implements Processor {
24
26
private final TypeResolver resolver ;
25
27
@@ -28,35 +30,41 @@ public TypeResolverProcessor(TypeResolver resolver) {
28
30
}
29
31
30
32
@ Override
33
+ @ SuppressWarnings ("unchecked" )
31
34
public Completes <Node > process (Node node , final String fullyQualifiedTypeName ) {
32
35
TypeDefinition type = Processor .requireBeing (node , TypeDefinition .class );
33
36
34
- List <Node > processedTypes = type .children .stream ()
37
+ List <Completes < Node >> processedTypeList = type .children .stream ()
35
38
.map (e -> (FieldDefinition ) e )
36
39
.map (this ::resolveType )
40
+ .map (e -> (Completes ) e )
41
+ .map (e -> (Completes <Node >) e )
37
42
.collect (Collectors .toList ());
38
43
39
- completesEventually ().with (new TypeDefinition (type .category , fullyQualifiedTypeName , type .typeName , processedTypes ));
44
+ Completes <List <Node >> eventuallyProcessedTypes = unwrap (processedTypeList );
45
+ eventuallyProcessedTypes .andFinallyConsume (processedTypes -> {
46
+ completesEventually ().with (new TypeDefinition (type .category , fullyQualifiedTypeName , type .typeName , processedTypes ));
47
+ });
48
+
40
49
return completes ();
41
50
42
51
}
43
52
44
- private FieldDefinition resolveType (FieldDefinition fieldDefinition ) {
53
+ private Completes < FieldDefinition > resolveType (FieldDefinition fieldDefinition ) {
45
54
final Type typeNode = fieldDefinition .type ;
46
55
47
56
if (typeNode instanceof BasicType ) {
48
57
final BasicType basicType = (BasicType ) typeNode ;
49
58
50
- final Type type =
51
- resolver
52
- .resolve (basicType .typeName , simple (basicType .typeName ))
53
- .map (definition -> (Type ) definition )
54
- .orElse (basicType );
59
+ Completes <Type > resolvedType = resolver .resolve (basicType .typeName , simple (basicType .typeName ))
60
+ .andThen (foundType -> foundType .map (definition -> (Type ) definition ).orElse (basicType ));
55
61
56
- return new FieldDefinition (type , fieldDefinition .version , fieldDefinition .name , fieldDefinition .defaultValue );
62
+ return resolvedType .andThen (type ->
63
+ new FieldDefinition (type , fieldDefinition .version , fieldDefinition .name , fieldDefinition .defaultValue )
64
+ );
57
65
}
58
66
59
- return fieldDefinition ;
67
+ return Completes . withSuccess ( fieldDefinition ) ;
60
68
}
61
69
62
70
private String simple (final String typeName ) {
@@ -66,4 +74,22 @@ private String simple(final String typeName) {
66
74
}
67
75
return typeName ;
68
76
}
77
+
78
+ private <T > Completes <List <T >> unwrap (List <Completes <T >> completes ) {
79
+ CountDownLatch latch = new CountDownLatch (completes .size ());
80
+ List <T > result = new ArrayList <>(completes .size ());
81
+ completes .forEach (complete -> {
82
+ complete .andThenConsume (result ::add )
83
+ .andFinallyConsume (e -> latch .countDown ());
84
+ });
85
+
86
+ return Completes .withSuccess (result )
87
+ .andThenConsume (i -> {
88
+ try {
89
+ latch .await ();
90
+ } catch (InterruptedException e ) {
91
+ logger ().error ("TypeResolverProcessor could not unwrap list of Completes<T> " + e .getMessage (), e );
92
+ }
93
+ });
94
+ }
69
95
}
0 commit comments