Skip to content

Commit e5b7f3d

Browse files
committed
Added common throwIfAny to throw 0 or more exceptions
1 parent 4aadb55 commit e5b7f3d

File tree

9 files changed

+51
-113
lines changed

9 files changed

+51
-113
lines changed

src/main/java/rx/exceptions/Exceptions.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
*/
1616
package rx.exceptions;
1717

18-
import java.util.HashSet;
19-
import java.util.Set;
18+
import java.util.*;
2019

2120
/**
2221
* @warn javadoc class description missing
@@ -148,5 +147,32 @@ public static Throwable getFinalCause(Throwable e) {
148147
}
149148
return e;
150149
}
151-
150+
/**
151+
* Throws a single or multiple exceptions contained in the collection, wrapping it into
152+
* {@code CompositeException} if necessary.
153+
* @param exceptions the collection of exceptions. If null or empty, no exception is thrown.
154+
* If the collection contains a single exception, that exception is either thrown as-is or wrapped into a
155+
* CompositeException. Multiple exceptions are wrapped into a CompositeException.
156+
* @param whileText the circumstance string to be appended to the thrown CompositeException, inserted after
157+
* the sentences "Exception" and "Multiple exceptions".
158+
*/
159+
public static void throwIfAny(Collection<? extends Throwable> exceptions, String whileText) {
160+
if (exceptions != null && !exceptions.isEmpty()) {
161+
if (exceptions.size() == 1) {
162+
Throwable t = exceptions.iterator().next();
163+
if (t instanceof RuntimeException) {
164+
throw (RuntimeException) t;
165+
} else
166+
if (t instanceof Error) {
167+
throw (Error) t;
168+
} else {
169+
throw new CompositeException(
170+
"Exception" + whileText, exceptions);
171+
}
172+
} else {
173+
throw new CompositeException(
174+
"Multiple exceptions" + whileText, exceptions);
175+
}
176+
}
177+
}
152178
}

src/main/java/rx/internal/operators/OperatorPublish.java

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,13 @@
1515
*/
1616
package rx.internal.operators;
1717

18-
import java.util.ArrayList;
19-
import java.util.LinkedHashMap;
20-
import java.util.List;
21-
import java.util.Map;
22-
import java.util.concurrent.atomic.AtomicLong;
23-
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
18+
import java.util.*;
19+
import java.util.concurrent.atomic.*;
2420

21+
import rx.*;
2522
import rx.Observable;
26-
import rx.Producer;
27-
import rx.Subscriber;
28-
import rx.Subscription;
29-
import rx.exceptions.CompositeException;
30-
import rx.exceptions.Exceptions;
31-
import rx.exceptions.MissingBackpressureException;
32-
import rx.functions.Action0;
33-
import rx.functions.Action1;
34-
import rx.functions.Func1;
23+
import rx.exceptions.*;
24+
import rx.functions.*;
3525
import rx.internal.util.RxRingBuffer;
3626
import rx.observables.ConnectableObservable;
3727
import rx.subscriptions.Subscriptions;
@@ -173,13 +163,7 @@ public void onError(Throwable e) {
173163
errors.add(e2);
174164
}
175165
}
176-
if (errors != null) {
177-
if (errors.size() == 1) {
178-
Exceptions.propagate(errors.get(0));
179-
} else {
180-
throw new CompositeException("Errors while emitting onError", errors);
181-
}
182-
}
166+
Exceptions.throwIfAny(errors, " while propagating an exception.");
183167
}
184168

185169
@Override

src/main/java/rx/internal/util/SubscriptionList.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import java.util.List;
2323

2424
import rx.Subscription;
25-
import rx.exceptions.CompositeException;
25+
import rx.exceptions.*;
2626

2727
/**
2828
* Subscription that represents a group of Subscriptions that are unsubscribed together.
@@ -106,20 +106,7 @@ private static void unsubscribeFromAll(Collection<Subscription> subscriptions) {
106106
es.add(e);
107107
}
108108
}
109-
if (es != null) {
110-
if (es.size() == 1) {
111-
Throwable t = es.get(0);
112-
if (t instanceof RuntimeException) {
113-
throw (RuntimeException) t;
114-
} else {
115-
throw new CompositeException(
116-
"Failed to unsubscribe to 1 or more subscriptions.", es);
117-
}
118-
} else {
119-
throw new CompositeException(
120-
"Failed to unsubscribe to 2 or more subscriptions.", es);
121-
}
122-
}
109+
Exceptions.throwIfAny(es, " while unsubscribing.");
123110
}
124111
/* perf support */
125112
public void clear() {

src/main/java/rx/internal/util/SubscriptionRandomList.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import java.util.Set;
2424

2525
import rx.Subscription;
26-
import rx.exceptions.CompositeException;
26+
import rx.exceptions.*;
2727
import rx.functions.Action1;
2828

2929
/**
@@ -155,19 +155,6 @@ private static <T extends Subscription> void unsubscribeFromAll(Collection<T> su
155155
es.add(e);
156156
}
157157
}
158-
if (es != null) {
159-
if (es.size() == 1) {
160-
Throwable t = es.get(0);
161-
if (t instanceof RuntimeException) {
162-
throw (RuntimeException) t;
163-
} else {
164-
throw new CompositeException(
165-
"Failed to unsubscribe to 1 or more subscriptions.", es);
166-
}
167-
} else {
168-
throw new CompositeException(
169-
"Failed to unsubscribe to 2 or more subscriptions.", es);
170-
}
171-
}
158+
Exceptions.throwIfAny(es, " while unsubscribing.");
172159
}
173160
}

src/main/java/rx/subjects/AsyncSubject.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@
1515
*/
1616
package rx.subjects;
1717

18-
import java.util.ArrayList;
19-
import java.util.List;
18+
import java.util.*;
2019

2120
import rx.Observer;
2221
import rx.annotations.Experimental;
23-
import rx.exceptions.CompositeException;
2422
import rx.exceptions.Exceptions;
2523
import rx.functions.Action1;
2624
import rx.internal.operators.NotificationLite;
@@ -122,13 +120,7 @@ public void onError(final Throwable e) {
122120
}
123121
}
124122

