GPOUtils { public static int serializedLength(GPOMutable gpo) { int arrayLength = 0; FieldsDescriptor fd = gpo.getFieldDescriptor(); List<Type> types = fd.getTypesList(); for (int typeIndex = 0; typeIndex < types.size(); typeIndex++) { Type type = types.get(typeIndex); switch (type) { case STRING: { for (String val : gpo.getFieldsString()) { arrayLength += Type.INTEGER.getByteSize(); arrayLength += val.getBytes().length; } break; } case OBJECT: { break; } default: { arrayLength += fd.getTypeToFields().get(type).size() * type.getByteSize(); } } } return arrayLength; } private  GPOUtils(); @SuppressWarnings("unchecked") static Map<String, Type> buildTypeMap(JSONObject jo); static GPOMutable deserialize(FieldsDescriptor fieldsDescriptor, JSONObject dpou); static Map<String, Set<Object>> deserializeToMap(FieldsDescriptor fieldsDescriptor, JSONObject dpou); static void setFieldFromJSON(GPOMutable gpo, Type type, String field, JSONArray jo, int index); static void setFieldFromJSON(GPOMutable gpo, String field, JSONObject jo); static Object getFieldFromJSON(FieldsDescriptor fd, String field, JSONObject jo); static Object getFieldFromJSON(Type type, JSONArray ja, int index); static JSONObject serializeJSONObject(GPOMutable gpo, Fields fields, ResultFormatter resultFormatter); static JSONObject serializeJSONObject(GPOMutable gpo, ResultFormatter resultFormatter); static int serializedLength(GPOMutable gpo); static byte[] serialize(GPOMutable gpo, GPOByteArrayList byteArrayList); static byte[] serialize(GPOMutable gpo, Fields excludedFields); static GPOMutable deserialize(FieldsDescriptor fd, byte[] serializedGPO, MutableInt offset); static GPOMutable deserialize(FieldsDescriptor fieldsDescriptor, Fields excludedFields, byte[] serializedGPO,
      int offset); static String deserializeString(byte[] buffer, MutableInt offset); static void serializeString(String val, byte[] buffer, MutableInt offset); static byte[] serializeString(String val); static long deserializeLong(byte[] buffer, MutableInt offset); static void serializeLong(long val, byte[] buffer, MutableInt offset); static byte[] serializeLong(long val); static long deserializeLong(byte[] buffer); static double deserializeDouble(byte[] buffer, MutableInt offset); static void serializeDouble(double valD, byte[] buffer, MutableInt offset); static byte[] serializeDouble(double valD); static int deserializeInt(byte[] buffer, MutableInt offset); static int deserializeInt(byte[] buffer); static void serializeInt(int val, byte[] buffer, MutableInt offset); static byte[] serializeInt(int val); static float deserializeFloat(byte[] buffer, MutableInt offset); static void serializeFloat(float valf, byte[] buffer, MutableInt offset); static byte[] serializeFloat(float valf); static short deserializeShort(byte[] buffer, MutableInt offset); static void serializeShort(short val, byte[] buffer, MutableInt offset); static byte[] serializeShort(short val); static byte deserializeByte(byte[] buffer, MutableInt offset); static void serializeByte(byte val, byte[] buffer, MutableInt offset); static byte[] serializeByte(byte val); static boolean deserializeBoolean(byte[] buffer, MutableInt offset); static void serializeBoolean(boolean val, byte[] buffer, MutableInt offset); static byte[] serializeBoolean(boolean val); static char deserializeChar(byte[] buffer, MutableInt offset); static void serializeChar(char val, byte[] buffer, MutableInt offset); static byte[] serializeChar(char val); @SuppressWarnings("unchecked") static T[] createGetters(List<String> fields, Map<String, String> valueToExpression,
      Class<?> clazz, Class<?> getterClazz, Class<?> getterMethodClazz); static Getter<Object, String>[] createGettersString(List<String> fields, Map<String, String> valueToExpression,
      Class<?> clazz); static Getter<Object, Object>[] createGettersObject(List<String> fields, Map<String, String> valueToExpression,
      Class<?> clazz); static GPOGetters buildGPOGetters(Map<String, String> fieldToGetter, FieldsDescriptor fieldsDescriptor,
      Class<?> clazz); static void copyPOJOToGPO(GPOMutable mutable, GPOGetters getters, Object object); static void indirectCopy(GPOMutable dest, GPOMutable src, IndexSubset indexSubset); static int hashcode(GPOMutable gpo); static int indirectHashcode(GPOMutable gpo, IndexSubset indexSubset); static boolean equals(GPOMutable dest, GPOMutable src); static boolean subsetEquals(GPOMutable dest, GPOMutable src, IndexSubset indexSubset); static boolean indirectEquals(GPOMutable dest, GPOMutable src, IndexSubset indexSubset); static void zeroFillNumeric(GPOMutable value); static IndexSubset computeSubIndices(FieldsDescriptor child, FieldsDescriptor parent); static Map<String, Object> getDestringedData(FieldsDescriptor fd, Map<String, String> stringMap); static Map<String, Object> convertToMapIntersection(GPOMutable gpo, Fields fields); static boolean insideRange(Type type, int val); static boolean numericTypeIntOrSmaller(Type type);  }
