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://tudelft.nl/staff/p.knoppers-1">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 String; the id of the new AbstractTransceiver
30       * @param addressFields MetaData; description of the elements of an address that the <code>get</code> method of this
31       *            AbstractTransceiver can handle
32       * @param resultFields MetaData; 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      /** {@inheritDoc} */
45      @Override
46      public final String getId()
47      {
48          return this.id;
49      }
50  
51      /** {@inheritDoc} */
52      @Override
53      public final MetaData getAddressFields()
54      {
55          return this.addressFields;
56      }
57  
58      /** {@inheritDoc} */
59      @Override
60      public final MetaData getResultFields()
61      {
62          return this.resultFields;
63      }
64  
65      /**
66       * Verify the composition of an Object[].
67       * @param metaData MetaData; the expected composition
68       * @param address Object[]; the object array that must be verified
69       * @return String; null if metaData is OK; descriptive text on error
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)) // anything goes
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     /** {@inheritDoc} */
104     @Override
105     public String toString()
106     {
107         return "AbstractTransceiver [id=" + this.id + ", addressFields=" + this.addressFields + ", resultFields="
108                 + this.resultFields + "]";
109     }
110 
111 }