1+ /*
2+ * Copyright 2018 the original author or authors.
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+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License
15+ */
16+ package net .jodah .failsafe ;
17+
18+ import java .util .Objects ;
19+
20+ /**
21+ * The result of an execution. Immutable.
22+ * <p>
23+ * Part of the Failsafe SPI.
24+ *
25+ * @author Jonathan Halterman
26+ */
27+ public class ExecutionResultWithException <E extends Throwable > extends ExecutionResult {
28+ public ExecutionResultWithException (Object result , E failure ) {
29+ this (result , failure , false , 0 , false , failure == null , failure == null );
30+ }
31+
32+ private ExecutionResultWithException (Object result , E failure , boolean nonResult , long waitNanos , boolean complete ,
33+ boolean success , Boolean successAll ) {
34+ super (result , failure , nonResult , waitNanos , complete , success , successAll );
35+ }
36+
37+ /**
38+ * Returns a an ExecutionResult with the {@code result} set, {@code completed} true and {@code success} true.
39+ */
40+ public static <E extends Throwable > ExecutionResultWithException <E > successWithException (Object result ) {
41+ return new ExecutionResultWithException <>(result , null );
42+ }
43+
44+ /**
45+ * Returns a an ExecutionResult with the {@code failure} set, {@code completed} true and {@code success} false.
46+ */
47+ public static <E extends Throwable > ExecutionResultWithException <E > failureWithException (E failure ) {
48+ return new ExecutionResultWithException <>(null , failure , false , 0 , true , false , false );
49+ }
50+
51+ @ SuppressWarnings ("unchecked" )
52+ public E getFailure () {
53+ return (E ) super .getFailure ();
54+ }
55+
56+ /**
57+ * Returns a copy of the ExecutionResult with a non-result, and completed and success set to true. Returns
58+ * {@code this} if {@link #success} and {@link #result} are unchanged.
59+ */
60+ ExecutionResultWithException <E > withNonResult () {
61+ return super .isSuccess () && super .getResult () == null && super .isNonResult () ?
62+ this :
63+ new ExecutionResultWithException <E >(null , null , true , super .getWaitNanos (), true , true , super .getSuccessAll ());
64+ }
65+
66+ /**
67+ * Returns a copy of the ExecutionResult with the {@code result} value, and completed and success set to true. Returns
68+ * {@code this} if {@link #success} and {@link #result} are unchanged.
69+ */
70+ public ExecutionResultWithException <E > withResult (Object result ) {
71+ return super .isSuccess () && ((super .getResult () == null && result == null ) || (super .getResult () != null && super .getResult ().equals (result ))) ?
72+ this :
73+ new ExecutionResultWithException <>(result , null , super .isNonResult (), super .getWaitNanos (), true , true , super .getSuccessAll ());
74+ }
75+
76+ /**
77+ * Returns a copy of the ExecutionResult with the value set to true, else this if nothing has changed.
78+ */
79+ @ SuppressWarnings ("unchecked" )
80+ public ExecutionResultWithException <E > withComplete () {
81+ return super .isComplete () ? this : new ExecutionResultWithException <E >(super .getResult (), (E ) super .getFailure (), super .isNonResult (), super .getWaitNanos (), true , super .isSuccess (), super .getSuccessAll ());
82+ }
83+
84+ /**
85+ * Returns a copy of the ExecutionResult with the {@code completed} and {@code success} values.
86+ */
87+ @ SuppressWarnings ("unchecked" )
88+ ExecutionResultWithException <E > with (boolean completed , boolean success ) {
89+ return super .isComplete () == completed && super .isSuccess () == success ?
90+ this :
91+ new ExecutionResultWithException <E >(super .getResult (), (E ) super .getFailure (), super .isNonResult (), super .getWaitNanos (), completed , success ,
92+ super .isSuccess () && super .getSuccessAll ());
93+ }
94+
95+ /**
96+ * Returns a copy of the ExecutionResult with the {@code waitNanos}, {@code completed} and {@code success} values.
97+ */
98+ @ SuppressWarnings ("unchecked" )
99+ public ExecutionResultWithException <E > with (long waitNanos , boolean completed , boolean success ) {
100+ return super .getWaitNanos () == waitNanos && super .isComplete () == completed && super .isSuccess () == success ?
101+ this :
102+ new ExecutionResultWithException <E >(super .getResult (), (E ) super .getFailure (), super .isNonResult (), waitNanos , completed , success ,
103+ super .isSuccess () && super .getSuccessAll ());
104+ }
105+
106+ @ Override
107+ public String toString () {
108+ return "ExecutionResult[" + "result=" + super .getResult () + ", failure=" + super .getFailure () + ", nonResult=" + super .isNonResult ()
109+ + ", waitNanos=" + super .getWaitNanos () + ", complete=" + super .isComplete () + ", success=" + super .isSuccess () + ", successAll=" + super .getSuccessAll ()
110+ + ']' ;
111+ }
112+
113+ @ Override
114+ public boolean equals (Object o ) {
115+ if (this == o )
116+ return true ;
117+ if (o == null || getClass () != o .getClass ())
118+ return false ;
119+ ExecutionResultWithException <E > that = (ExecutionResultWithException <E >) o ;
120+ return Objects .equals (super .getResult (), that .getResult ()) && Objects .equals (super .getFailure (), that .getFailure ());
121+ }
122+
123+ @ Override
124+ public int hashCode () {
125+ return Objects .hash (super .getResult (), super .getFailure ());
126+ }
127+ }
0 commit comments