1 /*
2  * Copyright 2010-2020 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 com.amazonaws.client.builder;
17
18 import com.amazonaws.annotation.Immutable;
19 import com.amazonaws.annotation.SdkProtectedApi;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.Map;
23
24 /**
25  * Type safe map of key/value pairs. Used for advanced or service specific client configuration in the client
26  * builders.
27  */

28 @SdkProtectedApi
29 @Immutable
30 public final class AdvancedConfig {
31
32     public final static AdvancedConfig EMPTY = AdvancedConfig.builder().build();
33
34     /**
35      * Type safe key for config options.
36      *
37      * @param <T> Type of value.
38      */

39     public static class Key<T> {
40     }
41
42     private final Map<Key<?>, Object> config;
43
44     private AdvancedConfig(Builder builder) {
45         this.config = Collections.unmodifiableMap(new HashMap<Key<?>, Object>(builder.config));
46     }
47
48     @SuppressWarnings("unchecked")
49     public <T> T get(Key<T> key) {
50         return (T) config.get(key);
51     }
52
53     public static Builder builder() {
54         return new Builder();
55     }
56
57     public static class Builder {
58
59         private final Map<Key<?>, Object> config = new HashMap<Key<?>, Object>();
60
61         private Builder() {
62         }
63
64         @SuppressWarnings("unchecked")
65         public <T> T get(Key<T> key) {
66             return (T) config.get(key);
67         }
68
69         public <T> Builder put(Key<T> key, T value) {
70             config.put(key, value);
71             return this;
72         }
73
74         public AdvancedConfig build() {
75             return new AdvancedConfig(this);
76         }
77     }
78
79 }
80