public static <U> void outputBoxes(java.util.List<Box<U>> boxes) { int counter = 0; for (Box<U> box : boxes) { U boxContents = box.get(); System.out.println("Box # " + counter + " contains [" + boxContents.toString() + "]"); counter++; } }
private static <T> void test(String label, Box<T> m, Ser<T> ser) throws Exception { long start = Time.now(); byte[] bytes = null; for (int i = 0; i < 1000000; i++) { bytes = ser.toBytes(m.get()); ser.fromBytes(bytes); } System.out.println(label + " ms: " + Time.since(start) + ", size: " + bytes.length); }
public static void main(String[] args) { // ONLY place Integer objects into this box! Box integerBox = new Box(); // Imagine this is one part of a large application // modified by one programmer. integerBox.add("10"); // note how the type is now String // ... and this is another, perhaps written // by a different programmer Integer someInteger = (Integer) integerBox.get(); System.out.println(someInteger); }
public static void main(String[] args) throws Exception { Box<Map> bxm = new Box<Map>() { @Override public Map get() { Map m = new HashMap() { { put("_id", 7); put("name", "Joe"); put("age", 27); put( "addresses", new ArrayList() { { add( new HashMap() { { put("_id", "home"); put("city", "San Francisco"); put("state", "CA"); } }); } }); put("created_at", System.currentTimeMillis()); put("updated_at", System.currentTimeMillis()); put("sucks", true); put("someFloat", 3.1414975); } }; return m; } }; Box<BSONObject> bxb = new Box<BSONObject>() { @Override public BSONObject get() { BSONObject m = new BasicBSONObject() { { put("_id", 7); put("name", "Joe"); put("age", 27); put( "addresses", new ArrayList() { { add( new HashMap() { { put("_id", "home"); put("city", "San Francisco"); put("state", "CA"); } }); } }); put("created_at", System.currentTimeMillis()); put("updated_at", System.currentTimeMillis()); put("sucks", true); put("someFloat", 3.141975); } }; return m; } }; Box<JSONObject> bjo = new Box<JSONObject>() { @Override public JSONObject get() { JSONObject m = new JSONObject() { { put("_id", 7); put("name", "Joe"); put("age", 27); put( "addresses", new JSONArray() { { add( new JSONValue() { { put("_id", "home"); put("city", "San Francisco"); put("state", "CA"); } }); } }); put("created_at", System.currentTimeMillis()); put("updated_at", System.currentTimeMillis()); put("sucks", true); put("someFloat", 3.141975); } }; return m; } }; MessagePack pack = new MessagePack(); Value read = pack.read(pack.write(bxm.get())); System.out.println("read = " + read); // System.out.println("read.getClass() = " + read.getClass()); // System.out.println(JSONValue.toJSONString(read)); // // System.out.println(JSONValue.toJSONString(m)); System.out.println("bson: " + new BasicBSONObject(bxm.get())); // test("JSONValue mapToBytesMap", bxm, new SimpleSer()); // test("JSONValue joToBytesToJo", bjo, new JSONObjectSer()); test("msgPack mapToBytesToMap", bxm, new MsgPackSer()); test("BSON bsonToBytesToBson", bxb, new BsonSer()); }