Made equals() compare arrays by value.
git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4364 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -7,14 +7,49 @@ import com.threerings.util.StringBuilder;
|
||||
public class Util
|
||||
{
|
||||
/**
|
||||
* A nicer test for equals that works if the objects implement Equalable
|
||||
* A nice utility method for testing equality in a better way.
|
||||
* If the objects are Equalable, then that will be tested. Arrays
|
||||
* and ByteArrays are also compared and are equal if they have
|
||||
* elements that are equals (deeply).
|
||||
*/
|
||||
public static function equals (obj1 :Object, obj2 :Object) :Boolean
|
||||
{
|
||||
// catch various common cases (both primitive or null)
|
||||
return (obj1 === obj2) ||
|
||||
// otherwise, they're only possibly still equal if Equalable
|
||||
((obj1 is Equalable) && (obj1 as Equalable).equals(obj2));
|
||||
if (obj1 === obj2) {
|
||||
return true;
|
||||
|
||||
} else if (obj1 is Equalable) {
|
||||
// if obj1 is Equalable, then that decides it
|
||||
return (obj1 as Equalable).equals(obj2);
|
||||
|
||||
} else if ((obj1 is Array) && (obj2 is Array)) {
|
||||
var ar1 :Array = (obj1 as Array);
|
||||
var ar2 :Array = (obj2 as Array);
|
||||
if (ar1.length != ar2.length) {
|
||||
return false;
|
||||
}
|
||||
for (var jj :int = 0; jj < ar1.length; jj++) {
|
||||
if (!equals(ar1[jj], ar2[jj])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
} else if ((obj1 is ByteArray) && (obj2 is ByteArray)) {
|
||||
var ba1 :ByteArray = (obj1 as ByteArray);
|
||||
var ba2 :ByteArray = (obj2 as ByteArray);
|
||||
if (ba1.length != ba2.length) {
|
||||
return false;
|
||||
}
|
||||
for (var ii :int = 0; ii < ba1.length; ii++) {
|
||||
if (ba1[ii] != ba2[ii]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function cast (obj :Object, clazz :Class) :Object
|
||||
|
||||
Reference in New Issue
Block a user