1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */

15
16 package software.amazon.awssdk.core.client.config;
17
18 import java.util.function.Consumer;
19 import software.amazon.awssdk.annotations.SdkProtectedApi;
20 import software.amazon.awssdk.utils.AttributeMap;
21 import software.amazon.awssdk.utils.SdkAutoCloseable;
22 import software.amazon.awssdk.utils.builder.CopyableBuilder;
23 import software.amazon.awssdk.utils.builder.ToCopyableBuilder;
24
25 /**
26  * A collection of configuration that is required by an AWS client in order to operate.
27  *
28  * Configuration can be set via {@link SdkClientConfiguration.Builder#option(ClientOption, Object)} and checked via
29  * {@link SdkClientConfiguration#option(ClientOption)}.
30  *
31  * This configuration can be merged with other configuration using {@link SdkClientConfiguration#merge}.
32  *
33  * This configuration object can be {@link #close()}d to release all closeable resources configured within it.
34  */

35 @SdkProtectedApi
36 public final class SdkClientConfiguration
37         implements ToCopyableBuilder<SdkClientConfiguration.Builder, SdkClientConfiguration>, SdkAutoCloseable {
38     private final AttributeMap attributes;
39
40     private SdkClientConfiguration(AttributeMap attributes) {
41         this.attributes = attributes;
42     }
43
44     /**
45      * Create a builder for a {@link SdkClientConfiguration}.
46      */

47     public static SdkClientConfiguration.Builder builder() {
48         return new Builder(AttributeMap.builder());
49     }
50
51     /**
52      * Retrieve the value of a specific option.
53      */

54     public <T> T option(ClientOption<T> option) {
55         return attributes.get(option);
56     }
57
58     /**
59      * Merge this configuration with another configuration, where this configuration's values take precedence.
60      */

61     public SdkClientConfiguration merge(SdkClientConfiguration configuration) {
62         return new SdkClientConfiguration(attributes.merge(configuration.attributes));
63     }
64
65     public SdkClientConfiguration merge(Consumer<SdkClientConfiguration.Builder> configuration) {
66         return merge(SdkClientConfiguration.builder().applyMutation(configuration).build());
67     }
68
69     @Override
70     public Builder toBuilder() {
71         return new Builder(attributes.toBuilder());
72     }
73
74     /**
75      * Close this configuration, which closes all closeable attributes.
76      */

77     @Override
78     public void close() {
79         attributes.close();
80     }
81
82     public static final class Builder implements CopyableBuilder<Builder, SdkClientConfiguration> {
83         private final AttributeMap.Builder attributes;
84
85         private Builder(AttributeMap.Builder attributes) {
86             this.attributes = attributes;
87         }
88
89         /**
90          * Configure the value of a specific option.
91          */

92         public <T> Builder option(ClientOption<T> option, T value) {
93             this.attributes.put(option, value);
94             return this;
95         }
96
97         @Override
98         public SdkClientConfiguration build() {
99             return new SdkClientConfiguration(attributes.build());
100         }
101     }
102 }
103