• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.waiters;
17 
18 import java.util.Optional;
19 import software.amazon.awssdk.annotations.SdkPublicApi;
20 
21 /**
22  * Represents a value that can be either a response or a Throwable
23  *
24  * @param <R> response type
25  */
26 @SdkPublicApi
27 public final class ResponseOrException<R> {
28 
29     private final Optional<R> response;
30     private final Optional<Throwable> exception;
31 
ResponseOrException(Optional<R> response, Optional<Throwable> exception)32     private ResponseOrException(Optional<R> response, Optional<Throwable> exception) {
33         this.response = response;
34         this.exception = exception;
35     }
36 
37     /**
38      * @return the optional response that has matched with the waiter success condition
39      */
response()40     public Optional<R> response() {
41         return response;
42     }
43 
44     /**
45      * @return the optional exception that has matched with the waiter success condition
46      */
exception()47     public Optional<Throwable> exception() {
48         return exception;
49     }
50 
51     /**
52      * Create a new ResponseOrException with the response
53      *
54      * @param value response
55      * @param <R> Response type
56      */
response(R value)57     public static <R> ResponseOrException<R> response(R value) {
58         return new ResponseOrException<>(Optional.of(value), Optional.empty());
59     }
60 
61     /**
62      * Create a new ResponseOrException with the exception
63      *
64      * @param value exception
65      * @param <R> Response type
66      */
exception(Throwable value)67     public static <R> ResponseOrException<R> exception(Throwable value) {
68         return new ResponseOrException<>(Optional.empty(), Optional.of(value));
69     }
70 
71     @Override
equals(Object o)72     public boolean equals(Object o) {
73         if (this == o) {
74             return true;
75         }
76         if (!(o instanceof ResponseOrException)) {
77             return false;
78         }
79 
80         ResponseOrException<?> either = (ResponseOrException<?>) o;
81 
82         return response.equals(either.response) && exception.equals(either.exception);
83     }
84 
85     @Override
hashCode()86     public int hashCode() {
87         return 31 * response.hashCode() + exception.hashCode();
88     }
89 }
90 
91