Skip to content

Commit

Permalink
Support Supplier<X> in builder setters for types that have a builder. (
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas-langer authored Aug 1, 2023
1 parent 6153086 commit 3dc2f5e
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,10 @@ void setters(PrototypeProperty.ConfiguredOption configured,
factorySetter(configured, setters, returnType, blueprintJavadoc, factoryMethod.createTargetType().get());
}

// if there is a builder factory method, we create a method with builder consumer
// if there is a builder factory method, we create a method with builder consumer of builder, and supplier of type
if (factoryMethod.builder().isPresent()) {
factorySetterConsumer(configured, setters, returnType, blueprintJavadoc, factoryMethod.builder().get());
factorySetterSupplier(configured, setters, returnType, blueprintJavadoc, factoryMethod.builder().get());
}
}

Expand Down Expand Up @@ -312,6 +313,47 @@ private void declaredSetter(PrototypeProperty.ConfiguredOption configured,
));
}

private void factorySetterSupplier(PrototypeProperty.ConfiguredOption configured,
List<GeneratedMethod> setters,
TypeName returnType,
Javadoc blueprintJavadoc,
FactoryMethods.FactoryMethod factoryMethod) {
TypeName supplierType = actualType();
if (!supplierType.wildcard()) {
supplierType = TypeName.builder(supplierType)
.wildcard(true)
.build();
}
supplierType = TypeName.builder(TypeNames.SUPPLIER)
.addTypeArgument(supplierType)
.build();

String argumentName = "supplier";

List<String> paramLines = new ArrayList<>();
paramLines.add("supplier of");
paramLines.addAll(blueprintJavadoc.returns());
Javadoc javadoc = new Javadoc(blueprintJavadoc.lines(),
List.of(new Javadoc.Tag(argumentName, paramLines)),
List.of("updated builder instance"),
List.of(new Javadoc.Tag("see", List.of("#" + getterName() + "()"))));

List<String> lines = new ArrayList<>();
lines.add("Objects.requireNonNull(" + argumentName + ");");
lines.add("this." + name() + "(" + argumentName + ".get());");
lines.add("return self();");

setters.add(new GeneratedMethod(
Set.of(setterModifier(configured).trim()),
setterName(),
returnType,
List.of(new GeneratedMethod.Argument(argumentName, supplierType)),
List.of(),
javadoc,
lines
));
}

private void factorySetterConsumer(PrototypeProperty.ConfiguredOption configured,
List<GeneratedMethod> setters,
TypeName returnType,
Expand Down
14 changes: 14 additions & 0 deletions common/types/src/main/java/io/helidon/common/types/Annotation.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,20 @@ public BUILDER typeName(java.util.function.Consumer<TypeName.Builder> consumer)
return self();
}

/**
* The type name, e.g., {@link java.util.Objects} -> "java.util.Objects".
*
* @param supplier supplier of
* the annotation type name
* @return updated builder instance
* @see #typeName()
*/
public BUILDER typeName(java.util.function.Supplier<? extends TypeName> supplier) {
Objects.requireNonNull(supplier);
this.typeName(supplier.get());
return self();
}

/**
* Get a key-value of all the annotation properties.
*
Expand Down
14 changes: 14 additions & 0 deletions common/types/src/main/java/io/helidon/common/types/TypeInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,20 @@ public BUILDER typeName(java.util.function.Consumer<TypeName.Builder> consumer)
return self();
}

/**
* The type name.
*
* @param supplier supplier of
* the type name
* @return updated builder instance
* @see #typeName()
*/
public BUILDER typeName(java.util.function.Supplier<? extends TypeName> supplier) {
Objects.requireNonNull(supplier);
this.typeName(supplier.get());
return self();
}

/**
* Any Map, List, Set, or method that has {@link TypeName#typeArguments()} will be analyzed and any
* type arguments will have
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;

/**
* Commonly used type names.
Expand Down Expand Up @@ -50,6 +51,10 @@ public final class TypeNames {
* Type name for {@link java.util.Optional}.
*/
public static final TypeName OPTIONAL = TypeName.create(Optional.class);
/**
* Type name for {@link java.util.function.Supplier}.
*/
public static final TypeName SUPPLIER = TypeName.create(Supplier.class);
/**
* Type name for {@link java.util.Collection}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,21 @@ public BUILDER typeName(java.util.function.Consumer<TypeName.Builder> consumer)
return self();
}

/**
* The type name for the element (e.g., java.util.List). If the element is a method, then this is the return type of
* the method.
*
* @param supplier supplier of
* the type name of the element
* @return updated builder instance
* @see #typeName()
*/
public BUILDER typeName(java.util.function.Supplier<? extends TypeName> supplier) {
Objects.requireNonNull(supplier);
this.typeName(supplier.get());
return self();
}

/**
* The element (e.g., method, field, etc) name.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ class InvocationTest {
.elementInfo(TypedElementInfo.builder()
.elementName("test")
.elementTypeKind(TypeValues.KIND_METHOD)
.typeName(TypeName.create(InvocationTest.class))
.build())
.typeName(TypeName.create(InvocationTest.class)))
.interceptors(List.of(first.provider, second.provider))
.build();
ArrayList<Object[]> calls = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@

import java.util.Base64;
import java.util.Collections;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Function;
Expand Down Expand Up @@ -69,8 +71,9 @@ class Http2ClientConnectionHandler {
}

void close() {
this.allConnections.keySet()
.forEach(Http2ClientConnection::close);
// this is to prevent concurrent modification (connections remove themself from the map)
Set<Http2ClientConnection> toClose = new HashSet<>(allConnections.keySet());
toClose.forEach(Http2ClientConnection::close);
this.activeConnection.set(null);
this.allConnections.clear();
}
Expand Down

0 comments on commit 3dc2f5e

Please sign in to comment.