-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathReactiveReadStreamImpl.java
166 lines (145 loc) · 3.99 KB
/
ReactiveReadStreamImpl.java
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
/*
* Copyright 2014 Red Hat, Inc.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* You may elect to redistribute this code under either of these licenses.
*/
package io.vertx.ext.reactivestreams.impl;
import io.vertx.core.Handler;
import io.vertx.ext.reactivestreams.ReactiveReadStream;
import org.reactivestreams.Subscription;
import java.util.ArrayDeque;
import java.util.Queue;
/**
* @author <a href="mailto:[email protected]">Nick Scavelli</a>
* @author <a href="http://tfox.org">Tim Fox</a>
*/
public class ReactiveReadStreamImpl<T> implements ReactiveReadStream<T> {
private final long batchSize;
private Handler<T> dataHandler;
private Handler<Void> endHandler;
private Handler<Throwable> exceptionHandler;
private Subscription subscription;
private final Queue<T> pending = new ArrayDeque<>();
private long demand = Long.MAX_VALUE;
private long tokens;
public ReactiveReadStreamImpl(long batchSize) {
this.batchSize = batchSize;
}
public synchronized ReactiveReadStream<T> handler(Handler<T> handler) {
this.dataHandler = handler;
if (dataHandler != null && demand > 0L) {
checkRequestTokens();
}
return this;
}
@Override
public synchronized ReactiveReadStream<T> pause() {
this.demand = 0L;
return this;
}
@Override
public ReactiveReadStream<T> fetch(long amount) {
if (amount > 0L) {
demand += amount;
if (demand < 0L) {
demand = Long.MAX_VALUE;
}
T data;
while (demand > 0L && (data = pending.poll()) != null) {
if (demand != Long.MAX_VALUE) {
demand--;
}
handleData(data);
}
checkRequestTokens();
}
return this;
}
@Override
public synchronized ReactiveReadStream<T> resume() {
return fetch(Long.MAX_VALUE);
}
@Override
public synchronized ReactiveReadStream<T> endHandler(Handler<Void> endHandler) {
this.endHandler = endHandler;
return this;
}
@Override
public synchronized ReactiveReadStream<T> exceptionHandler(Handler<Throwable> handler) {
this.exceptionHandler = handler;
return this;
}
@Override
public synchronized void onSubscribe(Subscription subscription) {
if (subscription == null) {
throw new NullPointerException("subscription");
}
if (this.subscription != null) {
subscription.cancel();
} else {
this.subscription = subscription;
}
}
@Override
public synchronized void onNext(T data) {
if (data == null) {
throw new NullPointerException("data");
}
checkUnsolicitedTokens();
if (demand > 0L) {
if (demand != Long.MAX_VALUE) {
demand--;
}
if (pending.size() > 0) {
pending.add(data);
data = pending.poll();
}
handleData(data);
} else {
pending.add(data);
}
}
@Override
public synchronized void onError(Throwable throwable) {
if (throwable == null) {
throw new NullPointerException("throwable");
}
if (exceptionHandler != null) {
exceptionHandler.handle(throwable);
}
}
@Override
public synchronized void onComplete() {
if (endHandler != null) {
endHandler.handle(null);
}
}
protected void checkUnsolicitedTokens() {
if (tokens == 0) {
throw new IllegalStateException("Data received but wasn't requested");
}
}
private synchronized void handleData(T data) {
if (dataHandler != null) {
dataHandler.handle(data);
tokens--;
checkRequestTokens();
}
}
private void checkRequestTokens() {
if (demand > 0L && subscription != null && tokens == 0) {
tokens = batchSize;
subscription.request(batchSize);
}
}
}