Skip to content

Commit b49943a

Browse files
Move Observer factory methods to Observers
Follow same pattern as rx.observables, rx.schedulers, rx.subjects, rx.subscriptions
1 parent 050e62a commit b49943a

2 files changed

Lines changed: 129 additions & 119 deletions

File tree

rxjava-core/src/main/java/rx/Observer.java

Lines changed: 0 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -74,125 +74,6 @@ protected Observer(Observer<?> op) {
7474
*/
7575
public abstract void onNext(T args);
7676

77-
/**
78-
* Create an empty Observer that ignores all events.
79-
*/
80-
public static final <T> Observer<T> create() {
81-
return new Observer<T>() {
82-
83-
@Override
84-
public final void onCompleted() {
85-
// do nothing
86-
}
87-
88-
@Override
89-
public final void onError(Throwable e) {
90-
throw new OnErrorNotImplementedException(e);
91-
}
92-
93-
@Override
94-
public final void onNext(T args) {
95-
// do nothing
96-
}
97-
98-
};
99-
}
100-
101-
/**
102-
* Create an Observer that receives `onNext` and ignores `onError` and `onCompleted`.
103-
*/
104-
public static final <T> Observer<T> create(final Action1<? super T> onNext) {
105-
if (onNext == null) {
106-
throw new IllegalArgumentException("onNext can not be null");
107-
}
108-
109-
return new Observer<T>() {
110-
111-
@Override
112-
public final void onCompleted() {
113-
// do nothing
114-
}
115-
116-
@Override
117-
public final void onError(Throwable e) {
118-
throw new OnErrorNotImplementedException(e);
119-
}
120-
121-
@Override
122-
public final void onNext(T args) {
123-
onNext.call(args);
124-
}
125-
126-
};
127-
}
128-
129-
/**
130-
* Create an Observer that receives `onNext` and `onError` and ignores `onCompleted`.
131-
*
132-
*/
133-
public static final <T> Observer<T> create(final Action1<? super T> onNext, final Action1<Throwable> onError) {
134-
if (onNext == null) {
135-
throw new IllegalArgumentException("onNext can not be null");
136-
}
137-
if (onError == null) {
138-
throw new IllegalArgumentException("onError can not be null");
139-
}
140-
141-
return new Observer<T>() {
142-
143-
@Override
144-
public final void onCompleted() {
145-
// do nothing
146-
}
147-
148-
@Override
149-
public final void onError(Throwable e) {
150-
onError.call(e);
151-
}
152-
153-
@Override
154-
public final void onNext(T args) {
155-
onNext.call(args);
156-
}
157-
158-
};
159-
}
160-
161-
/**
162-
* Create an Observer that receives `onNext`, `onError` and `onCompleted`.
163-
*
164-
*/
165-
public static final <T> Observer<T> create(final Action1<? super T> onNext, final Action1<Throwable> onError, final Action0 onComplete) {
166-
if (onNext == null) {
167-
throw new IllegalArgumentException("onNext can not be null");
168-
}
169-
if (onError == null) {
170-
throw new IllegalArgumentException("onError can not be null");
171-
}
172-
if (onComplete == null) {
173-
throw new IllegalArgumentException("onComplete can not be null");
174-
}
175-
176-
return new Observer<T>() {
177-
178-
@Override
179-
public final void onCompleted() {
180-
onComplete.call();
181-
}
182-
183-
@Override
184-
public final void onError(Throwable e) {
185-
onError.call(e);
186-
}
187-
188-
@Override
189-
public final void onNext(T args) {
190-
onNext.call(args);
191-
}
192-
193-
};
194-
}
195-
19677
/**
19778
* Used to register an unsubscribe callback.
19879
*/
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package rx.observers;
2+
3+
import rx.Observer;
4+
import rx.util.OnErrorNotImplementedException;
5+
import rx.util.functions.Action0;
6+
import rx.util.functions.Action1;
7+
8+
public class Observers {
9+
10+
/**
11+
* Create an empty Observer that ignores all events.
12+
*/
13+
public static final <T> Observer<T> create() {
14+
return new Observer<T>() {
15+
16+
@Override
17+
public final void onCompleted() {
18+
// do nothing
19+
}
20+
21+
@Override
22+
public final void onError(Throwable e) {
23+
throw new OnErrorNotImplementedException(e);
24+
}
25+
26+
@Override
27+
public final void onNext(T args) {
28+
// do nothing
29+
}
30+
31+
};
32+
}
33+
34+
/**
35+
* Create an Observer that receives `onNext` and ignores `onError` and `onCompleted`.
36+
*/
37+
public static final <T> Observer<T> create(final Action1<? super T> onNext) {
38+
if (onNext == null) {
39+
throw new IllegalArgumentException("onNext can not be null");
40+
}
41+
42+
return new Observer<T>() {
43+
44+
@Override
45+
public final void onCompleted() {
46+
// do nothing
47+
}
48+
49+
@Override
50+
public final void onError(Throwable e) {
51+
throw new OnErrorNotImplementedException(e);
52+
}
53+
54+
@Override
55+
public final void onNext(T args) {
56+
onNext.call(args);
57+
}
58+
59+
};
60+
}
61+
62+
/**
63+
* Create an Observer that receives `onNext` and `onError` and ignores `onCompleted`.
64+
*
65+
*/
66+
public static final <T> Observer<T> create(final Action1<? super T> onNext, final Action1<Throwable> onError) {
67+
if (onNext == null) {
68+
throw new IllegalArgumentException("onNext can not be null");
69+
}
70+
if (onError == null) {
71+
throw new IllegalArgumentException("onError can not be null");
72+
}
73+
74+
return new Observer<T>() {
75+
76+
@Override
77+
public final void onCompleted() {
78+
// do nothing
79+
}
80+
81+
@Override
82+
public final void onError(Throwable e) {
83+
onError.call(e);
84+
}
85+
86+
@Override
87+
public final void onNext(T args) {
88+
onNext.call(args);
89+
}
90+
91+
};
92+
}
93+
94+
/**
95+
* Create an Observer that receives `onNext`, `onError` and `onCompleted`.
96+
*
97+
*/
98+
public static final <T> Observer<T> create(final Action1<? super T> onNext, final Action1<Throwable> onError, final Action0 onComplete) {
99+
if (onNext == null) {
100+
throw new IllegalArgumentException("onNext can not be null");
101+
}
102+
if (onError == null) {
103+
throw new IllegalArgumentException("onError can not be null");
104+
}
105+
if (onComplete == null) {
106+
throw new IllegalArgumentException("onComplete can not be null");
107+
}
108+
109+
return new Observer<T>() {
110+
111+
@Override
112+
public final void onCompleted() {
113+
onComplete.call();
114+
}
115+
116+
@Override
117+
public final void onError(Throwable e) {
118+
onError.call(e);
119+
}
120+
121+
@Override
122+
public final void onNext(T args) {
123+
onNext.call(args);
124+
}
125+
126+
};
127+
}
128+
129+
}

0 commit comments

Comments
 (0)