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.retry.conditions;
17
18 import java.util.Collections;
19 import java.util.LinkedHashSet;
20 import java.util.Set;
21 import software.amazon.awssdk.annotations.SdkPublicApi;
22 import software.amazon.awssdk.core.retry.RetryPolicyContext;
23 import software.amazon.awssdk.utils.ToString;
24
25 /**
26  * Composite retry condition that evaluates to true if any containing condition evaluates to true.
27  */

28 @SdkPublicApi
29 public final class OrRetryCondition implements RetryCondition {
30
31     private final Set<RetryCondition> conditions = new LinkedHashSet<>();
32
33     private OrRetryCondition(RetryCondition... conditions) {
34         Collections.addAll(this.conditions, conditions);
35     }
36
37     public static OrRetryCondition create(RetryCondition... conditions) {
38         return new OrRetryCondition(conditions);
39     }
40
41     /**
42      * @return True if any condition returns true. False otherwise.
43      */

44     @Override
45     public boolean shouldRetry(RetryPolicyContext context) {
46         return conditions.stream().anyMatch(r -> r.shouldRetry(context));
47     }
48
49     @Override
50     public void requestWillNotBeRetried(RetryPolicyContext context) {
51         conditions.forEach(c -> c.requestWillNotBeRetried(context));
52     }
53
54     @Override
55     public void requestSucceeded(RetryPolicyContext context) {
56         conditions.forEach(c -> c.requestSucceeded(context));
57     }
58
59     @Override
60     public boolean equals(Object o) {
61         if (this == o) {
62             return true;
63         }
64         if (o == null || getClass() != o.getClass()) {
65             return false;
66         }
67
68         OrRetryCondition that = (OrRetryCondition) o;
69
70         return conditions.equals(that.conditions);
71     }
72
73     @Override
74     public int hashCode() {
75         return conditions.hashCode();
76     }
77
78     @Override
79     public String toString() {
80         return ToString.builder("OrRetryCondition")
81                        .add("conditions", conditions)
82                        .build();
83     }
84 }
85