@Test public void testSerializationLength() { final Map<String, Type> fieldToType = Maps.newHashMap(); final String tboolean = "tboolean"; final Boolean tbooleanv = true; final String tchar = "tchar"; final Character tcharv = 'A'; final String tstring = "tstring"; final String tstringv = "hello"; final String tfloat = "tfloat"; final Float tfloatv = 1.0f; final String tdouble = "tdouble"; final Double tdoublev = 2.0; final String tbyte = "tbyte"; final Byte tbytev = 50; final String tshort = "tshort"; final Short tshortv = 1000; final String tinteger = "tinteger"; final Integer tintegerv = 100000; final String tlong = "tlong"; final Long tlongv = 10000000000L; int totalBytes = 1 + 2 + 4 + tstringv.getBytes().length + 4 + 8 + 1 + 2 + 4 + 8; fieldToType.put(tboolean, Type.BOOLEAN); fieldToType.put(tchar, Type.CHAR); fieldToType.put(tstring, Type.STRING); fieldToType.put(tfloat, Type.FLOAT); fieldToType.put(tdouble, Type.DOUBLE); fieldToType.put(tbyte, Type.BYTE); fieldToType.put(tshort, Type.SHORT); fieldToType.put(tinteger, Type.INTEGER); fieldToType.put(tlong, Type.LONG); FieldsDescriptor fd = new FieldsDescriptor(fieldToType); GPOMutable gpo = new GPOMutable(fd); gpo.setFieldGeneric(tboolean, tbooleanv); gpo.setFieldGeneric(tchar, tcharv); gpo.setField(tstring, tstringv); gpo.setFieldGeneric(tfloat, tfloatv); gpo.setFieldGeneric(tdouble, tdoublev); gpo.setFieldGeneric(tbyte, tbytev); gpo.setFieldGeneric(tshort, tshortv); gpo.setFieldGeneric(tinteger, tintegerv); gpo.setFieldGeneric(tlong, tlongv); int serializeLength = GPOUtils.serializedLength(gpo); Assert.assertEquals("The serialized byte length is incorrect.", totalBytes, serializeLength); }
@Test public void testCornerCaseSerializationLegnth() { final Map<String, Type> fieldToType = Maps.newHashMap(); fieldToType.put("a", Type.OBJECT); fieldToType.put("b", Type.OBJECT); GPOMutable gpo = new GPOMutable(new FieldsDescriptor(fieldToType)); int serializeLength = GPOUtils.serializedLength(gpo); Assert.assertEquals(0, serializeLength); }