Skip to content

Commit 43d078f

Browse files
committed
gpclient: adding CollectorExpression
1 parent 8022b56 commit 43d078f

2 files changed

Lines changed: 112 additions & 0 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Copyright information and license terms for this software can be
3+
* found in the file LICENSE.TXT included with the distribution.
4+
*/
5+
package org.epics.gpclient;
6+
7+
/**
8+
* A {@link ChannelExpression} that exposes its {@link ReadCollector} and {@link WriteCollector}.
9+
*
10+
* @author carcassi
11+
*/
12+
public class CollectorExpression<R, C ,W> extends ChannelExpression<R, W> {
13+
14+
private boolean readConnected;
15+
private boolean writeConnected;
16+
17+
/**
18+
* A new collector expression.
19+
*
20+
*/
21+
CollectorExpression(ReadCollector<C, R> readCollector, WriteCollector<W> writeCollector) {
22+
super(readCollector, writeCollector);
23+
}
24+
25+
@Override
26+
protected void connectRead(PVDirector pvDirector) {
27+
readConnected = true;
28+
}
29+
30+
@Override
31+
protected void disconnectRead(PVDirector director) {
32+
}
33+
34+
@Override
35+
protected void connectWrite(PVDirector pvDirector) {
36+
writeConnected = true;
37+
}
38+
39+
@Override
40+
protected void disconnectWrite(PVDirector pvDirector) {
41+
}
42+
43+
/**
44+
* Retrieves the collector that can be used to send updates to the reader.
45+
* Note that the PVReader must first be started.
46+
*
47+
* @return the read collector
48+
*/
49+
@Override
50+
public ReadCollector<C, R> getReadCollector() {
51+
if (!readConnected) {
52+
throw new IllegalStateException("PVReader was not started");
53+
}
54+
return (ReadCollector<C, R>) super.getReadCollector();
55+
}
56+
57+
/**
58+
* Retrieves the collector that can be used to send/receives updates
59+
* from the writer. Note that the PVWrite must first be started.
60+
*
61+
* @return the write collecto
62+
*/
63+
@Override
64+
protected WriteCollector<W> getWriteCollector() {
65+
if (!writeConnected) {
66+
throw new IllegalStateException("PVWriter was not started");
67+
}
68+
return super.getWriteCollector();
69+
}
70+
71+
}

gpclient/gpclient-core/src/main/java/org/epics/gpclient/GPClient.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,47 @@ public static Expression<VType, Object> channel(String channelName) {
169169
return channel(channelName, cacheLastValue(VType.class));
170170
}
171171

172+
/**
173+
* An expression that allows to directly send/receive values to/from
174+
* PVReaders/PVWriters. This can be used for testing purpose or to integrate
175+
* data models that do not fit datasources or services.
176+
*
177+
* @param <R> the type to read
178+
* @param <C> the type to collect
179+
* @param <W> the type to write
180+
* @param readCollector the read buffer
181+
* @param writeCollector the write buffer
182+
* @return a new collector expression
183+
*/
184+
public static <R, C, W> CollectorExpression<R, C, W> collector(ReadCollector<C, R> readCollector, WriteCollector<W> writeCollector) {
185+
return new CollectorExpression<>(readCollector, writeCollector);
186+
}
187+
188+
/**
189+
* An expression that allows to directly send/receive values to/from
190+
* PVReaders/PVWriters. This can be used for testing purpose or to integrate
191+
* data models that do not fit datasources or services.
192+
*
193+
* @param <R> the type to read
194+
* @param <C> the type to collect
195+
* @param readCollector the read buffer
196+
* @return a new collector expression
197+
*/
198+
public static <R, C> CollectorExpression<R, C, Object> collector(ReadCollector<C, R> readCollector) {
199+
return collector(readCollector, new WriteCollector<>());
200+
}
201+
202+
/**
203+
* An expression that allows to directly send/receive values to/from
204+
* PVReaders/PVWriters. This can be used for testing purpose or to integrate
205+
* data models that do not fit datasources or services.
206+
*
207+
* @return a new collector expression
208+
*/
209+
public static CollectorExpression<VType, VType, Object> collector() {
210+
return collector(cacheLastValue(VType.class));
211+
}
212+
172213
/**
173214
* The default instance of the general purpose client.
174215
*

0 commit comments

Comments
 (0)