/
Response.java
76 lines (63 loc) · 1.59 KB
/
Response.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
package redis.clients.jedis;
import redis.clients.jedis.exceptions.JedisDataException;
public class Response<T> {
protected T response = null;
protected JedisDataException exception = null;
private boolean building = false;
private boolean built = false;
private boolean set = false;
private Builder<T> builder;
private Object data;
private Response<?> dependency = null;
public Response(Builder<T> b) {
this.builder = b;
}
public void set(Object data) {
this.data = data;
set = true;
}
public T get() {
// if response has dependency response and dependency is not built,
// build it first and no more!!
if (dependency != null && dependency.set && !dependency.built) {
dependency.build();
}
if (!set) {
throw new JedisDataException(
"Please close pipeline or multi block before calling this method.");
}
if (!built) {
build();
}
if (exception != null) {
throw exception;
}
return response;
}
public void setDependency(Response<?> dependency) {
this.dependency = dependency;
}
private void build() {
// check build state to prevent recursion
if (building) {
return;
}
building = true;
try {
if (data != null) {
if (data instanceof JedisDataException) {
exception = (JedisDataException) data;
} else {
response = builder.build(data);
}
}
data = null;
} finally {
building = false;
built = true;
}
}
public String toString() {
return "Response " + builder.toString();
}
}