|
1 | 1 | /** |
2 | 2 | * Copyright 2013 Netflix, Inc. |
3 | | - * |
| 3 | + * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
6 | 6 | * You may obtain a copy of the License at |
7 | | - * |
| 7 | + * |
8 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | - * |
| 9 | + * |
10 | 10 | * Unless required by applicable law or agreed to in writing, software |
11 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
32 | 32 | import rx.util.functions.Func1; |
33 | 33 |
|
34 | 34 | /** |
35 | | - * Subject that publishes the last and all subsequent events to each {@link Observer} that subscribes. |
| 35 | + * Subject that publishes the most recent and all subsequent events to each subscribed {@link Observer}. |
| 36 | + * |
36 | 37 | * <p> |
37 | 38 | * Example usage: |
38 | 39 | * <p> |
39 | 40 | * <pre> {@code |
40 | | - |
| 41 | +
|
41 | 42 | // observer will receive all events. |
42 | 43 | BehaviorSubject<Object> subject = BehaviorSubject.createWithDefaultValue("default"); |
43 | 44 | subject.subscribe(observer); |
44 | 45 | subject.onNext("one"); |
45 | 46 | subject.onNext("two"); |
46 | 47 | subject.onNext("three"); |
47 | | - |
48 | | - // observer will receive the "one", "two" and "three" events. |
| 48 | +
|
| 49 | + // observer will receive the "one", "two" and "three" events, but not "zero" |
49 | 50 | BehaviorSubject<Object> subject = BehaviorSubject.createWithDefaultValue("default"); |
| 51 | + subject.onNext("zero"); |
50 | 52 | subject.onNext("one"); |
51 | 53 | subject.subscribe(observer); |
52 | 54 | subject.onNext("two"); |
53 | 55 | subject.onNext("three"); |
54 | | - |
| 56 | +
|
55 | 57 | } </pre> |
56 | | - * |
| 58 | + * |
57 | 59 | * @param <T> |
58 | 60 | */ |
59 | 61 | public class BehaviorSubject<T> extends Subject<T, T> { |
60 | 62 |
|
61 | 63 | /** |
62 | | - * Creates a {@link BehaviorSubject} which publishes the last and all subsequent events to each |
| 64 | + * Creates a {@link BehaviorSubject} which publishes the last and all subsequent events to each |
63 | 65 | * {@link Observer} that subscribes to it. |
64 | | - * |
| 66 | + * |
65 | 67 | * @param defaultValue |
66 | | - * The value which will be published to any {@link Observer} as long as the |
| 68 | + * The value which will be published to any {@link Observer} as long as the |
67 | 69 | * {@link BehaviorSubject} has not yet received any events. |
68 | 70 | * @return the constructed {@link BehaviorSubject}. |
69 | 71 | */ |
70 | 72 | public static <T> BehaviorSubject<T> createWithDefaultValue(T defaultValue) { |
71 | 73 | final ConcurrentHashMap<Subscription, Observer<T>> observers = new ConcurrentHashMap<Subscription, Observer<T>>(); |
72 | 74 |
|
73 | 75 | final AtomicReference<T> currentValue = new AtomicReference<T>(defaultValue); |
74 | | - |
| 76 | + |
75 | 77 | Func1<Observer<T>, Subscription> onSubscribe = new Func1<Observer<T>, Subscription>() { |
76 | 78 | @Override |
77 | 79 | public Subscription call(Observer<T> observer) { |
@@ -104,7 +106,7 @@ protected BehaviorSubject(AtomicReference<T> currentValue, Func1<Observer<T>, Su |
104 | 106 | this.currentValue = currentValue; |
105 | 107 | this.observers = observers; |
106 | 108 | } |
107 | | - |
| 109 | + |
108 | 110 | @Override |
109 | 111 | public void onCompleted() { |
110 | 112 | for (Observer<T> observer : observers.values()) { |
@@ -224,7 +226,7 @@ private void assertErrorObserver(Observer<String> aObserver) |
224 | 226 | verify(aObserver, times(1)).onNext("one"); |
225 | 227 | verify(aObserver, times(1)).onError(testException); |
226 | 228 | } |
227 | | - |
| 229 | + |
228 | 230 | @Test |
229 | 231 | public void testUnsubscribe() |
230 | 232 | { |
|
0 commit comments