diff --git a/src/main/java/io/vlingo/schemata/codegen/processor/types/CacheTypeResolver.java b/src/main/java/io/vlingo/schemata/codegen/processor/types/CacheTypeResolver.java index 03a155f3..bf707ad2 100644 --- a/src/main/java/io/vlingo/schemata/codegen/processor/types/CacheTypeResolver.java +++ b/src/main/java/io/vlingo/schemata/codegen/processor/types/CacheTypeResolver.java @@ -7,12 +7,13 @@ package io.vlingo.schemata.codegen.processor.types; +import io.vlingo.common.Completes; +import io.vlingo.schemata.codegen.ast.types.TypeDefinition; + import java.util.HashMap; import java.util.Map; import java.util.Optional; -import io.vlingo.schemata.codegen.ast.types.TypeDefinition; - public class CacheTypeResolver implements TypeResolver { private final Map types; @@ -21,13 +22,13 @@ public CacheTypeResolver() { } @Override - public Optional resolve(String fullQualifiedTypeName, final String simpleTypeName) { + public Completes> resolve(String fullQualifiedTypeName, final String simpleTypeName) { for (final TypeDefinition type : types.values()) { if (type.fullyQualifiedTypeName.equals(fullQualifiedTypeName) || type.typeName.equals(simpleTypeName)) { - return Optional.of(type); + return Completes.withSuccess(Optional.of(type)); } } - return Optional.empty(); + return Completes.withSuccess(Optional.empty()); } public void produce(TypeDefinition typeDefinition, String version) { diff --git a/src/main/java/io/vlingo/schemata/codegen/processor/types/TypeResolver.java b/src/main/java/io/vlingo/schemata/codegen/processor/types/TypeResolver.java index 5758a454..ffd4cfd8 100644 --- a/src/main/java/io/vlingo/schemata/codegen/processor/types/TypeResolver.java +++ b/src/main/java/io/vlingo/schemata/codegen/processor/types/TypeResolver.java @@ -7,10 +7,11 @@ package io.vlingo.schemata.codegen.processor.types; -import java.util.Optional; - +import io.vlingo.common.Completes; import io.vlingo.schemata.codegen.ast.types.TypeDefinition; +import java.util.Optional; + public interface TypeResolver { - Optional resolve(final String fullQualifiedTypeName, final String simpleTypeName); + Completes> resolve(final String fullQualifiedTypeName, final String simpleTypeName); } diff --git a/src/main/java/io/vlingo/schemata/codegen/processor/types/TypeResolverProcessor.java b/src/main/java/io/vlingo/schemata/codegen/processor/types/TypeResolverProcessor.java index 793a5742..d4e47bfd 100644 --- a/src/main/java/io/vlingo/schemata/codegen/processor/types/TypeResolverProcessor.java +++ b/src/main/java/io/vlingo/schemata/codegen/processor/types/TypeResolverProcessor.java @@ -7,9 +7,6 @@ package io.vlingo.schemata.codegen.processor.types; -import java.util.List; -import java.util.stream.Collectors; - import io.vlingo.actors.Actor; import io.vlingo.common.Completes; import io.vlingo.schemata.Schemata; @@ -20,6 +17,11 @@ import io.vlingo.schemata.codegen.ast.types.TypeDefinition; import io.vlingo.schemata.codegen.processor.Processor; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.stream.Collectors; + public class TypeResolverProcessor extends Actor implements Processor { private final TypeResolver resolver; @@ -28,35 +30,41 @@ public TypeResolverProcessor(TypeResolver resolver) { } @Override + @SuppressWarnings("unchecked") public Completes process(Node node, final String fullyQualifiedTypeName) { TypeDefinition type = Processor.requireBeing(node, TypeDefinition.class); - List processedTypes = type.children.stream() + List> processedTypeList = type.children.stream() .map(e -> (FieldDefinition) e) .map(this::resolveType) + .map(e -> (Completes) e) + .map(e -> (Completes) e) .collect(Collectors.toList()); - completesEventually().with(new TypeDefinition(type.category, fullyQualifiedTypeName, type.typeName, processedTypes)); + Completes> eventuallyProcessedTypes = unwrap(processedTypeList); + eventuallyProcessedTypes.andFinallyConsume(processedTypes -> { + completesEventually().with(new TypeDefinition(type.category, fullyQualifiedTypeName, type.typeName, processedTypes)); + }); + return completes(); } - private FieldDefinition resolveType(FieldDefinition fieldDefinition) { + private Completes resolveType(FieldDefinition fieldDefinition) { final Type typeNode = fieldDefinition.type; if (typeNode instanceof BasicType) { final BasicType basicType = (BasicType) typeNode; - final Type type = - resolver - .resolve(basicType.typeName, simple(basicType.typeName)) - .map(definition -> (Type) definition) - .orElse(basicType); + Completes resolvedType = resolver.resolve(basicType.typeName, simple(basicType.typeName)) + .andThen(foundType -> foundType.map(definition -> (Type) definition).orElse(basicType)); - return new FieldDefinition(type, fieldDefinition.version, fieldDefinition.name, fieldDefinition.defaultValue); + return resolvedType.andThen(type -> + new FieldDefinition(type, fieldDefinition.version, fieldDefinition.name, fieldDefinition.defaultValue) + ); } - return fieldDefinition; + return Completes.withSuccess(fieldDefinition); } private String simple(final String typeName) { @@ -66,4 +74,22 @@ private String simple(final String typeName) { } return typeName; } + + private Completes> unwrap(List> completes) { + CountDownLatch latch = new CountDownLatch(completes.size()); + List result = new ArrayList<>(completes.size()); + completes.forEach(complete -> { + complete.andThenConsume(result::add) + .andFinallyConsume(e -> latch.countDown()); + }); + + return Completes.withSuccess(result) + .andThenConsume(i -> { + try { + latch.await(); + } catch (InterruptedException e) { + logger().error("TypeResolverProcessor could not unwrap list of Completes " + e.getMessage(), e); + } + }); + } }