View Javadoc
1   package org.opentrafficsim.sim0mq.publisher;
2   
3   import org.djutils.exceptions.Throw;
4   import org.djutils.metadata.MetaData;
5   
6   /**
7    * Common code for most implementations of TranceiverInterface.
8    * <p>
9    * Copyright (c) 2020-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
10   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
11   * </p>
12   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
13   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
14   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
15   */
16  public abstract class AbstractTransceiver implements TransceiverInterface
17  {
18      /** The address field descriptors. */
19      private final MetaData addressFields;
20  
21      /** The result field descriptors. */
22      private final MetaData resultFields;
23  
24      /** The id of the AbstractTransceiver. */
25      private final String id;
26  
27      /**
28       * Construct a new AbstractTransceiver.
29       * @param id the id of the new AbstractTransceiver
30       * @param addressFields description of the elements of an address that the <code>get</code> method of this
31       *            AbstractTransceiver can handle
32       * @param resultFields description of the result of the <code>get</code> method
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      @Override
45      public final String getId()
46      {
47          return this.id;
48      }
49  
50      @Override
51      public final MetaData getAddressFields()
52      {
53          return this.addressFields;
54      }
55  
56      @Override
57      public final MetaData getResultFields()
58      {
59          return this.resultFields;
60      }
61  
62      /**
63       * Verify the composition of an Object[].
64       * @param metaData the expected composition
65       * @param address the object array that must be verified
66       * @return null if metaData is OK; descriptive text on error
67       */
68      public static String verifyMetaData(final MetaData metaData, final Object[] address)
69      {
70          if (metaData.size() == 0 && (address == null || address.length == 0))
71          {
72              return null;
73          }
74          if (metaData.equals(MetaData.NO_META_DATA)) // anything goes
75          {
76              return null;
77          }
78          if (null == address)
79          {
80              return ("Address may not be null");
81          }
82          if (address.length != metaData.size())
83          {
84              return String.format("Address for %s has wrong length (expected %d address element%s, got %d address element%s)",
85                      metaData.getName(), metaData.size(), metaData.size() != 1 ? "s" : "", address.length,
86                      address.length != 1 ? "s" : "");
87          }
88          for (int index = 0; index < address.length; index++)
89          {
90              Object object = address[index];
91              if ((null != object) && (!(metaData.getObjectClass(index).isAssignableFrom(object.getClass()))))
92              {
93                  return String.format("objectArray[%d] (%s) cannot be used for %s", index, address[index],
94                          metaData.getObjectClass(index).getName());
95              }
96          }
97          return null;
98      }
99  
100     @Override
101     public String toString()
102     {
103         return "AbstractTransceiver [id=" + this.id + ", addressFields=" + this.addressFields + ", resultFields="
104                 + this.resultFields + "]";
105     }
106 
107 }