1 package org.opentrafficsim.sim0mq.publisher;
2
3 import org.djutils.exceptions.Throw;
4 import org.djutils.metadata.MetaData;
5
6
7
8
9
10
11
12
13
14
15
16 public abstract class AbstractTransceiver implements TransceiverInterface
17 {
18
19 private final MetaData addressFields;
20
21
22 private final MetaData resultFields;
23
24
25 private final String id;
26
27
28
29
30
31
32
33
34 public AbstractTransceiver(final String id, final MetaData addressFields, final MetaData resultFields)
35 {
36 Throw.whenNull(id, "id may not be null");
37 Throw.whenNull(addressFields, "addressFieldDescriptors may not be null");
38 Throw.whenNull(resultFields, "resultFieldDescriptors may not be null");
39 this.id = id;
40 this.addressFields = addressFields;
41 this.resultFields = resultFields;
42 }
43
44
45 @Override
46 public final String getId()
47 {
48 return this.id;
49 }
50
51
52 @Override
53 public final MetaData getAddressFields()
54 {
55 return this.addressFields;
56 }
57
58
59 @Override
60 public final MetaData getResultFields()
61 {
62 return this.resultFields;
63 }
64
65
66
67
68
69
70
71 public static String verifyMetaData(final MetaData metaData, final Object[] address)
72 {
73 if (metaData.size() == 0 && (address == null || address.length == 0))
74 {
75 return null;
76 }
77 if (metaData.equals(MetaData.NO_META_DATA))
78 {
79 return null;
80 }
81 if (null == address)
82 {
83 return ("Address may not be null");
84 }
85 if (address.length != metaData.size())
86 {
87 return String.format("Address for %s has wrong length (expected %d address element%s, got %d address element%s)",
88 metaData.getName(), metaData.size(), metaData.size() != 1 ? "s" : "", address.length,
89 address.length != 1 ? "s" : "");
90 }
91 for (int index = 0; index < address.length; index++)
92 {
93 Object object = address[index];
94 if ((null != object) && (!(metaData.getObjectClass(index).isAssignableFrom(object.getClass()))))
95 {
96 return String.format("objectArray[%d] (%s) cannot be used for %s", index, address[index],
97 metaData.getObjectClass(index).getName());
98 }
99 }
100 return null;
101 }
102
103
104 @Override
105 public String toString()
106 {
107 return "AbstractTransceiver [id=" + this.id + ", addressFields=" + this.addressFields + ", resultFields="
108 + this.resultFields + "]";
109 }
110
111 }