Testing more complex struct and enum declarations; fixing subtle bugs
[iot2.git] / iotjava / iotrmi / Java / IoTRMIUtil.java
index a3e6b54de71fa16c28b7997b7b5e5d209ebffc7f..23385ba5199947039f7d68d398cb95fc4c02b063 100644 (file)
@@ -16,7 +16,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
-import iotrmi.IoTRMITypes;
 
 /** Class IoTRMI provides utility services.
  *  <p>
@@ -32,15 +31,16 @@ public class IoTRMIUtil {
         * Class Properties
         */
        private Map<String,String> mapPrimitives;
-       private Map<String,Integer> mapPrimitiveSizesJava;
-       private Map<String,Integer> mapPrimitiveSizesCplus;
+       private Map<String,Integer> mapPrimitiveSizes;
        private Map<String,String> mapNonPrimitives;
 
        /**
         * Class Constants
         */
+       public final static int OBJECT_ID_LEN = 4;      // 4 bytes = 32 bits
        public final static int METHOD_ID_LEN = 4;      // 4 bytes = 32 bits
        public final static int PARAM_LEN = 4;          // 4 bytes = 32 bits (4-byte field that stores the length of the param)
+       public final static int RETURN_LEN = 4;         // 4 bytes = 32 bits (4-byte field that stores the length of the return object)
 
        public final static int SHT_LEN = 2;
        public final static int INT_LEN = 4;
