View Javadoc
1   package org.sim0mq.message.federationmanager;
2   
3   import org.djutils.exceptions.Throw;
4   import org.sim0mq.Sim0MQException;
5   import org.sim0mq.message.Sim0MQMessage;
6   
7   /**
8    * SetParameter, FM.3. Message sent by the FederateManager to the Model for setting the parameter values. Parameters are set one
9    * by one (but can be a Vector or Matrix).
10   * <p>
11   * Copyright (c) 2019-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
12   * BSD-style license. See <a href="http://sim0mq.org/docs/current/license.html">Sim0MQ License</a>.
13   * </p>
14   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
15   */
16  public class FM3SetParameterMessage extends Sim0MQMessage
17  {
18      /** Name of the parameter to be set. Links to a parameter name in the model. */
19      private final String parameterName;
20  
21      /** Value of the parameter to be set; can be any of the legal types in djutils-serialization. */
22      private final Object parameterValue;
23  
24      /** the unique message id. */
25      private static final String MESSAGETYPE = "FM.3";
26  
27      /** */
28      private static final long serialVersionUID = 20190712L;
29  
30      /**
31       * @param federationId the federation id can be coded using different types. Examples are two 64-bit longs indicating a
32       *            UUID, or a String with a UUID number, a String with meaningful identification, or a short or an int with a
33       *            simulation run number.
34       * @param senderId The sender id can be used to send back a message to the sender at some later time.
35       * @param receiverId The receiver id can be used to check whether the message is meant for us, or should be discarded (or an
36       *            error can be sent if we receive a message not meant for us).
37       * @param messageId The unique message number is meant to confirm with a callback that the message has been received
38       *            correctly. The number is unique for the sender, so not globally within the federation.
39       * @param parameterName String; Name of the parameter to be set. Links to a parameter name in the model
40       * @param parameterValue Object; Value of the parameter to be set; can be any of the legal types in djutils-serialization
41       * @throws Sim0MQException on unknown data type
42       * @throws NullPointerException when one of the parameters is null
43       */
44      public FM3SetParameterMessage(final Object federationId, final Object senderId, final Object receiverId,
45              final Object messageId, final String parameterName, final Object parameterValue)
46              throws Sim0MQException, NullPointerException
47      {
48          this(new Object[] {Sim0MQMessage.VERSION, true, federationId, senderId, receiverId, MESSAGETYPE, messageId, 2,
49                  parameterName, parameterValue});
50      }
51  
52      /**
53       * @param objectArray Object[]; Full message object array
54       * @throws Sim0MQException on unknown data type
55       * @throws NullPointerException when one of the parameters is null
56       */
57      public FM3SetParameterMessage(final Object[] objectArray) throws Sim0MQException, NullPointerException
58      {
59          super(objectArray, 2, MESSAGETYPE);
60          Throw.when(!(objectArray[8] instanceof String), Sim0MQException.class, "parameterName (field 8) should be String");
61          this.parameterName = objectArray[8].toString();
62          this.parameterValue = objectArray[9];
63      }
64  
65      /**
66       * @return parameterName
67       */
68      public String getParameterName()
69      {
70          return this.parameterName;
71      }
72  
73      /**
74       * @return parameterValue
75       */
76      public Object getParameterValue()
77      {
78          return this.parameterValue;
79      }
80  
81      /**
82       * Builder for the SetParameter Message. Can string setters together, and call build() at the end to build the actual
83       * message.
84       * <p>
85       * Copyright (c) 2019-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
86       * <br>
87       * BSD-style license. See <a href="http://sim0mq.org/docs/current/license.html">Sim0MQ License</a>.
88       * </p>
89       * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
90       */
91      public static class Builder extends Sim0MQMessage.Builder<FM3SetParameterMessage.Builder>
92      {
93          /** Name of the parameter to be set. Links to a parameter name in the model. */
94          private String parameterName;
95  
96          /** Value of the parameter to be set; can be any of the legal types in djutils-serialization. */
97          private Object parameterValue;
98  
99          /**
100          * Empty constructor.
101          */
102         public Builder()
103         {
104             // nothing to do.
105         }
106 
107         /**
108          * @param newParameterName set parameter name
109          * @return the original object for chaining
110          */
111         public final Builder setParameterName(final String newParameterName)
112         {
113             this.parameterName = newParameterName;
114             return this;
115         }
116 
117         /**
118          * @param newParameterValue set parameterValue
119          * @return the original object for chaining
120          */
121         public final Builder setParameterValue(final Object newParameterValue)
122         {
123             this.parameterValue = newParameterValue;
124             return this;
125         }
126 
127         @Override
128         public FM3SetParameterMessage build() throws Sim0MQException, NullPointerException
129         {
130             return new FM3SetParameterMessage(this.federationId, this.senderId, this.receiverId, this.messageId,
131                     this.parameterName, this.parameterValue);
132         }
133 
134     }
135 }