125-
if (errors != null) {
126-
if (errors.size() == 1) {
127-
Exceptions.propagate(errors.get(0));
128-
} else {
129-
throw new CompositeException("Errors while emitting AsyncSubject.onError", errors);
130-
}
131-
}
123+
Exceptions.throwIfAny(errors, " while propagating an exception.");
132124
}
133125
}
134126

src/main/java/rx/subjects/BehaviorSubject.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@
1616
package rx.subjects;
1717

1818

19-
import java.util.ArrayList;
20-
import java.util.List;
19+
import java.util.*;
2120

2221
import rx.Observer;
2322
import rx.annotations.Experimental;
24-
import rx.exceptions.CompositeException;
2523
import rx.exceptions.Exceptions;
2624
import rx.functions.Action1;
2725
import rx.internal.operators.NotificationLite;
@@ -148,13 +146,7 @@ public void onError(Throwable e) {
148146
}
149147
}
150148

151-
if (errors != null) {
152-
if (errors.size() == 1) {
153-
Exceptions.propagate(errors.get(0));
154-
} else {
155-
throw new CompositeException("Errors while emitting AsyncSubject.onError", errors);
156-
}
157-
}
149+
Exceptions.throwIfAny(errors, " while propagating an exception.");
158150
}
159151
}
160152

src/main/java/rx/subjects/PublishSubject.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@
1515
*/
1616
package rx.subjects;
1717

18-
import java.util.ArrayList;
19-
import java.util.List;
18+
import java.util.*;
2019

2120
import rx.Observer;
2221
import rx.annotations.Experimental;
23-
import rx.exceptions.CompositeException;
2422
import rx.exceptions.Exceptions;
2523
import rx.functions.Action1;
2624
import rx.internal.operators.NotificationLite;
@@ -106,13 +104,7 @@ public void onError(final Throwable e) {
106104
errors.add(e2);
107105
}
108106
}
109-
if (errors != null) {
110-
if (errors.size() == 1) {
111-
Exceptions.propagate(errors.get(0));
112-
} else {
113-
throw new CompositeException("Errors while emitting PublishSubject.onError", errors);
114-
}
115-
}
107+
Exceptions.throwIfAny(errors, " while propagating an exception.");
116108
}
117109
}
118110

src/main/java/rx/subjects/ReplaySubject.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,15 @@
1616
package rx.subjects;
1717

1818
import java.lang.reflect.Array;
19-
import java.util.ArrayList;
20-
import java.util.List;
19+
import java.util.*;
2120
import java.util.concurrent.TimeUnit;
2221
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
2322

23+
import rx.*;
2424
import rx.Observer;
25-
import rx.Scheduler;
2625
import rx.annotations.Experimental;
27-
import rx.exceptions.CompositeException;
2826
import rx.exceptions.Exceptions;
29-
import rx.functions.Action1;
30-
import rx.functions.Func1;
27+
import rx.functions.*;
3128
import rx.internal.operators.NotificationLite;
3229
import rx.internal.util.UtilityFunctions;
3330
import rx.schedulers.Timestamped;
@@ -394,13 +391,7 @@ public void onError(final Throwable e) {
394391
}
395392
}
396393

397-
if (errors != null) {
398-
if (errors.size() == 1) {
399-
Exceptions.propagate(errors.get(0));
400-
} else {
401-
throw new CompositeException("Errors while emitting ReplaySubject.onError", errors);
402-
}
403-
}
394+
Exceptions.throwIfAny(errors, " while propagating an exception.");
404395
}
405396
}
406397

src/main/java/rx/subscriptions/CompositeSubscription.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import java.util.Set;
2424

2525
import rx.Subscription;
26-
import rx.exceptions.CompositeException;
26+
import rx.exceptions.*;
2727

2828
/**
2929
* Subscription that represents a group of Subscriptions that are unsubscribed together.
@@ -141,19 +141,6 @@ private static void unsubscribeFromAll(Collection<Subscription> subscriptions) {
141141
es.add(e);
142142
}
143143
}
144-
if (es != null) {
145-
if (es.size() == 1) {
146-
Throwable t = es.get(0);
147-
if (t instanceof RuntimeException) {
148-
throw (RuntimeException) t;
149-
} else {
150-
throw new CompositeException(
151-
"Failed to unsubscribe to 1 or more subscriptions.", es);
152-
}
153-
} else {
154-
throw new CompositeException(
155-
"Failed to unsubscribe to 2 or more subscriptions.", es);
156-
}
157-
}
144+
Exceptions.throwIfAny(es, " while unsubscribing.");
158145
}
159146
}

0 commit comments

Comments
 (0)