@@ -59,12 +59,9 @@ public class IoTRMIUtil {
                mapPrimitives = new HashMap<String,String>();
                        IoTRMITypes.arraysToMap(mapPrimitives, 
                                IoTRMITypes.primitivesJava, IoTRMITypes.primitivesCplus);
-               mapPrimitiveSizesJava = new HashMap<String,Integer>();
-                       IoTRMITypes.arraysToMap(mapPrimitiveSizesJava, 
-                               IoTRMITypes.primitivesJava, IoTRMITypes.primitivesJavaSizes);
-               mapPrimitiveSizesCplus = new HashMap<String,Integer>();
-                       IoTRMITypes.arraysToMap(mapPrimitiveSizesCplus, 
-                               IoTRMITypes.primitivesCplus, IoTRMITypes.primitivesCplusSizes);
+               mapPrimitiveSizes = new HashMap<String,Integer>();
+                       IoTRMITypes.arraysToMap(mapPrimitiveSizes, 
+                               IoTRMITypes.primitivesJava, IoTRMITypes.primitivesSizes);
                mapNonPrimitives = new HashMap<String,String>();
                        IoTRMITypes.arraysToMap(mapNonPrimitives, 
                                IoTRMITypes.nonPrimitivesJava, IoTRMITypes.nonPrimitivesCplus);
@@ -108,12 +105,10 @@ public class IoTRMIUtil {
         */
        public int getTypeSize(String type) {
 
-               if (mapPrimitiveSizesJava.containsKey(type))
-                       return mapPrimitiveSizesJava.get(type);
-               else if (mapPrimitiveSizesCplus.containsKey(type))
-                       return mapPrimitiveSizesCplus.get(type);
+               if (mapPrimitiveSizes.containsKey(type))
+                       return mapPrimitiveSizes.get(type);
                else
-                       return -1; // Size is unknown
+                       return -1; // Size is unknown (variable length)
        }
        
 
@@ -168,7 +163,7 @@ public class IoTRMIUtil {
        /**
         * getParamObject() converts byte array of certain object type into Object
         */
-       public static Object getParamObject(Class<?> type, Class<?> genTypeKey, Class<?> genTypeVal, byte[] paramBytes) {
+       public static Object getParamObject(Class<?> type, Class<?> genTypeVal, byte[] paramBytes) {
                
                Object retObj = null;
                if (type == byte.class ||
@@ -200,17 +195,9 @@ public class IoTRMIUtil {
                // Array
                } else if (type.isArray()) {
                        retObj = getParamObjectArray(type, paramBytes);
-               // Set
-               // e.g. Set<String> - type = Set.class, genTypeVal = String.class
-               } else if (type == Set.class) {
-                       retObj = getParamSetObject(genTypeVal, paramBytes);
                // List
                } else if (type == List.class) {
                        retObj = getParamListObject(genTypeVal, paramBytes);
-               // Map
-               // e.g. Map<String,Integer> - type = Map.class, genTypeKey = String.class, genTypeVal = Integer.class
-               } else if (type == Map.class) {
-                       retObj = getParamMapObject(genTypeKey, genTypeVal, paramBytes);
                } else
                        throw new Error("IoTRMIUtil: Unrecognizable type: " + type.getName());
                
@@ -275,6 +262,13 @@ public class IoTRMIUtil {
                } else if ( (type == String[].class) ||
                                        (type == String.class)) {
                        retObj = (Object) byteArrayToStringArray(paramBytes);
+               //} else if (type.isArray()) {
+               // This is an array but it's more than 1 dimension, e.g. 2-dimensional,
+               //              3-dimensional, etc.
+                       // for loop to check inner array perhaps using object
+                       // then call this function recursively
+                       // combine the result altogether
+
                } else
                        throw new Error("IoTRMIUtil: Unrecognizable type: " + type.getName());
                
@@ -289,7 +283,7 @@ public class IoTRMIUtil {
                
                byte[] retObjBytes = null;
                if (obj instanceof Byte) {
-                       retObjBytes = (byte[]) obj;
+                       retObjBytes = new byte[] { (byte) obj };
                } else if (obj instanceof Short) {
                        retObjBytes = shortToByteArray((short) obj);
                } else if (obj instanceof Integer) {
@@ -310,14 +304,14 @@ public class IoTRMIUtil {
                } else if (obj.getClass().isArray()) {
                        retObjBytes = getArrayObjectBytes(obj);
                // Set and its implementations
-               } else if (obj instanceof Set<?>) {
-                       retObjBytes = setToByteArray((Set<?>) obj);
+               /*} else if (obj instanceof Set<?>) {
+                       retObjBytes = setToByteArray((Set<?>) obj);*/
                // List and its implementations
                } else if (obj instanceof List<?>) {
                        retObjBytes = listToByteArray((List<?>) obj);
                // Map and its implementations
-               } else if (obj instanceof Map<?,?>) {
-                       retObjBytes = mapToByteArray((Map<?,?>) obj);
+               /*} else if (obj instanceof Map<?,?>) {
+                       retObjBytes = mapToByteArray((Map<?,?>) obj);*/
                } else
                        throw new Error("IoTRMIUtil: Unrecognizable object: " + obj.getClass());
 
@@ -373,7 +367,7 @@ public class IoTRMIUtil {
 
 
        // Collection data structures
-       public static byte[] setToByteArray(Set<?> set) {
+       /*public static byte[] setToByteArray(Set<?> set) {
 
                // Find out the class of the type
                Iterator<?> it = set.iterator();
@@ -403,7 +397,7 @@ public class IoTRMIUtil {
 
                byte[] arrObjBytes = getArrayObjectBytes(arrObj);
                return arrObjBytes;
-       }
+       }*/
 
 
        public static byte[] listToByteArray(List<?> list) {
@@ -440,7 +434,7 @@ public class IoTRMIUtil {
 
 
        // Convert keySet of a Map
-       public static byte[] mapKeyToByteArray(Map<?,?> map) {
+       /*public static byte[] mapKeyToByteArray(Map<?,?> map) {
 
                // Map<K,V>
                // Find out the class of the type for K
@@ -562,7 +556,7 @@ public class IoTRMIUtil {
                        throw new Error("IoTRMIUtil: Unrecognizable object: " + genericType.getSimpleName());
 
                return retSet;
-       }
+       }*/
 
 
        // Get a List object from bytes
@@ -605,7 +599,7 @@ public class IoTRMIUtil {
 
 
        // Get a Key array for Map object from bytes
-       public static Object getParamMapObject(Class<?> genTypeKey, Class<?> genTypeVal, byte[] paramBytes) {
+       /*public static Object getParamMapObject(Class<?> genTypeKey, Class<?> genTypeVal, byte[] paramBytes) {
 
                // The complete set of bytes always consists of all keys followed by all values - <K,V> pairs
                // Calculate number of elements
@@ -627,7 +621,7 @@ public class IoTRMIUtil {
                IoTRMITypes.arraysToMap(retMap, retObjKey, retObjVal);
 
                return retMap;
-       }
+       }*/
 
 
        /**
@@ -1310,19 +1304,41 @@ public class IoTRMIUtil {
 
        public static void main(String[] args) {
 
-               boolean data = false;
+               //boolean data = false;
                //char data = 'c';
-               //float data = 12.5123f;
+//             float data = 1234.123f;
                //double data = 12.51231234;
-               //long data = 123456781234l;
+               //long data = 1234l;
                //short data = 1234;
                //int data = 12345678;
-               byte[] result = booleanToByteArray(data);
-               System.out.println("Result: " + Arrays.toString(result));
-               System.out.println("Converted back: " + byteArrayToBoolean(result));
+//             byte[] result = floatToByteArray(data);
+//             System.out.println("Result: " + Arrays.toString(result));
+//             System.out.println("Converted back: " + byteArrayToFloat(result));
+               
+               //String str = "methodA(int,string,float,double,double)";
+               //int hash = str.hashCode();
+               //System.out.println("Hash value: " + hash);
+
+               int[][] multi = new int[][] {
+                       { 1, 2, 3 },
+                       { 6, 5, 4},
+                       { 11, 17, 13}
+               };
+
+               for (int[] inner : multi ) {
+                       System.out.println("New row!");
+                       for (Object i : inner) {
+                               System.out.println("Element i: " + i);
+                       }
+                       System.out.println("Change row!\n");
+               }
+
+               int[] int1 = { 1, 2, 3 };
+               int[] int2 = { 6, 5, 4 };
+               int[] result = new int[int1.length + int2.length];
+               System.arraycopy(int1, 0, result, 0, int1.length);
+               System.arraycopy(int2, 0, result, int1.length, int2.length);
                
-               String str = "methodA(int,string,float,double,double)";
-               int hash = str.hashCode();
-               System.out.println("Hash value: " + hash);
+               System.out.println("Combined array: " + Arrays.toString(result));
        }
 }