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.retry;
17
18 import software.amazon.awssdk.annotations.SdkInternalApi;
19 import software.amazon.awssdk.core.exception.SdkException;
20 import software.amazon.awssdk.core.retry.RetryUtils;
21 import software.amazon.awssdk.core.retry.conditions.TokenBucketExceptionCostFunction;
22 import software.amazon.awssdk.utils.ToString;
23 import software.amazon.awssdk.utils.Validate;
24
25 @SdkInternalApi
26 public class DefaultTokenBucketExceptionCostFunction implements TokenBucketExceptionCostFunction {
27     private final Integer throttlingExceptionCost;
28     private final int defaultExceptionCost;
29
30     private DefaultTokenBucketExceptionCostFunction(Builder builder) {
31         this.throttlingExceptionCost = builder.throttlingExceptionCost;
32         this.defaultExceptionCost = Validate.paramNotNull(builder.defaultExceptionCost, "defaultExceptionCost");
33     }
34
35     @Override
36     public Integer apply(SdkException e) {
37         if (throttlingExceptionCost != null && RetryUtils.isThrottlingException(e)) {
38             return throttlingExceptionCost;
39         }
40
41         return defaultExceptionCost;
42     }
43
44     @Override
45     public String toString() {
46         return ToString.builder("TokenBucketExceptionCostCalculator")
47                        .add("throttlingExceptionCost", throttlingExceptionCost)
48                        .add("defaultExceptionCost", defaultExceptionCost)
49                        .build();
50     }
51
52     @Override
53     public boolean equals(Object o) {
54         if (this == o) {
55             return true;
56         }
57         if (o == null || getClass() != o.getClass()) {
58             return false;
59         }
60
61         DefaultTokenBucketExceptionCostFunction that = (DefaultTokenBucketExceptionCostFunction) o;
62
63         if (defaultExceptionCost != that.defaultExceptionCost) {
64             return false;
65         }
66
67         return throttlingExceptionCost != null ?
68                throttlingExceptionCost.equals(that.throttlingExceptionCost) :
69                that.throttlingExceptionCost == null;
70     }
71
72     @Override
73     public int hashCode() {
74         int result = throttlingExceptionCost != null ? throttlingExceptionCost.hashCode() : 0;
75         result = 31 * result + defaultExceptionCost;
76         return result;
77     }
78
79     public static final class Builder implements TokenBucketExceptionCostFunction.Builder {
80         private Integer throttlingExceptionCost;
81         private Integer defaultExceptionCost;
82
83         public TokenBucketExceptionCostFunction.Builder throttlingExceptionCost(int cost) {
84             this.throttlingExceptionCost = cost;
85             return this;
86         }
87
88         public TokenBucketExceptionCostFunction.Builder defaultExceptionCost(int cost) {
89             this.defaultExceptionCost = cost;
90             return this;
91         }
92
93         public TokenBucketExceptionCostFunction build() {
94             return new DefaultTokenBucketExceptionCostFunction(this);
95         }
96
97     }
98 }
99