Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.0 preparation: builder API update. #3641

Merged
merged 3 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion common/common/src/main/java/io/helidon/common/Builder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.helidon.common;

import java.util.function.Consumer;
import java.util.function.Supplier;

/**
Expand All @@ -25,17 +26,39 @@
* as instance builders (fluent API builder pattern), where method {@link java.util.function.Supplier#get()} would be
* misleading.
*
* @param <B> Type of the builder
* @param <T> Type of the built instance
*/
@FunctionalInterface
public interface Builder<T> extends Supplier<T> {
public interface Builder<B extends Builder<B, T>, T> extends Supplier<T> {
/**
* Build the instance from this builder.
*
* @return instance of the built type
*/
T build();

/**
* Update the builder in a fluen API way.
*
* @param consumer consumer of the builder instance
* @return updated builder instance
*/
default B update(Consumer<B> consumer) {
consumer.accept(identity());
return identity();
}

/**
* Instance of this builder as the correct type.
*
* @return this instance typed to correct type
*/
@SuppressWarnings("unchecked")
default B identity() {
return (B) this;
}

@Override
default T get() {
return build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates.
* Copyright (c) 2020, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -114,7 +114,7 @@ public String toString() {
return name;
}

static class Builder implements io.helidon.common.Builder<FeatureDescriptor> {
static class Builder implements io.helidon.common.Builder<Builder, FeatureDescriptor> {
private HelidonFlavor[] flavors = new HelidonFlavor[] {HelidonFlavor.SE, HelidonFlavor.MP};
private String name;
private String[] path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ public enum TraceOption {
* You can use system properties defined in the class to modify configuration, in which case you can just use
* {@link SerializationConfig#configureRuntime()} directly.
*/
public static class Builder implements io.helidon.common.Builder<SerializationConfig> {
public static class Builder implements io.helidon.common.Builder<Builder, SerializationConfig> {
private Action onWrongConfig = configuredAction(PROP_WRONG_CONFIG_ACTION, Action.WARN);
private Action onNoConfig = configuredAction(PROP_NO_CONFIG_ACTION, Action.WARN);
private String filterPattern = System.getProperty(PROP_PATTERN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ String getName() {
}
}

private static class Builder1 implements Builder<Result1> {
private static class Builder1 implements Builder<Builder1, Result1> {
private String message;

private Builder1() {
Expand All @@ -96,7 +96,7 @@ public Result1 build() {
}
}

private static class Builder2 implements Builder<Result2> {
private static class Builder2 implements Builder<Builder2, Result2> {
private String name;

private Builder2() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ V directGet(K key) {
* @param <V> type of values
*/
@Configured
public static class Builder<K, V> implements io.helidon.common.Builder<LruCache<K, V>> {
public static class Builder<K, V> implements io.helidon.common.Builder<Builder<K, V>, LruCache<K, V>> {
private int capacity = DEFAULT_CAPACITY;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public ScheduledExecutorService get() {
* A fluent API builder for {@link ScheduledThreadPoolSupplier}.
*/
@Configured
public static final class Builder implements io.helidon.common.Builder<ScheduledThreadPoolSupplier> {
public static final class Builder implements io.helidon.common.Builder<Builder, ScheduledThreadPoolSupplier> {
private int corePoolSize = EXECUTOR_DEFAULT_CORE_POOL_SIZE;
private boolean isDaemon = EXECUTOR_DEFAULT_IS_DAEMON;
private String threadNamePrefix = EXECUTOR_DEFAULT_THREAD_NAME_PREFIX;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public int corePoolSize() {
* A fluent API builder for {@link ThreadPoolSupplier}.
*/
@Configured
public static final class Builder implements io.helidon.common.Builder<ThreadPoolSupplier> {
public static final class Builder implements io.helidon.common.Builder<Builder, ThreadPoolSupplier> {
private int corePoolSize = DEFAULT_CORE_POOL_SIZE;
private int maxPoolSize = DEFAULT_MAX_POOL_SIZE;
private int keepAliveMinutes = DEFAULT_KEEP_ALIVE_MINUTES;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2020 Oracle and/or its affiliates.
* Copyright (c) 2019, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -165,7 +165,7 @@ static Builder builder() {
/**
* Fluent API builder for {@link Context}.
*/
class Builder implements io.helidon.common.Builder<Context> {
class Builder implements io.helidon.common.Builder<Builder, Context> {
private static final AtomicLong PARENT_CONTEXT_COUNTER = new AtomicLong(1);
// this will cycle through long values from 1 to Long.MAX_VALUE
private static final AtomicLong CHILD_CONTEXT_COUNTER = new AtomicLong(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private static Base64Value performCryptoOperation(int mode, String algorithm, St
/**
* Builder of the {@link AsymmetricCipher}.
*/
public static final class Builder implements io.helidon.common.Builder<AsymmetricCipher> {
public static final class Builder implements io.helidon.common.Builder<Builder, AsymmetricCipher> {

private String algorithm = ALGORITHM_RSA_ECB_OAEP256;
private String provider = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public Base64Value digest(Base64Value value) {
/**
* Builder of the {@link HashDigest}.
*/
public static final class Builder implements io.helidon.common.Builder<HashDigest> {
public static final class Builder implements io.helidon.common.Builder<Builder, HashDigest> {

private String algorithm = ALGORITHM_SHA_256;
private String provider = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public Base64Value digest(Base64Value value) {
/**
* Builder of the {@link HmacDigest}.
*/
public static final class Builder implements io.helidon.common.Builder<HmacDigest> {
public static final class Builder implements io.helidon.common.Builder<Builder, HmacDigest> {

private String algorithm = ALGORITHM_SHA_256;
private String provider = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ private java.security.Signature getSignature() throws NoSuchAlgorithmException,
/**
* Builder of the {@link Signature}.
*/
public static final class Builder implements io.helidon.common.Builder<Signature> {
public static final class Builder implements io.helidon.common.Builder<Builder, Signature> {

private String algorithm = ALGORITHM_SHA256_RSA;
private String provider = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public Base64Value decrypt(Base64Value encrypted) {
/**
* Builder of the {@link SymmetricCipher}.
*/
public static class Builder implements io.helidon.common.Builder<SymmetricCipher> {
public static class Builder implements io.helidon.common.Builder<Builder, SymmetricCipher> {

private String algorithm = ALGORITHM_AES_GCM;
private String provider = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates.
* Copyright (c) 2020, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,7 +23,7 @@
* @param <B> type of the builder
* @param <T> type which the builder builds
*/
public interface FormBuilder<B, T> extends Builder<T> {
public interface FormBuilder<B, T> extends Builder<FormBuilder<B, T>, T> {

/**
* Add a new values to specific content disposition name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ public boolean hasSuffix(String suffix) {
/**
* A fluent API builder to create instances of {@link MediaType}.
*/
public static final class Builder implements io.helidon.common.Builder<MediaType> {
public static final class Builder implements io.helidon.common.Builder<Builder, MediaType> {
private String type = AcceptPredicate.WILDCARD_VALUE;
private String subtype = AcceptPredicate.WILDCARD_VALUE;
private String charset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public String toString() {
/**
* A fluent API builder for {@link SetCookie}.
*/
public static final class Builder implements io.helidon.common.Builder<SetCookie> {
public static final class Builder implements io.helidon.common.Builder<Builder, SetCookie> {
private final String name;
private final String value;
private ZonedDateTime expires;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public List<X509Certificate> certs() {
* @see KeyConfig#fullBuilder()
*/
@Configured
public static class Builder implements io.helidon.common.Builder<KeyConfig> {
public static class Builder implements io.helidon.common.Builder<Builder, KeyConfig> {
private PrivateKey explicitPrivateKey;
private PublicKey explicitPublicKey;
private X509Certificate explicitPublicCert;
Expand Down Expand Up @@ -327,7 +327,7 @@ public Builder config(Config config) {
* KeyConfig#keystoreBuilder()}.
*/
@Configured(ignoreBuildMethod = true)
public static final class KeystoreBuilder implements io.helidon.common.Builder<KeyConfig> {
public static final class KeystoreBuilder implements io.helidon.common.Builder<Builder, KeyConfig> {
private static final String DEFAULT_KEYSTORE_TYPE = "PKCS12";

private String keystoreType = DEFAULT_KEYSTORE_TYPE;
Expand Down Expand Up @@ -647,7 +647,7 @@ public KeystoreBuilder config(Config config) {
* use this builder), or to PKCS#12 keystore format (and use {@link KeystoreBuilder}).
*/
@Configured(ignoreBuildMethod = true)
public static final class PemBuilder implements io.helidon.common.Builder<KeyConfig> {
public static final class PemBuilder implements io.helidon.common.Builder<Builder, KeyConfig> {
private final StreamHolder privateKeyStream = new StreamHolder("privateKey");
private final StreamHolder publicKeyStream = new StreamHolder("publicKey");
private final StreamHolder certChainStream = new StreamHolder("certChain");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ <SOURCE, TARGET> TARGET map(SOURCE source, GenericType<SOURCE> sourceType, Gener
/**
* Fluent API builder for {@link io.helidon.common.mapper.MapperManager}.
*/
final class Builder implements io.helidon.common.Builder<MapperManager> {
final class Builder implements io.helidon.common.Builder<Builder, MapperManager> {
private HelidonServiceLoader.Builder<MapperProvider> providers = HelidonServiceLoader
.builder(ServiceLoader.load(MapperProvider.class));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ static MultiFromByteChannelBuilder multiFromByteChannelBuilder(ReadableByteChann
* Fluent API builder for creating a {@link io.helidon.common.reactive.Multi} from a
* {@link java.nio.channels.ReadableByteChannel}.
*/
final class MultiFromByteChannelBuilder implements Builder<Multi<ByteBuffer>> {
final class MultiFromByteChannelBuilder implements Builder<MultiFromByteChannelBuilder, Multi<ByteBuffer>> {
private static final int DEFAULT_BUFFER_CAPACITY = 1024 * 8;
private static final RetrySchema DEFAULT_RETRY_SCHEMA = RetrySchema.linear(0, 10, 250);
private static final String THREAD_PREFIX = "multi-rbc-";
Expand Down Expand Up @@ -342,7 +342,7 @@ boolean isExternalExecutor() {
/**
* Fluent API builder for creating a subscriber consuming {@code Multi<ByteBuffer>} to {@link WritableByteChannel}.
*/
final class MultiToByteChannelBuilder implements Builder<Function<? super Multi<ByteBuffer>, ? extends Single<Void>>> {
final class MultiToByteChannelBuilder implements Builder<MultiToByteChannelBuilder, Function<? super Multi<ByteBuffer>, ? extends Single<Void>>> {

private final WritableByteChannel writableByteChannel;
private Executor executor;
Expand Down Expand Up @@ -385,7 +385,7 @@ public MultiToByteChannelBuilder executor(Executor executor) {
* Fluent API builder for creating a {@link io.helidon.common.reactive.Multi} from an
* {@link java.io.InputStream}.
*/
final class MultiFromInputStreamBuilder implements Builder<Multi<ByteBuffer>> {
final class MultiFromInputStreamBuilder implements Builder<MultiFromInputStreamBuilder, Multi<ByteBuffer>> {

private int bufferSize = 1024;
private ExecutorService executor;
Expand Down Expand Up @@ -431,7 +431,7 @@ public Multi<ByteBuffer> build() {
/**
* Fluent API builder for {@link io.helidon.common.reactive.OutputStreamMulti}.
*/
final class OutputStreamMultiBuilder implements Builder<OutputStreamMulti> {
final class OutputStreamMultiBuilder implements Builder<OutputStreamMultiBuilder, OutputStreamMulti> {

private Duration timeout;
private BiConsumer<Long, Long> consumer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ void fail(Throwable ex) {
*
* @param <T> type of returned multi
*/
public static class Builder<T> implements io.helidon.common.Builder<MultiTappedPublisher<T>> {
public static class Builder<T> implements io.helidon.common.Builder<Builder<T>, MultiTappedPublisher<T>> {
private final Multi<T> source;
private Consumer<? super Flow.Subscription> onSubscribeCallback;
private Consumer<? super T> onNextCallback;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public static <T> SingleTappedPublisher.Builder<T> builder(Single<T> source) {
*
* @param <T> type of returned multi
*/
public static class Builder<T> implements io.helidon.common.Builder<SingleTappedPublisher<T>> {
public static class Builder<T> implements io.helidon.common.Builder<Builder<T>, SingleTappedPublisher<T>> {
private final Single<T> source;
private Consumer<? super Flow.Subscription> onSubscribeCallback;
private Consumer<? super T> onNextCallback;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public List<T> asList() {
*
* @param <T> type of the service to be loaded
*/
public static final class Builder<T> implements io.helidon.common.Builder<HelidonServiceLoader<T>> {
public static final class Builder<T> implements io.helidon.common.Builder<Builder<T>, HelidonServiceLoader<T>> {
private final ServiceLoader<T> serviceLoader;
private final List<ServiceWithPriority<T>> customServices = new LinkedList<ServiceWithPriority<T>>();
private final Set<String> excludedServiceClasses = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public String toString() {
return name + "(" + ordinal + ")" + ", wrapping: " + delegate.toString();
}

static class Builder implements io.helidon.common.Builder<MetaConfigSource> {
static class Builder implements io.helidon.common.Builder<Builder, MetaConfigSource> {
private org.eclipse.microprofile.config.spi.ConfigSource delegate;
private int ordinal;
private boolean ordinalNotSet = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2020 Oracle and/or its affiliates.
* Copyright (c) 2017, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -223,7 +223,7 @@ private static Enumeration<URL> findAllResources(String resource) {
*/
public static final class Builder extends AbstractConfigSourceBuilder<Builder, Void>
implements ParsableSource.Builder<Builder>,
io.helidon.common.Builder<ClasspathConfigSource> {
io.helidon.common.Builder<Builder, ClasspathConfigSource> {

private URL url;
private String resource;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2020 Oracle and/or its affiliates.
* Copyright (c) 2017, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -97,7 +97,7 @@ public static Builder builder() {
* then {@link OverrideSource#load} throws {@link ConfigException}.
*/
public static final class Builder extends AbstractSourceBuilder<Builder, Void>
implements io.helidon.common.Builder<ClasspathOverrideSource> {
implements io.helidon.common.Builder<Builder, ClasspathOverrideSource> {

private URL url;
private String resource;
Expand Down
2 changes: 1 addition & 1 deletion config/config/src/main/java/io/helidon/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -1138,7 +1138,7 @@ interface Context {
* @see ConfigParser
* @see ConfigFilter
*/
interface Builder {
interface Builder extends io.helidon.common.Builder<Builder, Config> {
/**
* Sets ordered list of {@link ConfigSource} instance to be used as single source of configuration
* to be wrapped into {@link Config} API.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2020 Oracle and/or its affiliates.
* Copyright (c) 2017, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -155,7 +155,7 @@ public Optional<NodeContent> load() throws ConfigException {
public static final class Builder extends AbstractConfigSourceBuilder<Builder, Path>
implements PollableSource.Builder<Builder>,
WatchableSource.Builder<Builder, Path>,
io.helidon.common.Builder<DirectoryConfigSource> {
io.helidon.common.Builder<Builder, DirectoryConfigSource> {
private Path path;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2020 Oracle and/or its affiliates.
* Copyright (c) 2017, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -172,7 +172,7 @@ public static final class Builder extends AbstractConfigSourceBuilder<Builder, P
implements PollableSource.Builder<Builder>,
WatchableSource.Builder<Builder, Path>,
ParsableSource.Builder<Builder>,
io.helidon.common.Builder<FileConfigSource> {
io.helidon.common.Builder<Builder, FileConfigSource> {

private Path path;

Expand Down
Loading