public static void main(String[] args) { PropertyBag bag = new PropertyBag(); bag.putByte(0, (byte) 1); bag.putShort(1, (short) 2); bag.putInt(2, (int) 3); bag.putLong(3, (long) 4); bag.putFloat(4, (float) 1.1); bag.putDouble(5, (double) 2.2); bag.putBytes(6, new byte[] {0, 1, 2, 3}); bag.putString(7, "1234"); bag.putBoolean(8, true); bag.putNull(9); PropertyBag bag2 = new PropertyBag(); bag2.put(0, (byte) 1); bag2.put(1, (short) 2); bag2.put(2, (int) 3); bag2.put(3, (long) 4); bag2.put(4, (float) 1.1); bag2.put(5, (double) 2.2); bag2.put(6, new byte[] {0, 1, 2, 3}); bag2.put(7, "1234"); bag2.put(8, bag); bag2.putBoolean(9, true); bag2.putNull(10); bag2.put(11, bag); PropertyBag test = PropertyBag.valueOfByties(bag2.getByties()); System.out.println(test.toString()); }
/** * 向Bag中存放任意类型,如果类型不可识别,则不存放 * * @param property 属性 * @param value 属性值 */ public void put(int property, Object value) { if (value != null) { Class<?> clazz = value.getClass(); if (clazz == Boolean.TYPE || clazz == Boolean.class) { putBoolean(property, (Boolean) value); } else if (clazz == Byte.TYPE || clazz == Byte.class) { putByte(property, (Byte) value); } else if (clazz == Short.TYPE || clazz == Short.class) { putShort(property, (Short) value); } else if (clazz == Integer.TYPE || clazz == Integer.class) { putInt(property, (Integer) value); } else if (clazz == Long.TYPE || clazz == Long.class) { putLong(property, (Long) value); } else if (clazz == Float.TYPE || clazz == Float.class) { putFloat(property, (Float) value); } else if (clazz == Double.TYPE || clazz == Double.class) { putDouble(property, (Double) value); } else if (clazz == byte[].class) { putBytes(property, (byte[]) value); } else if (clazz == String.class) { putString(property, (String) value); } else if (clazz == PropertyBag.class) { putPropertyBag(property, (PropertyBag) value); } else if (clazz == PropertyList.class) { putPropertyList(property, (PropertyList) value); } } else { putNull(property); } }