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.internal.http.loader;
17
18 import static software.amazon.awssdk.utils.Validate.notEmpty;
19
20 import java.util.Arrays;
21 import java.util.List;
22 import java.util.Optional;
23 import software.amazon.awssdk.annotations.SdkInternalApi;
24
25 /**
26  * Consults a chain of {@link SdkHttpServiceProvider} looking for one that can provide a service instance.
27  */

28 @SdkInternalApi
29 final class SdkHttpServiceProviderChain<T> implements SdkHttpServiceProvider<T> {
30
31     private final List<SdkHttpServiceProvider<T>> httpProviders;
32
33     @SafeVarargs
34     SdkHttpServiceProviderChain(SdkHttpServiceProvider<T>... httpProviders) {
35         this.httpProviders = Arrays.asList(notEmpty(httpProviders, "httpProviders cannot be null or empty"));
36     }
37
38     @Override
39     public Optional<T> loadService() {
40         return httpProviders.stream()
41                             .map(SdkHttpServiceProvider::loadService)
42                             .filter(Optional::isPresent)
43                             .map(Optional::get)
44                             .findFirst();
45     }
46
47 }
48