|
50 | 50 | * @param <T>
|
51 | 51 | */
|
52 | 52 | public final class ReplaySubject<T> extends Subject<T, T> {
|
53 |
| - |
54 | 53 | public static <T> ReplaySubject<T> create() {
|
| 54 | + return create(16); |
| 55 | + } |
| 56 | + public static <T> ReplaySubject<T> create(int initialCapacity) { |
55 | 57 | final SubjectSubscriptionManager<T> subscriptionManager = new SubjectSubscriptionManager<T>();
|
56 |
| - final ReplayState<T> state = new ReplayState<T>(); |
| 58 | + final ReplayState<T> state = new ReplayState<T>(initialCapacity); |
57 | 59 |
|
58 | 60 | OnSubscribeFunc<T> onSubscribe = subscriptionManager.getOnSubscribeFunc(
|
59 | 61 | /**
|
@@ -91,9 +93,13 @@ public void call(SubjectObserver<? super T> o) {
|
91 | 93 |
|
92 | 94 | private static class ReplayState<T> {
|
93 | 95 | // single-producer, multi-consumer
|
94 |
| - final History<T> history = new History<T>(); |
| 96 | + final History<T> history; |
95 | 97 | // each Observer is tracked here for what events they have received
|
96 |
| - final ConcurrentHashMap<Observer<? super T>, Integer> replayState = new ConcurrentHashMap<Observer<? super T>, Integer>(); |
| 98 | + final ConcurrentHashMap<Observer<? super T>, Integer> replayState; |
| 99 | + public ReplayState(int initialCapacity) { |
| 100 | + history = new History<T>(initialCapacity); |
| 101 | + replayState = new ConcurrentHashMap<Observer<? super T>, Integer>(); |
| 102 | + } |
97 | 103 | }
|
98 | 104 |
|
99 | 105 | private final SubjectSubscriptionManager<T> subscriptionManager;
|
@@ -197,10 +203,14 @@ private static <T> int replayObserverFromIndex(History<T> history, Integer l, Su
|
197 | 203 | * @param <T>
|
198 | 204 | */
|
199 | 205 | private static class History<T> {
|
200 |
| - private AtomicInteger index = new AtomicInteger(0); |
201 |
| - private final ArrayList<T> list = new ArrayList<T>(/* 1024 */); |
202 |
| - private AtomicReference<Notification<T>> terminalValue = new AtomicReference<Notification<T>>(); |
203 |
| - |
| 206 | + private final AtomicInteger index; |
| 207 | + private final ArrayList<T> list; |
| 208 | + private final AtomicReference<Notification<T>> terminalValue; |
| 209 | + public History(int initialCapacity) { |
| 210 | + index = new AtomicInteger(0); |
| 211 | + list = new ArrayList<T>(initialCapacity); |
| 212 | + terminalValue = new AtomicReference<Notification<T>>(); |
| 213 | + } |
204 | 214 | public boolean next(T n) {
|
205 | 215 | if (terminalValue.get() == null) {
|
206 | 216 | list.add(n);
|
|
0 commit comments