Use Joiner instead of StringBuilder, in some places.

Use String concatination in others (it's faster in actionscript than in Java)


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5959 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Ray Greenwell
2009-09-16 21:13:07 +00:00
parent 9f967dd575
commit 055aac9b55
23 changed files with 102 additions and 214 deletions
@@ -21,9 +21,7 @@
package com.threerings.io {
import com.threerings.util.ClassUtil;
import com.threerings.util.StringBuilder;
import com.threerings.util.StringUtil;
import com.threerings.util.Joiner;
/**
* A simple serializable object implements the {@link Streamable}
@@ -49,19 +47,18 @@ public class SimpleStreamableObject implements Streamable
*/
public function toString () :String
{
var buf :StringBuilder = new StringBuilder("[");
buf.append(ClassUtil.tinyClassName(this)).append("(");
toStringBuilder(buf);
return buf.append(")]").toString();
var j :Joiner = Joiner.createFor(this);
toStringJoiner(j);
return j.toString();
}
/**
* Handles the toString-ification of all public members. Derived
* classes can override and include non-public members if desired.
*/
protected function toStringBuilder (buf :StringBuilder): void
protected function toStringJoiner (j :Joiner): void
{
StringUtil.fieldsToString(buf, this);
j.addFields(this);
}
}
}
@@ -25,7 +25,7 @@ import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.util.Boxed;
import com.threerings.util.StringBuilder;
import com.threerings.util.Joiner;
/**
* An attribute changed event is dispatched when a single attribute of a
@@ -107,11 +107,10 @@ public class AttributeChangedEvent extends NamedEvent
}
// documentation inherited
override protected function toStringBuf (buf :StringBuilder) :void
override protected function toStringJoiner (j :Joiner) :void
{
buf.append("CHANGE:");
super.toStringBuf(buf);
buf.append(", value=", _value);
super.toStringJoiner(j);
j.add("value", _value);
}
override public function writeObject (out :ObjectOutputStream) :void
@@ -24,8 +24,8 @@ package com.threerings.presents.dobj {
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.util.Joiner;
import com.threerings.util.StreamableArrayList;
import com.threerings.util.StringBuilder;
/**
* Used to manage and submit groups of events on a collection of distributed objects in a single
@@ -90,15 +90,10 @@ public class CompoundEvent extends DEvent
}
// from DObject
override protected function toStringBuf (buf :StringBuilder) :void
override protected function toStringJoiner (j :Joiner) :void
{
buf.append("COMPOUND:");
super.toStringBuf(buf);
var nn :int = _events.size();
for (var ii :int = 0; ii < nn; ii++) {
buf.append(", ", _events.get(ii));
}
super.toStringJoiner(j);
j.addArgsArray(_events.asArray());
}
// from DObject
+8 -10
View File
@@ -25,7 +25,7 @@ import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.io.Streamable;
import com.threerings.util.StringBuilder;
import com.threerings.util.Joiner;
public /* abstract */ class DEvent
implements Streamable
@@ -96,21 +96,19 @@ public /* abstract */ class DEvent
*/
public function toString () :String
{
var buf :StringBuilder = new StringBuilder();
buf.append("[");
toStringBuf(buf);
buf.append("]");
return buf.toString();
var j :Joiner = Joiner.createFor(this);
toStringJoiner(j);
return j.toString();
}
/**
* This should be overridden by derived classes (which should be sure
* to call <code>super.toString()</code>) to append the derived class
* specific event information to the string buffer.
* to call <code>super.toStringJoiner()</code> first) to append the derived class
* specific event information onto the String representation.
*/
protected function toStringBuf (buf :StringBuilder) :void
protected function toStringJoiner (j :Joiner) :void
{
buf.append("targetOid=", _toid);
j.add("targetOid", _toid);
}
/** The oid of the object that is the target of this event. */
+11 -12
View File
@@ -26,8 +26,8 @@ import flash.errors.IllegalOperationError;
import flash.events.EventDispatcher;
import com.threerings.util.ClassUtil;
import com.threerings.util.Joiner;
import com.threerings.util.Log;
import com.threerings.util.StringBuilder;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
@@ -157,34 +157,33 @@ public class DObject // extends EventDispatcher
*/
public function which () :String
{
var buf :StringBuilder = new StringBuilder();
whichBuf(buf);
return buf.toString();
var j :Joiner = Joiner.createFor(this);
whichJoiner(j);
return j.toString();
}
/**
* Used to briefly describe this distributed object.
*/
protected function whichBuf (buf :StringBuilder) :void
protected function whichJoiner (j :Joiner) :void
{
buf.append(ClassUtil.shortClassName(this), ":", _oid);
j.addArgs(_oid);
}
// documentation inherited
public function toString () :String
{
var buf :StringBuilder = new StringBuilder("[");
toStringBuf(buf);
buf.append("]");
return buf.toString();
var j :Joiner = Joiner.createFor(this);
toStringJoiner(j);
return j.toString();
}
/**
* Generates a string representation of this object.
*/
public function toStringBuf (buf :StringBuilder) :void
public function toStringJoiner (j :Joiner) :void
{
buf.append("oid=", _oid);
j.add("oid", _oid);
}
/**
+1 -5
View File
@@ -26,7 +26,6 @@ import com.threerings.util.Cloneable;
import com.threerings.util.Equalable;
import com.threerings.util.Iterator;
import com.threerings.util.Log;
import com.threerings.util.StringBuilder;
import com.threerings.util.Util;
import com.threerings.io.ObjectInputStream;
@@ -225,10 +224,7 @@ public class DSet
*/
public function toString () :String
{
var buf :StringBuilder = new StringBuilder("(");
buf.append(_entries.toString());
buf.append(")");
return buf.toString();
return "(" + _entries.toString() + ")";
}
// documentation inherited from interface Streamable
@@ -25,7 +25,7 @@ import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.util.Boxed;
import com.threerings.util.StringBuilder;
import com.threerings.util.Joiner;
/**
* An element updated event is dispatched when an element of an array
@@ -132,12 +132,10 @@ public class ElementUpdatedEvent extends NamedEvent
}
// documentation inherited
override protected function toStringBuf (buf :StringBuilder) :void
override protected function toStringJoiner (j :Joiner) :void
{
buf.append("UPDATE:");
super.toStringBuf(buf);
buf.append(", value=", _value);
buf.append(", index=", _index);
super.toStringJoiner(j);
j.add("value", _value, "index", _index);
}
protected var _value :Object;
@@ -24,8 +24,8 @@ package com.threerings.presents.dobj {
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.util.Joiner;
import com.threerings.util.Log;
import com.threerings.util.StringBuilder;
/**
* An entry added event is dispatched when an entry is added to a {@link
@@ -88,11 +88,10 @@ public class EntryAddedEvent extends NamedEvent
}
// documentation inherited
override protected function toStringBuf (buf :StringBuilder) :void
override protected function toStringJoiner (j :Joiner) :void
{
buf.append("ELADD:");
super.toStringBuf(buf);
buf.append(", entry=", _entry);
super.toStringJoiner(j);
j.add("entry", _entry);
}
override public function writeObject (out :ObjectOutputStream) :void
@@ -25,8 +25,8 @@ import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.util.Boxed;
import com.threerings.util.Joiner;
import com.threerings.util.Log;
import com.threerings.util.StringBuilder;
/**
* An entry removed event is dispatched when an entry is removed from a
@@ -107,11 +107,10 @@ public class EntryRemovedEvent extends NamedEvent
}
// documentation inherited
override protected function toStringBuf (buf :StringBuilder) :void
override protected function toStringJoiner (j :Joiner) :void
{
buf.append("ELREM:");
super.toStringBuf(buf);
buf.append(", key=", _key);
super.toStringJoiner(j);
j.add("key", _key);
}
override public function writeObject (out :ObjectOutputStream) :void
@@ -24,8 +24,8 @@ package com.threerings.presents.dobj {
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.util.Joiner;
import com.threerings.util.Log;
import com.threerings.util.StringBuilder;
/**
* An entry updated event is dispatched when an entry of a {@link DSet} is
@@ -89,8 +89,7 @@ public class EntryUpdatedEvent extends NamedEvent
_oldEntry = dset.update(_entry);
if (_oldEntry == null) {
// complain if we didn't update anything
Log.getLog(this).warning("No matching entry to update " +
"[entry=" + this + ", set=" + dset + "].");
Log.getLog(this).warning("No matching entry to update", "entry", this, "set", dset);
return false;
}
}
@@ -106,11 +105,10 @@ public class EntryUpdatedEvent extends NamedEvent
}
// documentation inherited
override protected function toStringBuf (buf :StringBuilder) :void
override protected function toStringJoiner (j :Joiner) :void
{
buf.append("ELUPD:");
super.toStringBuf(buf);
buf.append(", entry=", _entry);
super.toStringJoiner(j);
j.add("entry", _entry);
}
override public function writeObject (out :ObjectOutputStream) :void
@@ -25,7 +25,7 @@ import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.io.TypedArray;
import com.threerings.util.StringBuilder;
import com.threerings.util.Joiner;
/**
* Used to dispatch an invocation notification from the server to a
@@ -103,13 +103,10 @@ public class InvocationNotificationEvent extends DEvent
}
// documentation inherited
override protected function toStringBuf (buf :StringBuilder) :void
override protected function toStringJoiner (j :Joiner) :void
{
buf.append("INOT:");
super.toStringBuf(buf);
buf.append(", rcvId=", _receiverId);
buf.append(", methodId=", _methodId);
buf.append(", args=", _args);
super.toStringJoiner(j);
j.add("rcvId", _receiverId, "methodId", _methodId, "args", _args);
}
// documentation inherited
@@ -24,7 +24,7 @@ package com.threerings.presents.dobj {
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.util.StringBuilder;
import com.threerings.util.Joiner;
/**
* Used to dispatch an invocation request from the client to the server.
@@ -99,13 +99,10 @@ public class InvocationRequestEvent extends DEvent
}
// documentation inherited
override protected function toStringBuf (buf :StringBuilder) :void
override protected function toStringJoiner (j :Joiner) :void
{
buf.append("IREQ:");
super.toStringBuf(buf);
buf.append(", code=", _invCode);
buf.append(", methodId=", _methodId);
buf.append(", args=", _args);
super.toStringJoiner(j);
j.add("code", _invCode, "methodId", _methodId, "args", _args);
}
// documentation inherited from interface Streamable
@@ -24,7 +24,7 @@ package com.threerings.presents.dobj {
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.util.StringBuilder;
import com.threerings.util.Joiner;
/**
* Used to dispatch an invocation response from the server to the client.
@@ -99,13 +99,10 @@ public class InvocationResponseEvent extends DEvent
}
// documentation inherited
override protected function toStringBuf (buf :StringBuilder) :void
override protected function toStringJoiner (j :Joiner) :void
{
buf.append("IRSP:");
super.toStringBuf(buf);
buf.append(", reqid=", _requestId);
buf.append(", methodId=", _methodId);
buf.append(", args=", _args);
super.toStringJoiner(j);
j.add("reqId", _requestId, "methodId", _methodId, "args", _args);
}
// documentation inherited
@@ -26,7 +26,7 @@ import com.threerings.io.ObjectOutputStream;
import com.threerings.io.TypedArray;
import com.threerings.util.Boxed;
import com.threerings.util.StringBuilder;
import com.threerings.util.Joiner;
/**
* A message event is used to dispatch a message to all subscribers of a
@@ -97,11 +97,10 @@ public class MessageEvent extends NamedEvent
}
// documentation inherited
override protected function toStringBuf (buf :StringBuilder) :void
override protected function toStringJoiner (j :Joiner) :void
{
buf.append("MSG:");
super.toStringBuf(buf);
buf.append(", args=", _args);
super.toStringJoiner(j);
j.add("args", _args);
}
// documentation inherited
@@ -21,7 +21,7 @@
package com.threerings.presents.dobj {
import com.threerings.util.StringBuilder;
import com.threerings.util.Joiner;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
@@ -53,10 +53,10 @@ public /* abstract */ class NamedEvent extends DEvent
return _name;
}
override protected function toStringBuf (buf :StringBuilder) :void
override protected function toStringJoiner (j :Joiner) :void
{
super.toStringBuf(buf);
buf.append(", name=", _name);
super.toStringJoiner(j);
j.add("name", _name);
}
override public function writeObject (out :ObjectOutputStream) :void
@@ -24,7 +24,7 @@ package com.threerings.presents.dobj {
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.util.StringBuilder;
import com.threerings.util.Joiner;
/**
* An object added event is dispatched when an object is added to an
@@ -97,11 +97,10 @@ public class ObjectAddedEvent extends NamedEvent
}
// documentation inherited
override protected function toStringBuf (buf :StringBuilder) :void
override protected function toStringJoiner (j :Joiner) :void
{
buf.append("OBJADD:");
super.toStringBuf(buf);
buf.append(", oid=", _oid);
super.toStringJoiner(j);
j.add("oid", _oid);
}
protected var _oid :int;
@@ -21,8 +21,6 @@
package com.threerings.presents.dobj {
import com.threerings.util.StringBuilder;
/**
* An object destroyed event is dispatched when an object has been removed
* from the distributed object system. It can also be constructed to
@@ -59,12 +57,5 @@ public class ObjectDestroyedEvent extends DEvent
listener.objectDestroyed(this);
}
}
// documentation inherited
override protected function toStringBuf (buf :StringBuilder) :void
{
buf.append("DESTROY:");
super.toStringBuf(buf);
}
}
}
@@ -21,7 +21,7 @@
package com.threerings.presents.dobj {
import com.threerings.util.StringBuilder;
import com.threerings.util.Joiner;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
@@ -99,11 +99,10 @@ public class ObjectRemovedEvent extends NamedEvent
}
// documentation inherited
override protected function toStringBuf (buf :StringBuilder) :void
override protected function toStringJoiner (j :Joiner) :void
{
buf.append("OBJREM:");
super.toStringBuf(buf);
buf.append(", oid=", _oid);
super.toStringJoiner(j);
j.add("oid", _oid);
}
protected var _oid :int;
@@ -29,8 +29,6 @@ import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.io.TypedArray;
import com.threerings.util.StringBuilder;
/**
* An oid list is used to store lists of object ids. The list will not
* allow duplicate ids. This class is not synchronized, with the
@@ -127,11 +125,7 @@ public class OidList
public function toString () :String
{
var buf :StringBuilder = new StringBuilder();
buf.append("{");
buf.append(_oids.toString());
buf.append("}");
return buf.toString();
return "{" + _oids.toString() + "}";
}
private var _oids :TypedArray;
@@ -23,67 +23,19 @@ package com.threerings.presents.net {
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.io.Streamable;
import com.threerings.io.SimpleStreamableObject;
public /* abstract */ class Credentials
implements Streamable
public /* abstract */ class Credentials extends SimpleStreamableObject
{
public function Credentials ()
{
}
// from interface Streamable
public function writeObject (out :ObjectOutputStream) :void
//throws IOError
{
// nada
}
// from interface Streamable
public function readObject (ins :ObjectInputStream) :void
override public function readObject (ins :ObjectInputStream) :void
//throws IOError
{
throw new Error(); // we never read Creds on the client
}
/*
// main
public function toString (joiner :Joiner = null) :String
{
if (joiner == null) {
return toString(new Joiner("ClassName"));
}
return joiner.add("baz", bit, "count", 33).toString();
}
// subclass
override public function toString (joiner :Joiner = null) :String
{
if (joiner != null) {
joiner.add("foo", bar);
}
return super.toString(joiner);
}
// -----
public function toString () :String
{
return join(new Joiner(this)).toString();
}
protected function join (joiner :Joiner) :Joiner
{
return joiner.add(
"baz", bit, "count", 33);
}
override protected join (joiner :Joiner) :Joiner
{
return super.join(joiner).add(
"foo", bar);
}
*/
}
}
@@ -26,6 +26,8 @@ import com.adobe.crypto.MD5;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.util.Joiner;
/**
* Extends the basic credentials to provide bureau-specific fields.
*/
@@ -60,9 +62,10 @@ public class ServiceCreds extends Credentials
}
// from Object
public function toString () :String
override protected function toStringJoiner (j :Joiner) :void
{
return "[id=" + clientId + ", token=" + _authToken + "]";
super.toStringJoiner(j);
j.add("token", _authToken);
}
/**
@@ -21,8 +21,8 @@
package com.threerings.presents.net {
import com.threerings.util.Joiner;
import com.threerings.util.Name;
import com.threerings.util.StringBuilder;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
@@ -55,26 +55,10 @@ public class UsernamePasswordCreds extends Credentials
out.writeField(_password);
}
// from Credentials
override public function readObject (ins :ObjectInputStream) :void
override protected function toStringJoiner (j :Joiner) :void
{
_username = Name(ins.readObject());
_password = (ins.readField(String) as String);
}
// from Object
public function toString () :String
{
var buf :StringBuilder = new StringBuilder("[");
toStringBuf(buf);
buf.append("]");
return buf.toString();
}
protected function toStringBuf (buf :StringBuilder) :void
{
buf.append("username=", _username);
buf.append(", password=", _password);
j.add("username", _username, "password", _password);
super.toStringJoiner(j);
}
protected var _username :Name;
+10 -12
View File
@@ -302,25 +302,24 @@ public class MessageBundle
{
args = Util.unfuckVarargs(args);
var buf :StringBuilder = new StringBuilder();
buf.append(key, "|");
var s :String = key + "|";
for (var ii :int = 0; ii < args.length; ii++) {
if (ii > 0) {
buf.append("|");
s += "|";
}
var arg :String = String(args[ii]);
for (var p :int = 0; p < arg.length; p++) {
var ch :String = arg.charAt(p);
if (ch == "|") {
buf.append("\\!");
s += "\\!";
} else if (ch == "\\") {
buf.append("\\\\");
s += "\\\\";
} else {
buf.append(ch);
s += ch;
}
}
}
return buf.toString();
return s;
}
/**
@@ -404,19 +403,18 @@ public class MessageBundle
return val;
}
var buf :StringBuilder = new StringBuilder();
var s :String = "";
for (var ii :int = 0; ii < val.length; ii++) {
var ch :String = val.charAt(ii);
if (ch != "\\" || ii == val.length-1) {
buf.append(ch);
s += ch;
} else {
// look at the next character
ch = val.charAt(++ii);
buf.append((ch == "!") ? "|" : ch);
s += (ch == "!") ? "|" : ch;
}
}
return buf.toString();
return s;
}
/** The message manager via whom we'll resolve fully qualified