FM3SetParameterMessage.java

  1. package org.sim0mq.message.federationmanager;

  2. import org.djutils.exceptions.Throw;
  3. import org.sim0mq.Sim0MQException;
  4. import org.sim0mq.message.Sim0MQMessage;

  5. /**
  6.  * SetParameter, FM.3. Message sent by the FederateManager to the Model for setting the parameter values. Parameters are set one
  7.  * by one (but can be a Vector or Matrix).
  8.  * <p>
  9.  * Copyright (c) 2019-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  10.  * BSD-style license. See <a href="http://sim0mq.org/docs/current/license.html">Sim0MQ License</a>.
  11.  * </p>
  12.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  13.  */
  14. public class FM3SetParameterMessage extends Sim0MQMessage
  15. {
  16.     /** Name of the parameter to be set. Links to a parameter name in the model. */
  17.     private final String parameterName;

  18.     /** Value of the parameter to be set; can be any of the legal types in djutils-serialization. */
  19.     private final Object parameterValue;

  20.     /** the unique message id. */
  21.     private static final String MESSAGETYPE = "FM.3";

  22.     /** */
  23.     private static final long serialVersionUID = 20190712L;

  24.     /**
  25.      * @param federationId the federation id can be coded using different types. Examples are two 64-bit longs indicating a
  26.      *            UUID, or a String with a UUID number, a String with meaningful identification, or a short or an int with a
  27.      *            simulation run number.
  28.      * @param senderId The sender id can be used to send back a message to the sender at some later time.
  29.      * @param receiverId The receiver id can be used to check whether the message is meant for us, or should be discarded (or an
  30.      *            error can be sent if we receive a message not meant for us).
  31.      * @param messageId The unique message number is meant to confirm with a callback that the message has been received
  32.      *            correctly. The number is unique for the sender, so not globally within the federation.
  33.      * @param parameterName String; Name of the parameter to be set. Links to a parameter name in the model
  34.      * @param parameterValue Object; Value of the parameter to be set; can be any of the legal types in djutils-serialization
  35.      * @throws Sim0MQException on unknown data type
  36.      * @throws NullPointerException when one of the parameters is null
  37.      */
  38.     public FM3SetParameterMessage(final Object federationId, final Object senderId, final Object receiverId,
  39.             final Object messageId, final String parameterName, final Object parameterValue)
  40.             throws Sim0MQException, NullPointerException
  41.     {
  42.         this(new Object[] {Sim0MQMessage.VERSION, true, federationId, senderId, receiverId, MESSAGETYPE, messageId, 2,
  43.                 parameterName, parameterValue});
  44.     }

  45.     /**
  46.      * @param objectArray Object[]; Full message object array
  47.      * @throws Sim0MQException on unknown data type
  48.      * @throws NullPointerException when one of the parameters is null
  49.      */
  50.     public FM3SetParameterMessage(final Object[] objectArray) throws Sim0MQException, NullPointerException
  51.     {
  52.         super(objectArray, 2, MESSAGETYPE);
  53.         Throw.when(!(objectArray[8] instanceof String), Sim0MQException.class, "parameterName (field 8) should be String");
  54.         this.parameterName = objectArray[8].toString();
  55.         this.parameterValue = objectArray[9];
  56.     }

  57.     /**
  58.      * @return parameterName
  59.      */
  60.     public String getParameterName()
  61.     {
  62.         return this.parameterName;
  63.     }

  64.     /**
  65.      * @return parameterValue
  66.      */
  67.     public Object getParameterValue()
  68.     {
  69.         return this.parameterValue;
  70.     }

  71.     /**
  72.      * Builder for the SetParameter Message. Can string setters together, and call build() at the end to build the actual
  73.      * message.
  74.      * <p>
  75.      * Copyright (c) 2019-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  76.      * <br>
  77.      * BSD-style license. See <a href="http://sim0mq.org/docs/current/license.html">Sim0MQ License</a>.
  78.      * </p>
  79.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  80.      */
  81.     public static class Builder extends Sim0MQMessage.Builder<FM3SetParameterMessage.Builder>
  82.     {
  83.         /** Name of the parameter to be set. Links to a parameter name in the model. */
  84.         private String parameterName;

  85.         /** Value of the parameter to be set; can be any of the legal types in djutils-serialization. */
  86.         private Object parameterValue;

  87.         /**
  88.          * Empty constructor.
  89.          */
  90.         public Builder()
  91.         {
  92.             // nothing to do.
  93.         }

  94.         /**
  95.          * @param newParameterName set parameter name
  96.          * @return the original object for chaining
  97.          */
  98.         public final Builder setParameterName(final String newParameterName)
  99.         {
  100.             this.parameterName = newParameterName;
  101.             return this;
  102.         }

  103.         /**
  104.          * @param newParameterValue set parameterValue
  105.          * @return the original object for chaining
  106.          */
  107.         public final Builder setParameterValue(final Object newParameterValue)
  108.         {
  109.             this.parameterValue = newParameterValue;
  110.             return this;
  111.         }

  112.         @Override
  113.         public FM3SetParameterMessage build() throws Sim0MQException, NullPointerException
  114.         {
  115.             return new FM3SetParameterMessage(this.federationId, this.senderId, this.receiverId, this.messageId,
  116.                     this.parameterName, this.parameterValue);
  117.         }

  118.     }
  119. }