So, these classes like Integer and langBoolean exist solely for

streaming to our java server, so let's just make them streamable directly.
Does away with the need for custom Streamers.
Also made them Hashable, in case we read a StreamableHashMap containing
Integers as keys, for example.
Less code is better code.
(Why was BooleanStreamer added the other day? langBoolean was already Streamable
 and was already in Translations)


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5960 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Ray Greenwell
2009-09-17 21:42:25 +00:00
parent 055aac9b55
commit a24b0d2d03
14 changed files with 147 additions and 368 deletions
+1 -7
View File
@@ -29,14 +29,9 @@ import com.threerings.util.ClassUtil;
import com.threerings.util.Enum;
import com.threerings.io.streamers.ArrayStreamer;
import com.threerings.io.streamers.BooleanStreamer;
import com.threerings.io.streamers.ByteStreamer;
import com.threerings.io.streamers.ByteArrayStreamer;
import com.threerings.io.streamers.ByteEnumStreamer;
import com.threerings.io.streamers.EnumStreamer;
import com.threerings.io.streamers.FloatStreamer;
import com.threerings.io.streamers.IntegerStreamer;
import com.threerings.io.streamers.LongStreamer;
import com.threerings.io.streamers.NumberStreamer;
import com.threerings.io.streamers.StringStreamer;
@@ -147,8 +142,7 @@ public class Streamer
}
_byJName = new Dictionary();
for each (var c :Class in
[ StringStreamer, NumberStreamer, ByteStreamer, IntegerStreamer, LongStreamer,
FloatStreamer, ArrayStreamer, ByteArrayStreamer, BooleanStreamer ]) {
[ StringStreamer, NumberStreamer, ArrayStreamer, ByteArrayStreamer ]) {
registerStreamer(Streamer(new c()));
}
}
+3 -2
View File
@@ -56,12 +56,13 @@ public class Translations
// initialize some standard classes
addTranslation("Object", "java.lang.Object");
addTranslation("String", "java.lang.String");
addTranslation("Array", "[Ljava.lang.Object;");
addTranslation("flash.utils.ByteArray", "[B");
addTranslation("com.threerings.util.langBoolean", "java.lang.Boolean");
addTranslation("com.threerings.util.Byte", "java.lang.Byte");
addTranslation("com.threerings.util.Short", "java.lang.Short");
addTranslation("com.threerings.util.Integer", "java.lang.Integer");
addTranslation("com.threerings.util.Long", "java.lang.Long");
addTranslation("com.threerings.util.Float", "java.lang.Float");
addTranslation("Array", "[Ljava.lang.Object;");
addTranslation("flash.utils.ByteArray", "[B");
}
}
@@ -1,55 +0,0 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2009 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// 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
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.io.streamers {
import com.threerings.util.langBoolean;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.io.Streamer;
/**
* A Streamer for Integer objects.
*/
public class BooleanStreamer extends Streamer
{
public function BooleanStreamer ()
{
super(langBoolean, "java.lang.Boolean");
}
override public function createObject (ins :ObjectInputStream) :Object
{
return new langBoolean(ins.readBoolean());
}
override public function writeObject (obj :Object, out :ObjectOutputStream) :void
{
out.writeBoolean((obj as langBoolean).value);
}
override public function readObject (obj :Object, ins :ObjectInputStream) :void
{
// unneeded, done in createObject
}
}
}
@@ -1,56 +0,0 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2009 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// 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
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.io.streamers {
import com.threerings.util.Byte;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.io.Streamer;
/**
* A Streamer for Byte objects.
*/
public class ByteStreamer extends Streamer
{
public function ByteStreamer ()
{
super(Byte, "java.lang.Byte");
}
override public function createObject (ins :ObjectInputStream) :Object
{
return new Byte(ins.readByte());
}
override public function writeObject (obj :Object, out :ObjectOutputStream) :void
{
var byte :Byte = (obj as Byte);
out.writeByte(byte.value);
}
override public function readObject (obj :Object, ins :ObjectInputStream) :void
{
// unneeded, done in createObject
}
}
}
@@ -1,56 +0,0 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2009 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// 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
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.io.streamers {
import com.threerings.util.Float;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.io.Streamer;
/**
* A Streamer for Float objects.
*/
public class FloatStreamer extends Streamer
{
public function FloatStreamer ()
{
super(Float, "java.lang.Float");
}
override public function createObject (ins :ObjectInputStream) :Object
{
return new Float(ins.readFloat());
}
override public function writeObject (obj :Object, out :ObjectOutputStream) :void
{
var float :Float = (obj as Float);
out.writeFloat(float.value);
}
override public function readObject (obj :Object, ins :ObjectInputStream) :void
{
// unneeded, done in createObject
}
}
}
@@ -1,56 +0,0 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2009 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// 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
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.io.streamers {
import com.threerings.util.Integer;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.io.Streamer;
/**
* A Streamer for Integer objects.
*/
public class IntegerStreamer extends Streamer
{
public function IntegerStreamer ()
{
super(Integer, "java.lang.Integer");
}
override public function createObject (ins :ObjectInputStream) :Object
{
return new Integer(ins.readInt());
}
override public function writeObject (obj :Object, out :ObjectOutputStream) :void
{
var inty :Integer = (obj as Integer);
out.writeInt(inty.value);
}
override public function readObject (obj :Object, ins :ObjectInputStream) :void
{
// unneeded, done in createObject
}
}
}
@@ -1,52 +0,0 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2009 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// 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
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.io.streamers {
import com.threerings.util.Long;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.io.Streamer;
/**
* A Streamer for Long objects.
*/
public class LongStreamer extends Streamer
{
public function LongStreamer ()
{
super(Long, "java.lang.Long");
}
override public function writeObject (obj :Object, out :ObjectOutputStream) :void
{
var longy :Long = (obj as Long);
out.writeBytes(longy.bytes, 0, longy.bytes.length);
}
override public function readObject (obj :Object, ins :ObjectInputStream) :void
{
var longy :Long = (obj as Long);
ins.readBytes(longy.bytes, 0, longy.bytes.length);
}
}
}
@@ -1,56 +0,0 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2009 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// 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
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.io.streamers {
import com.threerings.util.Short;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.io.Streamer;
/**
* A Streamer for Short objects.
*/
public class ShortStreamer extends Streamer
{
public function ShortStreamer ()
{
super(Short, "java.lang.Short");
}
override public function createObject (ins :ObjectInputStream) :Object
{
return new Short(ins.readShort());
}
override public function writeObject (obj :Object, out :ObjectOutputStream) :void
{
var short :Short = (obj as Short);
out.writeShort(short.value);
}
override public function readObject (obj :Object, ins :ObjectInputStream) :void
{
// unneeded, done in createObject
}
}
}
+24 -2
View File
@@ -21,11 +21,15 @@
package com.threerings.util {
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.io.Streamable;
/**
* Equivalent to java.lang.Byte.
*/
public class Byte
implements Equalable, Boxed
implements Hashable, Boxed, Streamable
{
public static function valueOf (val :int) :Byte
{
@@ -43,7 +47,7 @@ public class Byte
/**
* Constructor.
*/
public function Byte (byteValue :int)
public function Byte (byteValue :int = 0)
{
_value = byteValue;
}
@@ -54,6 +58,12 @@ public class Byte
return (other is Byte) && (_value === (other as Byte).value);
}
// from Hashable
public function hashCode () :int
{
return _value;
}
// from Boxed
public function unbox () :Object
{
@@ -66,6 +76,18 @@ public class Byte
return "Byte(" + _value + ")";
}
// from Streamable
public function readObject (ins :ObjectInputStream) :void
{
_value = ins.readByte();
}
// from Streamable
public function writeObject (out :ObjectOutputStream) :void
{
out.writeByte(_value);
}
protected var _value :int;
}
}
+25 -3
View File
@@ -21,11 +21,15 @@
package com.threerings.util {
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.io.Streamable;
/**
* Equivalent to java.lang.Float.
*/
public class Float
implements Equalable, Boxed
public final class Float
implements Hashable, Boxed, Streamable
{
public static function valueOf (val :Number) :Float
{
@@ -43,7 +47,7 @@ public class Float
/**
* Constructor.
*/
public function Float (floatValue :Number)
public function Float (floatValue :Number = 0)
{
_value = floatValue;
}
@@ -54,12 +58,30 @@ public class Float
return (other is Float) && (_value === (other as Float).value);
}
// from Hashable
public function hashCode () :int
{
return int(_value);
}
// from Boxed
public function unbox () :Object
{
return _value;
}
// from Streamable
public function readObject (ins :ObjectInputStream) :void
{
_value = ins.readFloat();
}
// from Streamable
public function writeObject (out :ObjectOutputStream) :void
{
out.writeFloat(_value);
}
protected var _value :Number;
}
}
+25 -4
View File
@@ -21,6 +21,10 @@
package com.threerings.util {
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.io.Streamable;
/**
* Equivalent to java.lang.Integer.
*/
@@ -29,8 +33,8 @@ package com.threerings.util {
// autotranslate between int <--> java.lang.Integer and
// Number <--> java.lang.Double. However, a Number object that refers
// to an integer value is actually an int. Yes, it's totally fucked.
public class Integer
implements Equalable, Boxed
public final class Integer
implements Hashable, Boxed, Streamable
{
public static function valueOf (val :int) :Integer
{
@@ -48,7 +52,7 @@ public class Integer
/**
* Constructor.
*/
public function Integer (intValue :int)
public function Integer (intValue :int = 0)
{
_value = intValue;
}
@@ -59,6 +63,12 @@ public class Integer
return (other is Integer) && (_value === (other as Integer).value);
}
// from Hashable
public function hashCode () :int
{
return _value;
}
// from Boxed
public function unbox () :Object
{
@@ -79,7 +89,18 @@ public class Integer
return (val1 > val2) ? 1 : (val1 == val2 ? 0 : -1);
}
protected var _value :int;
// from Streamable
public function readObject (ins :ObjectInputStream) :void
{
_value = ins.readInt();
}
// from Streamable
public function writeObject (out :ObjectOutputStream) :void
{
out.writeInt(_value);
}
protected var _value :int;
}
}
+36 -14
View File
@@ -24,20 +24,22 @@ package com.threerings.util {
import flash.utils.ByteArray;
import flash.utils.Endian;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.io.Streamable;
/**
* Equivalent to java.lang.Long.
*/
public class Long
implements Equalable
public final class Long
implements Hashable, Streamable
{
public var bytes :ByteArray;
public function Long ()
{
bytes = new ByteArray();
bytes.endian = Endian.BIG_ENDIAN;
bytes.writeDouble(0);
bytes.position = 0;
_bytes = new ByteArray();
_bytes.endian = Endian.BIG_ENDIAN;
_bytes.writeDouble(0);
_bytes.position = 0;
}
/**
@@ -57,7 +59,7 @@ public class Long
var n :Number = Math.round(value);
var l :Long = new Long();
for (var ii :int = 7; ii >= 0; ii--) {
l.bytes[ii] = (n % 256);
l._bytes[ii] = (n % 256);
n = Math.floor(n / 256);
}
return l;
@@ -70,10 +72,10 @@ public class Long
public function toNumber () :Number
{
var n :Number = 0;
var positive :Boolean = ((bytes[0] & 0x80) == 0x00);
var positive :Boolean = ((_bytes[0] & 0x80) == 0x00);
for (var ii :int = 0; ii < 8; ii++) {
// if the number is negative, complement each byte as it comes in, and fix up later
n = n * 256 + (positive ? bytes[ii] : (255 - bytes[ii]));
n = n * 256 + (positive ? _bytes[ii] : (255 - _bytes[ii]));
}
// now fix up negative numbers
if (! positive) {
@@ -88,8 +90,8 @@ public class Long
for (var ii :int = 0; ii < 8; ii++) {
// my kingdom for a hex formatting routine!
if (ii != 0) { s += " "; }
if (bytes[ii] < 16) { s += "0"; }
s += int(bytes[ii]).toString(16);
if (_bytes[ii] < 16) { s += "0"; }
s += int(_bytes[ii]).toString(16);
}
s += "]";
return s;
@@ -98,7 +100,27 @@ public class Long
// from Equalable
public function equals (other :Object) :Boolean
{
return (other is Long) && Util.equals(this.bytes, Long(other).bytes);
return (other is Long) && Util.equals(_bytes, Long(other)._bytes);
}
// from Hashable
public function hashCode () :int
{
return _bytes[0];
}
// from Streamable
public function readObject (ins :ObjectInputStream) :void
{
ins.readBytes(_bytes, 0, 8);
}
// from Streamable
public function writeObject (out :ObjectOutputStream) :void
{
out.writeBytes(_bytes, 0, 8);
}
protected var _bytes :ByteArray;
}
}
+25 -3
View File
@@ -21,11 +21,15 @@
package com.threerings.util {
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.io.Streamable;
/**
* Equivalent to java.lang.Short.
*/
public class Short
implements Equalable, Boxed
public final class Short
implements Hashable, Boxed, Streamable
{
/** The minimum possible short value. */
public static const MIN_VALUE :int = -Math.pow(2, 15);
@@ -46,7 +50,7 @@ public class Short
return _value;
}
public function Short (shortValue :int)
public function Short (shortValue :int = 0)
{
_value = shortValue;
}
@@ -57,12 +61,30 @@ public class Short
return (other is Short) && (_value === (other as Short).value);
}
// from Hashable
public function hashCode () :int
{
return _value;
}
// from Boxed
public function unbox () :Object
{
return _value;
}
// from Streamable
public function readObject (ins :ObjectInputStream) :void
{
_value = ins.readShort();
}
// from Streamable
public function writeObject (out :ObjectOutputStream) :void
{
out.writeShort(_value);
}
protected var _value :int;
}
}
+8 -2
View File
@@ -28,8 +28,8 @@ import com.threerings.io.Streamable;
/**
* Equivalent to java.lang.Boolean.
*/
public class langBoolean
implements Equalable, Streamable, Boxed
public final class langBoolean
implements Hashable, Boxed, Streamable
{
public static function valueOf (val :Boolean) :langBoolean
{
@@ -59,6 +59,12 @@ public class langBoolean
(_value === (other as langBoolean).value);
}
// from Hashable
public function hashCode () :int
{
return _value ? 1 : 0;
}
// from Streamable
public function writeObject (out :ObjectOutputStream) :void
{