All the latest and ... well, the latest anyway.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3950 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Ray Greenwell
2006-03-15 03:28:02 +00:00
parent 4fe0d6e484
commit ed1be4a0f2
18 changed files with 551 additions and 350 deletions
+50
View File
@@ -0,0 +1,50 @@
package com.threerings.util {
/**
* Contains methods that should be in Array, but aren't.
*/
public class ArrayUtil
{
/**
* Remove the first instance of the specified element from the array.
*/
public static function removeFirst (arr :Array, element :Object) :void
{
removeImpl(arr, element, true);
}
/**
* Remove the last instance of the specified element from the array.
*/
public static function removeLast (arr :Array, element :Object) :void
{
arr.reverse();
removeFirst(arr, element);
arr.reverse();
}
/**
* Removes all instances of the specified element from the array.
*/
public static function removeAll (arr :Array, element :Object) :void
{
removeImpl(arr, element, false);
}
/**
* Implementation of remove methods.
*/
private static function removeImpl (
arr :Array, element :Object, firstOnly :Boolean) :void
{
for (var ii :int = 0; ii < arr.length; ii++) {
if (arr[ii] === element) {
arr.splice(ii--, 1);
if (firstOnly) {
return;
}
}
}
}
}
}
+3 -10
View File
@@ -19,7 +19,7 @@
// 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.util;
package com.threerings.util {
/**
* A message bundle provides an easy mechanism by which to obtain
@@ -54,14 +54,6 @@ public class MessageBundle
return getResourceString(key, false) != null;
}
/**
* Get a String from the resource bundle, or null if there was an error.
*/
protected function getResourceString (key :String) :String
{
return getResourceString(key, true);
}
/**
* Get a String from the resource bundle, or null if there was an
* error.
@@ -71,7 +63,7 @@ public class MessageBundle
* if the resource didn't exist.
*/
protected function getResourceString (
key :String, reportMissing :Boolean) :String
key :String, reportMissing :Boolean = true) :String
{
// TODO!!!
// try {
@@ -391,3 +383,4 @@ public class MessageBundle
protected static const QUAL_PREFIX :String = "%";
protected static const QUAL_SEP :String = ":";
}
}
+4 -4
View File
@@ -127,7 +127,7 @@ public class MessageManager
// } else {
rbundle = ResourceBundle.getBundle(fqpath, _locale);
// }
} catch (MissingResourceException mre) {
} catch (mre :Error) {
Log.warning("Unable to resolve resource bundle " +
"[path=" + fqpath + ", locale=" + _locale + "].");
}
@@ -136,7 +136,7 @@ public class MessageManager
// interpret that as a derivation of MessageBundle to instantiate
// for handling that class
if (rbundle != null) {
String mbclass = null;
var mbclass :String = null;
try {
mbclass = rbundle.getString(MBUNDLE_CLASS_KEY);
if (!StringUtil.isBlank(mbclass)) {
@@ -144,10 +144,10 @@ public class MessageManager
Class.forName(mbclass).newInstance();
}
} catch (MissingResourceException mre) {
} catch (mre :Error) {
// nothing to worry about
} catch (Throwable t) {
} catch (t :Error) {
Log.warning("Failure instantiating custom message bundle " +
"[mbclass=" + mbclass + ", error=" + t + "].");
}
+1 -13
View File
@@ -12,19 +12,7 @@ public class StringUtil
*/
public static function trim (str :String) :String
{
while (str.search(/\s/) == 0) {
str = str.substring(1);
}
do {
var endstr :String = str.substring(str.length - 1);
if (endstr.search(/\s/) != -1) {
str = str.substring(0, str.length - 1);
} else {
break;
}
} while (true);
return str;
return mx.utils.StringUtil.trim(str);
}
}
}
+140
View File
@@ -0,0 +1,140 @@
//
// $Id: TimeUtil.java 3372 2005-03-01 01:16:01Z ray $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 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.util {
/**
* Utility for times.
*/
public class TimeUtil
{
/** Time unit constant. */
public static const MILLISECOND :int = 0;
/** Time unit constant. */
public static const SECOND :int = 1;
/** Time unit constant. */
public static const MINUTE :int = 2;
/** Time unit constant. */
public static const HOUR :int = 3;
/** Time unit constant. */
public static const DAY :int = 4;
// TODO: Weeks?, months?
protected static const MAX_UNIT :int = DAY;
/**
* Get a translatable string specifying the magnitude of the specified
* duration. Results will be between "1 second" and "X hours", with
* all times rounded to the nearest unit. "0 units" will never be
* displayed, the minimum is 1.
*/
public static function getTimeOrderString (
duration :long, minUnit :int, maxUnit :int = -1) :String
{
// enforce sanity
if (maxUnit == -1) {
maxUnit = MAX_UNIT;
}
minUnit = Math.min(minUnit, maxUnit);
maxUnit = Math.min(maxUnit, MAX_UNIT);
for (var uu :int = MILLISECOND; uu <= MAX_UNIT; uu++) {
var quantity :int = getQuantityPerUnit(uu);
if ((minUnit <= uu) && (duration < quantity || maxUnit == uu)) {
duration = Math.max(1, duration);
return MessageBundle.tcompose(getTransKey(uu),
String(duration));
}
duration = Math.round(duration / quantity);
}
// will not happen, because eventually uu will be MAX_UNIT
return null;
}
/**
* Get a translatable string specifying the duration, down to the
* minimum granularity.
*/
public static function getTimeString (duration :long, minUnit :int) :String
{
// sanity
minUnit = Math.min(minUnit, MAX_UNIT);
duration = Math.abs(duration);
var list :Array = new Array();
var parts :int = 0; // how many parts are in the translation string?
for (var uu :int = MILLISECOND; uu <= MAX_UNIT; uu++) {
var quantity :int = getQuantityPerUnit(uu);
if (minUnit <= uu) {
list.push(MessageBundle.tcompose(getTransKey(uu),
String(duration % quantity)));
parts++;
}
duration /= quantity;
if (duration <= 0 && parts > 0) {
break;
}
}
if (parts == 1) {
return (list[0] as String);
} else {
return MessageBundle.compose("m.times_" + parts, list);
}
}
/**
* Internal method to get the quantity for the specified unit.
* (Not very OO)
*/
protected static function getQuantityPerUnit (unit :int) :int
{
switch (unit) {
case MILLISECOND: return 1000;
case SECOND: case MINUTE: return 60;
case HOUR: return 24;
case DAY: return Integer.MAX_VALUE;
default: return -1;
}
}
/**
* Internal method to get the translation key for the specified unit.
* (Not very OO)
*/
protected static function getTransKey (unit :int) :String
{
switch (unit) {
case MILLISECOND: return "m.millisecond";
case SECOND: return "m.second";
case MINUTE: return "m.minute";
case HOUR: return "m.hour";
case DAY: return "m.day";
default: return null;
}
}
}
}