Mark StreamableArrayList and StreamableHashMap as replaced by their basic Java counterparts. Basic

Map and List types are streamed as fields, which are smaller on the wire.  I'd deprecate them, but
yohoho uses them all over the place.

Handle generating code for StreamableArrayList and StreamableHashMap in actionscript for existing
code.



git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@6215 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Charlie Groves
2010-10-18 21:18:36 +00:00
parent 3a25e8a3b9
commit 20d97bf26a
5 changed files with 45 additions and 15 deletions
@@ -44,6 +44,8 @@ import com.threerings.io.ObjectOutputStream;
import com.threerings.io.Streamable; import com.threerings.io.Streamable;
import com.threerings.util.ActionScript; import com.threerings.util.ActionScript;
import com.threerings.util.StreamableArrayList;
import com.threerings.util.StreamableHashMap;
import com.threerings.presents.data.InvocationMarshaller; import com.threerings.presents.data.InvocationMarshaller;
import com.threerings.presents.dobj.DObject; import com.threerings.presents.dobj.DObject;
@@ -207,9 +209,21 @@ public class GenActionScriptTask extends GenTask
return (Long.TYPE.equals(type) || !type.isPrimitive()) && !String.class.equals(type); return (Long.TYPE.equals(type) || !type.isPrimitive()) && !String.class.equals(type);
} }
/** Returns if the given class is an implementation of Map that doesn't know about Streaming */
protected static boolean isNaiveMap (Class<?> type)
{
return Map.class.isAssignableFrom(type) && !type.equals(StreamableHashMap.class);
}
/** Returns if the given class is an implementation of List that doesn't know about Streaming */
protected static boolean isNaiveList (Class<?> type)
{
return List.class.isAssignableFrom(type) && !type.equals(StreamableArrayList.class);
}
protected static String toActionScriptType (Class<?> type, boolean isField) protected static String toActionScriptType (Class<?> type, boolean isField)
{ {
if (type.isArray() || List.class.isAssignableFrom(type)) { if (type.isArray() || isNaiveList(type)) {
if (Byte.TYPE.equals(type.getComponentType())) { if (Byte.TYPE.equals(type.getComponentType())) {
return "flash.utils.ByteArray"; return "flash.utils.ByteArray";
} }
@@ -217,7 +231,7 @@ public class GenActionScriptTask extends GenTask
return "com.threerings.io.TypedArray"; return "com.threerings.io.TypedArray";
} }
return "Array"; return "Array";
} else if (Map.class.isAssignableFrom(type)) { } else if (isNaiveMap(type)) {
return "com.threerings.util.Map"; return "com.threerings.util.Map";
} else if (Integer.TYPE.equals(type) || } else if (Integer.TYPE.equals(type) ||
Byte.TYPE.equals(type) || Byte.TYPE.equals(type) ||
@@ -273,10 +287,10 @@ public class GenActionScriptTask extends GenTask
} else if (type.equals(Double.TYPE)) { } else if (type.equals(Double.TYPE)) {
return "readDouble()"; return "readDouble()";
} else if (Map.class.isAssignableFrom(type)) { } else if (isNaiveMap(type)) {
return "readField(MapStreamer.INSTANCE)"; return "readField(MapStreamer.INSTANCE)";
} else if (List.class.isAssignableFrom(type)) { } else if (isNaiveList(type)) {
return "readField(ArrayStreamer.INSTANCE)"; return "readField(ArrayStreamer.INSTANCE)";
} else if (type.isArray()) { } else if (type.isArray()) {
@@ -333,10 +347,10 @@ public class GenActionScriptTask extends GenTask
(type.isArray() && type.getComponentType().isPrimitive())) { (type.isArray() && type.getComponentType().isPrimitive())) {
return "writeField(" + name + ")"; return "writeField(" + name + ")";
} else if (List.class.isAssignableFrom(type)) { } else if (isNaiveList(type)) {
return "writeField(" + name + ", ArrayStreamer.INSTANCE)"; return "writeField(" + name + ", ArrayStreamer.INSTANCE)";
} else if (Map.class.isAssignableFrom(type)) { } else if (isNaiveMap(type)) {
return "writeField(" + name + ", MapStreamer.INSTANCE)"; return "writeField(" + name + ", MapStreamer.INSTANCE)";
} else { } else {
@@ -25,17 +25,20 @@ import java.util.ArrayList;
import java.io.IOException; import java.io.IOException;
import com.samskivert.annotation.ReplacedBy;
import com.threerings.io.ObjectInputStream; import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream; import com.threerings.io.ObjectOutputStream;
import com.threerings.io.Streamable; import com.threerings.io.Streamable;
/** /**
* An {@link ArrayList} extension that can be streamed. The contents of * An {@link ArrayList} extension that can be streamed. The contents of the list must also be of
* the list must also be of streamable types. * streamable types.
* *
* @see Streamable * @see Streamable
* @param <E> the type of elements stored in this list. * @param <E> the type of elements stored in this list.
*/ */
@ReplacedBy("java.util.List")
public class StreamableArrayList<E> extends ArrayList<E> public class StreamableArrayList<E> extends ArrayList<E>
implements Streamable implements Streamable
{ {
@@ -26,6 +26,8 @@ import java.util.Map;
import java.io.IOException; import java.io.IOException;
import com.samskivert.annotation.ReplacedBy;
import com.threerings.io.ObjectInputStream; import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream; import com.threerings.io.ObjectOutputStream;
import com.threerings.io.Streamable; import com.threerings.io.Streamable;
@@ -38,6 +40,7 @@ import com.threerings.io.Streamable;
* @param <K> the type of key stored in this map. * @param <K> the type of key stored in this map.
* @param <V> the type of value stored in this map. * @param <V> the type of value stored in this map.
*/ */
@ReplacedBy("java.util.Map")
public class StreamableHashMap<K, V> extends HashMap<K, V> public class StreamableHashMap<K, V> extends HashMap<K, V>
implements Streamable implements Streamable
{ {
@@ -4,7 +4,7 @@
// //
// Narya library - tools for developing networked games // Narya library - tools for developing networked games
// Copyright (C) 2002-2010 Three Rings Design, Inc., All Rights Reserved // Copyright (C) 2002-2010 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/ // http://code.google.com/p/narya/
// //
// This library is free software; you can redistribute it and/or modify it // This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published // under the terms of the GNU Lesser General Public License as published
@@ -30,8 +30,10 @@ import com.threerings.util.Long;
import com.threerings.io.TypedArray; import com.threerings.io.TypedArray;
import flash.utils.ByteArray; import flash.utils.ByteArray;
import com.threerings.io.streamers.ArrayStreamer; import com.threerings.io.streamers.ArrayStreamer;
import com.threerings.util.StreamableArrayList;
import com.threerings.util.Map; import com.threerings.util.Map;
import com.threerings.io.streamers.MapStreamer; import com.threerings.io.streamers.MapStreamer;
import com.threerings.util.StreamableHashMap;
// GENERATED PREAMBLE END // GENERATED PREAMBLE END
import com.threerings.util.Integer; import com.threerings.util.Integer;
@@ -41,8 +43,6 @@ import com.threerings.util.Util;
// GENERATED CLASSDECL START // GENERATED CLASSDECL START
public class ASStreamableSubset extends SimpleStreamableObject public class ASStreamableSubset extends SimpleStreamableObject
{ {
public function ASStreamableSubset ()
{}
// GENERATED CLASSDECL END // GENERATED CLASSDECL END
public static function createWithJavaDefaults () :ASStreamableSubset public static function createWithJavaDefaults () :ASStreamableSubset
@@ -85,12 +85,16 @@ public class ASStreamableSubset extends SimpleStreamableObject
return bool1 === o.bool1 && short2 === o.short2 && int3 === o.int3 && long4.equals(o.long4) && return bool1 === o.bool1 && short2 === o.short2 && int3 === o.int3 && long4.equals(o.long4) &&
float5 === o.float5 && double6 === o.double6 && char7 === o.char7 && float5 === o.float5 && double6 === o.double6 && char7 === o.char7 &&
byte8 === o.byte8 && string1 === o.string1 && nullString1 === o.nullString1 && byte8 === o.byte8 && string1 === o.string1 && nullString1 === o.nullString1 &&
Util.equals(bools, o.bools) && Util.equals(bytes, o.bytes) && Util.equals(bools, o.bools) && Util.equals(bytes, o.bytes) &&
Util.equals(ints, o.ints) && Util.equals(nullBools, o.nullBools) && Util.equals(ints, o.ints) && Util.equals(nullBools, o.nullBools) &&
Util.equals(nullBytes, o.nullBytes) && Util.equals(nullInts, o.nullInts) && Util.equals(nullBytes, o.nullBytes) && Util.equals(nullInts, o.nullInts) &&
Util.equals(strings, o.strings) && Util.equals(nullStrings, o.nullStrings) && Util.equals(strings, o.strings) && Util.equals(nullStrings, o.nullStrings) &&
Util.equals(sal, o.sal) &&
Maps.equals(stringMap, o.stringMap) && Maps.equals(stringIntMap, o.stringIntMap) Maps.equals(stringMap, o.stringMap) && Maps.equals(stringIntMap, o.stringIntMap)
Maps.equals(nullStringMap, o.nullStringMap); Maps.equals(nullStringMap, o.nullStringMap) && Maps.equals(shm, o.shm);
} }
@@ -113,9 +117,11 @@ public class ASStreamableSubset extends SimpleStreamableObject
public var nullInts :TypedArray; public var nullInts :TypedArray;
public var strings :TypedArray; public var strings :TypedArray;
public var nullStrings :TypedArray; public var nullStrings :TypedArray;
public var sal :StreamableArrayList;
public var stringMap :Map; public var stringMap :Map;
public var stringIntMap :Map; public var stringIntMap :Map;
public var nullStringMap :Map; public var nullStringMap :Map;
public var shm :StreamableHashMap;
override public function readObject (ins :ObjectInputStream) :void override public function readObject (ins :ObjectInputStream) :void
{ {
@@ -138,9 +144,11 @@ public class ASStreamableSubset extends SimpleStreamableObject
nullInts = ins.readField(TypedArray.getJavaType(int)); nullInts = ins.readField(TypedArray.getJavaType(int));
strings = ins.readField(ArrayStreamer.INSTANCE); strings = ins.readField(ArrayStreamer.INSTANCE);
nullStrings = ins.readField(ArrayStreamer.INSTANCE); nullStrings = ins.readField(ArrayStreamer.INSTANCE);
sal = ins.readObject(StreamableArrayList);
stringMap = ins.readField(MapStreamer.INSTANCE); stringMap = ins.readField(MapStreamer.INSTANCE);
stringIntMap = ins.readField(MapStreamer.INSTANCE); stringIntMap = ins.readField(MapStreamer.INSTANCE);
nullStringMap = ins.readField(MapStreamer.INSTANCE); nullStringMap = ins.readField(MapStreamer.INSTANCE);
shm = ins.readObject(StreamableHashMap);
} }
override public function writeObject (out :ObjectOutputStream) :void override public function writeObject (out :ObjectOutputStream) :void
@@ -164,9 +172,11 @@ public class ASStreamableSubset extends SimpleStreamableObject
out.writeField(nullInts); out.writeField(nullInts);
out.writeField(strings, ArrayStreamer.INSTANCE); out.writeField(strings, ArrayStreamer.INSTANCE);
out.writeField(nullStrings, ArrayStreamer.INSTANCE); out.writeField(nullStrings, ArrayStreamer.INSTANCE);
out.writeObject(sal);
out.writeField(stringMap, MapStreamer.INSTANCE); out.writeField(stringMap, MapStreamer.INSTANCE);
out.writeField(stringIntMap, MapStreamer.INSTANCE); out.writeField(stringIntMap, MapStreamer.INSTANCE);
out.writeField(nullStringMap, MapStreamer.INSTANCE); out.writeField(nullStringMap, MapStreamer.INSTANCE);
out.writeObject(shm);
} }
// GENERATED STREAMING END // GENERATED STREAMING END
@@ -56,9 +56,9 @@ public class StreamableTest extends TestCase
"01000200000003000000000000000440a0000040180000000000000007080100036f6e6500010000" + "01000200000003000000000000000440a0000040180000000000000007080100036f6e6500010000" +
"0003010001010000000301020301000000030000000100000002000000030000000100000003fffe" + "0003010001010000000301020301000000030000000100000002000000030000000100000003fffe" +
"00106a6176612e6c616e672e537472696e6700036f6e650002000374776f00020005746872656500" + "00106a6176612e6c616e672e537472696e6700036f6e650002000374776f00020005746872656500" +
"01000000030002000374776f0002000132000200036f6e6500020001310002000574687265650002" + "000001000000030002000374776f0002000132000200036f6e650002000131000200057468726565" +
"00013301000000030002000374776ffffd00116a6176612e6c616e672e496e746567657200000002" + "000200013301000000030002000374776ffffd00116a6176612e6c616e672e496e74656765720000" +
"000200036f6e6500030000000100020005746872656500030000000300"); "0002000200036f6e65000300000001000200057468726565000300000003000000");
var input:ObjectInputStream = new ObjectInputStream(javaData); var input:ObjectInputStream = new ObjectInputStream(javaData);
var read :ASStreamableSubset = input.readObject(ASStreamableSubset); var read :ASStreamableSubset = input.readObject(ASStreamableSubset);
assertTrue(sub.equals(read)); assertTrue(sub.equals(read));