|
16 | 16 | package rx.observables;
|
17 | 17 |
|
18 | 18 | import rx.Observable;
|
| 19 | +import rx.Scheduler; |
| 20 | +import rx.Subscriber; |
19 | 21 | import rx.functions.Func1;
|
20 | 22 |
|
21 | 23 | /**
|
22 |
| - * An {@link Observable} that has been grouped by key, the value of which can be obtained with |
23 |
| - * {@link #getKey()}. |
| 24 | + * An {@link Observable} that has been grouped by key, the value of which can be obtained with {@link #getKey()}. |
24 | 25 | * <p>
|
25 | 26 | * <em>Note:</em> A {@link GroupedObservable} will cache the items it is to emit until such time as it
|
26 |
| - * is subscribed to. For this reason, in order to avoid memory leaks, you should not simply ignore those |
27 |
| - * {@code GroupedObservable}s that do not concern you. Instead, you can signal to them that they may |
| 27 | + * is subscribed to. For this reason, in order to avoid memory leaks, you should not simply ignore those {@code GroupedObservable}s that do not concern you. Instead, you can signal to them that they |
| 28 | + * may |
28 | 29 | * discard their buffers by applying an operator like {@link Observable#take}{@code (0)} to them.
|
29 | 30 | *
|
30 | 31 | * @param <K>
|
|
37 | 38 | public class GroupedObservable<K, T> extends Observable<T> {
|
38 | 39 | private final K key;
|
39 | 40 |
|
40 |
| - public GroupedObservable(K key, OnSubscribe<T> onSubscribe) { |
| 41 | + /** |
| 42 | + * Converts an {@link Observable} into a {@code GroupedObservable} with a particular key. |
| 43 | + * |
| 44 | + * @param key |
| 45 | + * the key to identify the group of items emitted by this {@code GroupedObservable} |
| 46 | + * @param o |
| 47 | + * the {@link Observable} to convert |
| 48 | + * @return a {@code GroupedObservable} representation of {@code o}, with key {@code key} |
| 49 | + */ |
| 50 | + public static <K, T> GroupedObservable<K, T> from(K key, final Observable<T> o) { |
| 51 | + return new GroupedObservable<K, T>(key, new OnSubscribe<T>() { |
| 52 | + |
| 53 | + @Override |
| 54 | + public void call(Subscriber<? super T> s) { |
| 55 | + o.unsafeSubscribe(s); |
| 56 | + } |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Returns an Observable that will execute the specified function when a {@link Subscriber} subscribes to |
| 62 | + * it. |
| 63 | + * <p> |
| 64 | + * <img width="640" height="200" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/create.png" alt=""> |
| 65 | + * <p> |
| 66 | + * Write the function you pass to {@code create} so that it behaves as an Observable: It should invoke the |
| 67 | + * Subscriber's {@link Subscriber#onNext onNext}, {@link Subscriber#onError onError}, and {@link Subscriber#onCompleted onCompleted} methods appropriately. |
| 68 | + * <p> |
| 69 | + * A well-formed Observable must invoke either the Subscriber's {@code onCompleted} method exactly once or |
| 70 | + * its {@code onError} method exactly once. |
| 71 | + * <p> |
| 72 | + * See <a href="http://go.microsoft.com/fwlink/?LinkID=205219">Rx Design Guidelines (PDF)</a> for detailed |
| 73 | + * information. |
| 74 | + * <dl> |
| 75 | + * <dt><b>Scheduler:</b></dt> |
| 76 | + * <dd>{@code create} does not operate by default on a particular {@link Scheduler}.</dd> |
| 77 | + * </dl> |
| 78 | + * |
| 79 | + * @param <K> |
| 80 | + * the type of the key |
| 81 | + * @param <T> |
| 82 | + * the type of the items that this Observable emits |
| 83 | + * @param f |
| 84 | + * a function that accepts an {@code Subscriber<T>}, and invokes its {@code onNext}, {@code onError}, and {@code onCompleted} methods as appropriate |
| 85 | + * @return a GroupedObservable that, when a {@link Subscriber} subscribes to it, will execute the specified |
| 86 | + * function |
| 87 | + */ |
| 88 | + public final static <K, T> GroupedObservable<K, T> create(K key, OnSubscribe<T> f) { |
| 89 | + return new GroupedObservable<K, T>(key, f); |
| 90 | + } |
| 91 | + |
| 92 | + protected GroupedObservable(K key, OnSubscribe<T> onSubscribe) { |
41 | 93 | super(onSubscribe);
|
42 | 94 | this.key = key;
|
43 | 95 | }
|
|
0 commit comments