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:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user