xref: /aosp_15_r20/external/aws-sdk-java-v2/utils/src/main/java/software/amazon/awssdk/utils/builder/SdkBuilder.java (revision 8a52c7834d808308836a99fc2a6e0ed8db339086)
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.utils.builder;
17 
18 import java.util.function.Consumer;
19 import software.amazon.awssdk.annotations.SdkPublicApi;
20 
21 /**
22  * A mutable object that can be used to create an immutable object of type T.
23  *
24  * @param <B> the builder type (this)
25  * @param <T> the type that the builder will build
26  */
27 @SdkPublicApi
28 public interface SdkBuilder<B extends SdkBuilder<B, T>, T> extends Buildable {
29 
30     /**
31      * An immutable object that is created from the
32      * properties that have been set on the builder.
33      *
34      * @return an instance of T
35      */
36     @Override
build()37     T build();
38 
39     /**
40      * A convenience operator that takes something that will
41      * mutate the builder in some way and allows inclusion of it
42      * in chaining operations. For example instead of:
43      *
44      * <pre><code>
45      * Builder builder = ClassBeingBuilt.builder();
46      * builder = Util.addSomeDetailToTheBuilder(builder);
47      * ClassBeingBuilt clz = builder.build();
48      * </code></pre>
49      * <p>
50      * This can be done in a statement:
51      *
52      * <pre><code>
53      * ClassBeingBuilt = ClassBeingBuilt.builder().applyMutation(Util::addSomeDetailToTheBuilder).build();
54      * </code></pre>
55      *
56      * @param mutator the function that mutates the builder
57      * @return B the mutated builder instance
58      */
59     @SuppressWarnings("unchecked")
applyMutation(Consumer<B> mutator)60     default B applyMutation(Consumer<B> mutator) {
61         mutator.accept((B) this);
62         return (B) this;
63     }
64 }
65