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  * Implementors of this interface provide a way to get from an instance of T to a {@link CopyableBuilder}. This allows
23  * modification of an otherwise immutable object using the source object as a base.
24  *
25  * @param <T> the type that the builder will build (this)
26  * @param <B> the builder type
27  */
28 @SdkPublicApi
29 public interface ToCopyableBuilder<B extends CopyableBuilder<B, T>, T extends ToCopyableBuilder<B, T>> {
30     /**
31      * Take this object and create a builder that contains all of the current property values of this object.
32      *
33      * @return a builder for type T
34      */
toBuilder()35     B toBuilder();
36 
37     /**
38      * A convenience method for calling {@link #toBuilder()}, updating the returned builder and then calling
39      * {@link CopyableBuilder#build()}. This is useful for making small modifications to the existing object.
40      *
41      * @param modifier A function that mutates this immutable object using the provided builder.
42      * @return A new copy of this object with the requested modifications.
43      */
copy(Consumer<? super B> modifier)44     default T copy(Consumer<? super B> modifier) {
45         return toBuilder().applyMutation(modifier::accept).build();
46     }
47 }
48