From b74769d5f6ea96c37dd966872226ce0af180dc73 Mon Sep 17 00:00:00 2001 From: mdb Date: Tue, 31 Oct 2000 00:51:13 +0000 Subject: [PATCH] Added a toString() that does something nice with arrays. git-svn-id: https://samskivert.googlecode.com/svn/trunk@13 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../java/com/samskivert/util/StringUtil.java | 82 ++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) diff --git a/projects/samskivert/src/java/com/samskivert/util/StringUtil.java b/projects/samskivert/src/java/com/samskivert/util/StringUtil.java index 00b80bc5..7f6b11bd 100644 --- a/projects/samskivert/src/java/com/samskivert/util/StringUtil.java +++ b/projects/samskivert/src/java/com/samskivert/util/StringUtil.java @@ -1,5 +1,5 @@ // -// $Id: StringUtil.java,v 1.1 2000/10/31 00:04:15 mdb Exp $ +// $Id: StringUtil.java,v 1.2 2000/10/31 00:51:13 mdb Exp $ package com.samskivert.util; @@ -15,4 +15,84 @@ public class StringUtil { return (value == null || value.trim().length() == 0); } + + /** + * Converts the supplied object to a string. Normally this is + * accomplished via the object's built in toString() + * method, but in the case of arrays, toString() is + * called on each element and the contents are listed like so: + * + *
+     * (value, value, value)
+     * 
+ * + * Arrays of ints, longs, floats and doubles are also handled for + * convenience. Also note that passing null will result in the string + * "null" being returned. + */ + public static String toString (Object val) + { + StringBuffer buf = new StringBuffer(); + + if (val instanceof int[]) { + buf.append("("); + int[] v = (int[])val; + for (int i = 0; i < v.length; i++) { + if (i > 0) { + buf.append(", "); + } + buf.append(v[i]); + } + buf.append(")"); + + } else if (val instanceof long[]) { + buf.append("("); + long[] v = (long[])val; + for (int i = 0; i < v.length; i++) { + if (i > 0) { + buf.append(", "); + } + buf.append(v[i]); + } + buf.append(")"); + + } else if (val instanceof float[]) { + buf.append("("); + float[] v = (float[])val; + for (int i = 0; i < v.length; i++) { + if (i > 0) { + buf.append(", "); + } + buf.append(v[i]); + } + buf.append(")"); + + } else if (val instanceof double[]) { + buf.append("("); + double[] v = (double[])val; + for (int i = 0; i < v.length; i++) { + if (i > 0) { + buf.append(", "); + } + buf.append(v[i]); + } + buf.append(")"); + + } else if (val instanceof Object[]) { + buf.append("("); + Object[] v = (Object[])val; + for (int i = 0; i < v.length; i++) { + if (i > 0) { + buf.append(", "); + } + buf.append(v[i]); + } + buf.append(")"); + + } else { + buf.append(val); + } + + return buf.toString(); + } }