View Javadoc
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   * Common code for id transceivers that use an empty address.
14   * <p>
15   * Copyright (c) 2020-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
16   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
17   * </p>
18   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
19   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
20   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
21   */
22  public abstract class AbstractIdTransceiver extends AbstractTransceiver
23  {
24      /** The network. */
25      private final Network network;
26  
27      /**
28       * Construct a GtuIdTransceiver.
29       * @param network Network; the OTS network
30       * @param id String; name of the IdTransceiver
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       * Retrieve the set of names of objects that can be individually subscribed to.
43       * @return Set&lt;?&gt;; the set of names of objects whose that can be subscribed to. Each object in this set should
44       *         implement <code>Identifiable</code>
45       */
46      abstract ImmutableSet<?> getSet();
47  
48      /** {@inheritDoc} */
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       * Retrieve the network.
71       * @return Network; the network
72       */
73      final Network getNetwork()
74      {
75          return this.network;
76      }
77  
78  }