1 package org.opentrafficsim.sim0mq.publisher;
2
3 import org.djutils.base.Identifiable;
4 import org.djutils.exceptions.Throw;
5 import org.djutils.immutablecollections.ImmutableSet;
6 import org.djutils.metadata.MetaData;
7 import org.djutils.metadata.ObjectDescriptor;
8 import org.djutils.serialization.SerializationException;
9 import org.opentrafficsim.core.network.Network;
10 import org.sim0mq.Sim0MQException;
11
12
13
14
15
16
17
18
19
20
21
22 public abstract class AbstractIdTransceiver extends AbstractTransceiver
23 {
24
25 private final Network network;
26
27
28
29
30
31
32 public AbstractIdTransceiver(final Network network, final String id)
33 {
34 super(id, new MetaData("No address", "empty address", new ObjectDescriptor[0]),
35 new MetaData("No address", "empty address", new ObjectDescriptor[] {new ObjectDescriptor("String array",
36 "String array filled with all currently valid GTU ids", String[].class)}));
37 Throw.whenNull(network, "Network may not be null");
38 this.network = network;
39 }
40
41
42
43
44
45
46 abstract ImmutableSet<?> getSet();
47
48
49 @Override
50 public final Object[] get(final Object[] address, final ReturnWrapper returnWrapper)
51 throws Sim0MQException, SerializationException
52 {
53 String bad = verifyMetaData(getAddressFields(), address);
54 if (bad != null)
55 {
56 returnWrapper.nack(bad);
57 return null;
58 }
59 ImmutableSet<?> set = getSet();
60 Object[] result = new Object[set.size()];
61 int nextIndex = 0;
62 for (Object object : set)
63 {
64 result[nextIndex++] = ((Identifiable) object).getId();
65 }
66 return result;
67 }
68
69
70
71
72
73 final Network getNetwork()
74 {
75 return this.network;
76 }
77
78 }