LogBuilder.arrayStr() -> ArrayUtil.toString(), made public.
If Java were invented today, you can be damn sure the array classes would have reasonable hashCode(), equals(), and toString() implementations, instead of falling back to Object's. git-svn-id: https://samskivert.googlecode.com/svn/trunk@2969 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -22,6 +22,7 @@ package com.samskivert.util;
|
|||||||
|
|
||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
@@ -906,6 +907,43 @@ public class ArrayUtil
|
|||||||
return dest;
|
return dest;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the String representation of the specified Object, which must be an array.
|
||||||
|
*
|
||||||
|
* @throws IllegalArgumentException if array is not actually an array.
|
||||||
|
*/
|
||||||
|
public static String toString (Object array)
|
||||||
|
{
|
||||||
|
if (array instanceof Object[]) {
|
||||||
|
return Arrays.deepToString((Object[])array); // go deep, baby
|
||||||
|
|
||||||
|
} else if (array instanceof int[]) {
|
||||||
|
return Arrays.toString((int[])array);
|
||||||
|
|
||||||
|
} else if (array instanceof byte[]) {
|
||||||
|
return Arrays.toString((byte[])array);
|
||||||
|
|
||||||
|
} else if (array instanceof char[]) {
|
||||||
|
return Arrays.toString((char[])array);
|
||||||
|
|
||||||
|
} else if (array instanceof short[]) {
|
||||||
|
return Arrays.toString((short[])array);
|
||||||
|
|
||||||
|
} else if (array instanceof long[]) {
|
||||||
|
return Arrays.toString((long[])array);
|
||||||
|
|
||||||
|
} else if (array instanceof float[]) {
|
||||||
|
return Arrays.toString((float[])array);
|
||||||
|
|
||||||
|
} else if (array instanceof double[]) {
|
||||||
|
return Arrays.toString((double[])array);
|
||||||
|
|
||||||
|
} else if (array instanceof boolean[]) {
|
||||||
|
return Arrays.toString((boolean[])array);
|
||||||
|
}
|
||||||
|
throw new IllegalArgumentException("Not an array: " + array);
|
||||||
|
}
|
||||||
|
|
||||||
/** The default random object used when shuffling an array. */
|
/** The default random object used when shuffling an array. */
|
||||||
protected static final Random _rnd = new Random();
|
protected static final Random _rnd = new Random();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,8 +20,6 @@
|
|||||||
|
|
||||||
package com.samskivert.util;
|
package com.samskivert.util;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Formats a message and an array of alternating key value pairs like so:
|
* Formats a message and an array of alternating key value pairs like so:
|
||||||
*
|
*
|
||||||
@@ -66,7 +64,8 @@ public class LogBuilder
|
|||||||
_log.append(args[ii]).append('=');
|
_log.append(args[ii]).append('=');
|
||||||
try {
|
try {
|
||||||
Object arg = args[ii + 1];
|
Object arg = args[ii + 1];
|
||||||
_log.append(((arg == null) || !arg.getClass().isArray()) ? arg : arrayStr(arg));
|
_log.append(((arg == null) || !arg.getClass().isArray())
|
||||||
|
? arg : ArrayUtil.toString(arg));
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
_log.append("<toString() failure: ").append(t).append('>');
|
_log.append("<toString() failure: ").append(t).append('>');
|
||||||
}
|
}
|
||||||
@@ -91,41 +90,6 @@ public class LogBuilder
|
|||||||
return _log.toString();
|
return _log.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the String representation of the specified Object, which must be an array.
|
|
||||||
*/
|
|
||||||
protected static String arrayStr (Object arg)
|
|
||||||
{
|
|
||||||
if (arg instanceof Object[]) {
|
|
||||||
return Arrays.deepToString((Object[])arg); // go deep, baby
|
|
||||||
|
|
||||||
} else if (arg instanceof int[]) {
|
|
||||||
return Arrays.toString((int[])arg);
|
|
||||||
|
|
||||||
} else if (arg instanceof byte[]) {
|
|
||||||
return Arrays.toString((byte[])arg);
|
|
||||||
|
|
||||||
} else if (arg instanceof char[]) {
|
|
||||||
return Arrays.toString((char[])arg);
|
|
||||||
|
|
||||||
} else if (arg instanceof short[]) {
|
|
||||||
return Arrays.toString((short[])arg);
|
|
||||||
|
|
||||||
} else if (arg instanceof long[]) {
|
|
||||||
return Arrays.toString((long[])arg);
|
|
||||||
|
|
||||||
} else if (arg instanceof float[]) {
|
|
||||||
return Arrays.toString((float[])arg);
|
|
||||||
|
|
||||||
} else if (arg instanceof double[]) {
|
|
||||||
return Arrays.toString((double[])arg);
|
|
||||||
|
|
||||||
} else if (arg instanceof boolean[]) {
|
|
||||||
return Arrays.toString((boolean[])arg);
|
|
||||||
}
|
|
||||||
throw new IllegalArgumentException("Not an array: " + arg);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected boolean _hasArgs;
|
protected boolean _hasArgs;
|
||||||
protected StringBuilder _log;
|
protected StringBuilder _log;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -160,7 +160,7 @@ public abstract class Logger
|
|||||||
Object arg = args[ii + 1];
|
Object arg = args[ii + 1];
|
||||||
buf.append(((arg == null) || !arg.getClass().isArray())
|
buf.append(((arg == null) || !arg.getClass().isArray())
|
||||||
? arg
|
? arg
|
||||||
: LogBuilder.arrayStr(arg));
|
: ArrayUtil.toString(arg));
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
buf.append("<toString() failure: ").append(t).append(">");
|
buf.append("<toString() failure: ").append(t).append(">");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user