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.regions.providers;
17
18 import java.util.function.Supplier;
19 import software.amazon.awssdk.annotations.SdkProtectedApi;
20 import software.amazon.awssdk.regions.Region;
21 import software.amazon.awssdk.utils.Lazy;
22 import software.amazon.awssdk.utils.ToString;
23
24 /**
25  * A wrapper for {@link AwsRegionProvider} that defers creation of the underlying provider until the first time the
26  * {@link AwsRegionProvider#getRegion()} method is invoked.
27  */

28 @SdkProtectedApi
29 public class LazyAwsRegionProvider implements AwsRegionProvider {
30     private final Lazy<AwsRegionProvider> delegate;
31
32     public LazyAwsRegionProvider(Supplier<AwsRegionProvider> delegateConstructor) {
33         this.delegate = new Lazy<>(delegateConstructor);
34     }
35
36     @Override
37     public Region getRegion() {
38         return delegate.getValue().getRegion();
39     }
40
41     @Override
42     public String toString() {
43         return ToString.builder("LazyAwsRegionProvider")
44                        .add("delegate", delegate)
45                        .build();
46     }
47 }
48