View Javadoc
1   package org.opentrafficsim.imb;
2   
3   import nl.tno.imb.TByteBuffer;
4   
5   /**
6    * Transmit an array of Object to the IMB hub.
7    * <p>
8    * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
9    * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
10   * <p>
11   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Oct 27, 2016 <br>
12   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
13   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
14   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
15   */
16  public class ObjectArrayToIMB
17  {
18  
19      /**
20       * Do not instantiate
21       */
22      private ObjectArrayToIMB()
23      {
24          // Cannot be instantiated
25      }
26  
27      /**
28       * Convert an array of Object to an IMB payload.
29       * @param data Object[]; the data to transmit
30       * @return TByteBuffer; the constructed IMB payload
31       * @throws IMBException
32       */
33      public static TByteBuffer objectArrayToIMBPayload(final Object[] data) throws IMBException
34      {
35          TByteBuffer payload = new TByteBuffer();
36          for (Object o : data)
37          {
38              switch (o.getClass().getName())
39              {
40                  case "java.lang.String":
41                      payload.prepare((String) o);
42                      break;
43  
44                  case "java.lang.Integer":
45                      payload.prepare((Integer) o);
46                      break;
47  
48                  case "java.lang.Long":
49                      payload.prepare((Long) o);
50                      break;
51  
52                  case "java.lang.Double":
53                      payload.prepare((Double) o);
54                      break;
55  
56                  case "java.lang.Float":
57                      payload.prepare((Float) o);
58                      break;
59  
60                  case "java.lang.Byte":
61                      payload.prepare((Byte) o);
62                      break;
63  
64                  case "java.lang.Character":
65                      payload.prepare((Character) o);
66                      break;
67  
68                  case "java.lang.Boolean":
69                      payload.prepare((Boolean) o);
70                      break;
71  
72                  default:
73                      if (o instanceof SelfWrapper)
74                      {
75                          ((SelfWrapper) o).prepare(payload);
76                      }
77                      else
78                      {
79                          throw new IMBException("don't know how to prepare " + o.getClass().getName());
80                      }
81                      break;
82              }
83          }
84          payload.prepareApply();
85          for (Object o : data)
86          {
87              switch (o.getClass().getName())
88              {
89                  case "java.lang.String":
90                      payload.qWrite((String) o);
91                      break;
92  
93                  case "java.lang.Integer":
94                      payload.qWrite((Integer) o);
95                      break;
96  
97                  case "java.lang.Long":
98                      payload.qWrite((Long) o);
99                      break;
100 
101                 case "java.lang.Double":
102                     payload.qWrite((Double) o);
103                     break;
104 
105                 case "java.lang.Float":
106                     payload.qWrite((Float) o);
107                     break;
108 
109                 case "java.lang.Byte":
110                     payload.qWrite((Byte) o);
111                     break;
112 
113                 case "java.lang.Character":
114                     payload.qWrite((Character) o);
115                     break;
116 
117                 case "java.lang.Boolean":
118                     payload.qWrite((Boolean) o);
119                     break;
120 
121                 default:
122                     if (o instanceof SelfWrapper)
123                     {
124                         ((SelfWrapper) o).qWrite(payload);
125                     }
126                     else
127                     {
128                         // Cannot happen; would have been caught in the prepare pass
129                         throw new IMBException("don't know how to qWrite " + o.getClass().getName());
130                     }
131                     break;
132             }
133         }
134         return payload;
135     }
136 
137 }