/
FlatChainImpl.java
284 lines (239 loc) · 7.99 KB
/
FlatChainImpl.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* Copyright 2021 Refinitiv
*
* DISCLAIMER: This source code has been written by Refinitiv for the only
* purpose of illustrating articles published on the Refinitiv Developer
* Community. It has not been tested for usage in production environments.
* Refinitiv cannot be held responsible for any issues that may happen if
* these objects or the related source code is used in production or any other
* client environment.
*
* Refinitiv Developer Community: https://developers.refinitiv.com
*
*/
package com.refinitiv.platformservices.rt.objects.chain;
import com.refinitiv.ema.access.OmmConsumer;
import com.refinitiv.platformservices.rt.objects.common.Dispatcher;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
class FlatChainImpl implements FlatChain, ChainRecordContext, ChainRecordListener
{
private static final int WAITING_LOOP_TIMEOUT_IN_MS = 200;
private final OmmConsumer ommConsumer;
private final String name;
private final String serviceName;
private final boolean withUpdates;
private final SummaryLinksToSkipByDisplayTemplate summaryLinksToSkipByDisplayTemplate;
private final int nameGuessesCount;
private final boolean synchronousModeActivated;
private final boolean autoDispatch;
private final OnElementAddedFunction onElementAddedFunction;
private final OnElementChangedFunction onElementChangedFunction;
private final OnElementRemovedFunction onElementRemovedFunction;
private final OnCompleteFunction onCompleteFunction;
private final OnErrorFunction onErrorFunction;
private final ChainRecordFactory chainRecordFactory;
private ChainRecord firstChainRecord;
private enum State {OPENING, OPENED, CLOSING, CLOSED, IN_ERROR};
private State state;
private final Map<Long, String> elementsByPosition = new TreeMap<>();
private static Dispatcher dispatcher;
public FlatChainImpl(FlatChain.Builder builder)
{
ommConsumer = builder.ommConsumer;
name = builder.chainName;
serviceName = builder.serviceName;
withUpdates = builder.withUpdates;
summaryLinksToSkipByDisplayTemplate = builder.summaryLinksToSkipByDisplayTemplate;
nameGuessesCount = builder.nameGuessesCount;
synchronousModeActivated = builder.synchronousModeActivated;
autoDispatch = builder.autoDispatch;
onElementAddedFunction = builder.onElementAddedFunction;
onElementChangedFunction = builder.onElementChangedFunction;
onElementRemovedFunction = builder.onElementRemovedFunction;
onCompleteFunction = builder.onCompleteFunction;
onErrorFunction = builder.onErrorFunction;
ChainRecordContext chainRecordContext = this;
ChainRecordListener chainRecordListener = this;
chainRecordFactory = new ChainRecordFactoryImpl(chainRecordContext, chainRecordListener);
state = State.CLOSED;
dispatcher = new Dispatcher.Builder()
.withOmmConsumer(ommConsumer)
.build();
}
@Override
public String getName()
{
return name;
}
@Override
public String getServiceName()
{
return serviceName;
}
@Override
public void open()
{
synchronized(this)
{
if(state == State.OPENED || state == State.OPENING || state == State.IN_ERROR)
return;
state = State.OPENING;
firstChainRecord = chainRecordFactory.acquire(getName());
firstChainRecord.open();
}
if(synchronousModeActivated)
{
waitForCompletion();
}
}
@Override
public synchronized void close()
{
if(state == State.CLOSED || state == State.CLOSING)
return;
state = State.CLOSING;
elementsByPosition.clear();
firstChainRecord.close();
firstChainRecord = null;
chainRecordFactory.releaseAll();
}
@Override
public synchronized boolean isComplete()
{
if(state == State.OPENED || state == State.IN_ERROR)
{
return true;
}
return false;
}
@Override
public synchronized boolean isAChain()
{
if (firstChainRecord == null)
{
return false;
}
return firstChainRecord.isAChainRecord();
}
@Override
public synchronized Map<Long, String> getElements()
{
Map<Long, String> deepCopiedElementsToReturn = Collections.unmodifiableMap(elementsByPosition);
return deepCopiedElementsToReturn;
}
@Override
public OmmConsumer getOmmConsumer()
{
return ommConsumer;
}
@Override
public boolean getWithUpdates()
{
return withUpdates;
}
@Override
public SummaryLinksToSkipByDisplayTemplate getSummaryLinksToSkipByDisplayTemplate()
{
return summaryLinksToSkipByDisplayTemplate;
}
@Override
public int getNameGuessesCount()
{
return nameGuessesCount;
}
@Override
public OnElementAddedFunction getOnElementAddedFunction()
{
return onElementAddedFunction;
}
@Override
public OnElementChangedFunction getOnElementChangedFunction()
{
return onElementChangedFunction;
}
@Override
public OnElementRemovedFunction getOnElementRemovedFunction()
{
return onElementRemovedFunction;
}
@Override
public OnCompleteFunction getOnCompleteFunction()
{
return onCompleteFunction;
}
@Override
public OnErrorFunction getOnErrorFunction()
{
return onErrorFunction;
}
@Override
public synchronized void onLinkAdded(long position, String name, ChainRecord chainRecord)
{
if(state == State.CLOSED || state == State.CLOSING)
return;
elementsByPosition.put(position, name);
onElementAddedFunction.onElementAdded(position, name, this);
}
@Override
public synchronized void onLinkRemoved(long position, ChainRecord chainRecord)
{
if(state == State.CLOSED || state == State.CLOSING)
return;
elementsByPosition.remove(position);
onElementRemovedFunction.onElementRemoved(position, this);
}
@Override
public synchronized void onLinkChanged(long position, String previousName, String newName, ChainRecord chainRecord)
{
if(state == State.CLOSED || state == State.CLOSING)
return;
elementsByPosition.replace(position, newName);
onElementChangedFunction.onElementChanged(position, previousName, newName, this);
}
@Override
public synchronized void onCompleted(ChainRecord chainRecord)
{
if(state == State.OPENED || state == State.CLOSED || state == State.CLOSING)
return;
state = State.OPENED;
onCompleteFunction.onComplete(this);
}
@Override
public synchronized void onError(String errorMessage, ChainRecord chainRecord)
{
if(state == State.CLOSED || state == State.CLOSING)
return;
State previousState = state;
state = State.IN_ERROR;
onErrorFunction.onError(errorMessage, this);
if(previousState == State.OPENING)
{
onCompleteFunction.onComplete(this);
}
}
/**
* Wait for the completion of this MarketPrice either by sleeping
* (autoDispatch==false) or by dispatching events (autoDispatch==false)
*/
private void waitForCompletion()
{
do
{
if(autoDispatch)
{
dispatcher.dispatchEventsUntilComplete(this);
}
else
{
try
{
Thread.sleep(WAITING_LOOP_TIMEOUT_IN_MS);
}
catch (InterruptedException exception) {}
}
}
while (!isComplete());
}
}