forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObservers.java
More file actions
191 lines (164 loc) · 6.73 KB
/
Observers.java
File metadata and controls
191 lines (164 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.observers;
import rx.Observer;
import rx.exceptions.OnErrorNotImplementedException;
import rx.functions.Action0;
import rx.functions.Action1;
/**
* Helper methods and utilities for creating and working with {@link Observer} objects.
*/
public final class Observers {
private Observers() {
throw new IllegalStateException("No instances!");
}
private static final Observer<Object> EMPTY = new Observer<Object>() {
@Override
public final void onCompleted() {
// do nothing
}
@Override
public final void onError(Throwable e) {
throw new OnErrorNotImplementedException(e);
}
@Override
public final void onNext(Object args) {
// do nothing
}
};
/**
* Returns an inert {@link Observer} that does nothing in response to the emissions or notifications from
* any {@code Observable} it subscribes to but will throw an exception if its
* {@link Observer#onError onError} method is called.
*
* @return an inert {@code Observer}
*/
@SuppressWarnings("unchecked")
public static <T> Observer<T> empty() {
return (Observer<T>) EMPTY;
}
/**
* Creates an {@link Observer} that receives the emissions of any {@code Observable} it subscribes to via
* {@link Observer#onNext onNext} but ignores {@link Observer#onCompleted onCompleted} notifications;
* it will throw an {@link OnErrorNotImplementedException} if {@link Observer#onError onError} is invoked.
*
* @param onNext
* a function that handles each item emitted by an {@code Observable}
* @throws IllegalArgumentException
* if {@code onNext} is {@code null}
* @return an {@code Observer} that calls {@code onNext} for each emitted item from the {@code Observable}
* the {@code Observer} subscribes to
*/
public static final <T> Observer<T> create(final Action1<? super T> onNext) {
if (onNext == null) {
throw new IllegalArgumentException("onNext can not be null");
}
return new Observer<T>() {
@Override
public final void onCompleted() {
// do nothing
}
@Override
public final void onError(Throwable e) {
throw new OnErrorNotImplementedException(e);
}
@Override
public final void onNext(T args) {
onNext.call(args);
}
};
}
/**
* Creates an {@link Observer} that receives the emissions of any {@code Observable} it subscribes to via
* {@link Observer#onNext onNext} and handles any {@link Observer#onError onError} notification but ignores
* an {@link Observer#onCompleted onCompleted} notification.
*
* @param onNext
* a function that handles each item emitted by an {@code Observable}
* @param onError
* a function that handles an error notification if one is sent by an {@code Observable}
* @throws IllegalArgumentException
* if either {@code onNext} or {@code onError} are {@code null}
* @return an {@code Observer} that calls {@code onNext} for each emitted item from the {@code Observable}
* the {@code Observer} subscribes to, and calls {@code onError} if the {@code Observable} notifies
* of an error
*/
public static final <T> Observer<T> create(final Action1<? super T> onNext, final Action1<Throwable> onError) {
if (onNext == null) {
throw new IllegalArgumentException("onNext can not be null");
}
if (onError == null) {
throw new IllegalArgumentException("onError can not be null");
}
return new Observer<T>() {
@Override
public final void onCompleted() {
// do nothing
}
@Override
public final void onError(Throwable e) {
onError.call(e);
}
@Override
public final void onNext(T args) {
onNext.call(args);
}
};
}
/**
* Creates an {@link Observer} that receives the emissions of any {@code Observable} it subscribes to via
* {@link Observer#onNext onNext} and handles any {@link Observer#onError onError} or
* {@link Observer#onCompleted onCompleted} notifications.
*
* @param onNext
* a function that handles each item emitted by an {@code Observable}
* @param onError
* a function that handles an error notification if one is sent by an {@code Observable}
* @param onComplete
* a function that handles a sequence complete notification if one is sent by an {@code Observable}
* @throws IllegalArgumentException
* if either {@code onNext}, {@code onError}, or {@code onComplete} are {@code null}
* @return an {@code Observer} that calls {@code onNext} for each emitted item from the {@code Observable}
* the {@code Observer} subscribes to, calls {@code onError} if the {@code Observable} notifies
* of an error, and calls {@code onComplete} if the {@code Observable} notifies that the observable
* sequence is complete
*/
public static final <T> Observer<T> create(final Action1<? super T> onNext, final Action1<Throwable> onError, final Action0 onComplete) {
if (onNext == null) {
throw new IllegalArgumentException("onNext can not be null");
}
if (onError == null) {
throw new IllegalArgumentException("onError can not be null");
}
if (onComplete == null) {
throw new IllegalArgumentException("onComplete can not be null");
}
return new Observer<T>() {
@Override
public final void onCompleted() {
onComplete.call();
}
@Override
public final void onError(Throwable e) {
onError.call(e);
}
@Override
public final void onNext(T args) {
onNext.call(args);
}
};
}
}