diff --git a/projects/samskivert/src/java/com/samskivert/util/StringUtil.java b/projects/samskivert/src/java/com/samskivert/util/StringUtil.java index 19a6d3f7..6687847c 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.31 2002/02/23 18:24:02 mdb Exp $ +// $Id: StringUtil.java,v 1.32 2002/03/08 02:40:02 mdb Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Michael Bayne @@ -422,6 +422,34 @@ public class StringUtil return vals; } + /** + * Parses an array of floats from it's string representation. The + * array should be represented as a bare list of numbers separated by + * commas, for example: + * + *
+ * 25.0, .5, 1, 0.99 + *+ * + * Any inability to parse the array will result in the function + * returning null. + */ + public static float[] parseFloatArray (String source) + { + StringTokenizer tok = new StringTokenizer(source, ","); + float[] vals = new float[tok.countTokens()]; + for (int i = 0; tok.hasMoreTokens(); i++) { + try { + // trim the whitespace from the token + String token = tok.nextToken().trim(); + vals[i] = Float.parseFloat(token); + } catch (NumberFormatException nfe) { + return null; + } + } + return vals; + } + /** * Parses an array of strings from a single string. The array should * be represented as a bare list of strings separated by commas, for diff --git a/projects/samskivert/src/java/com/samskivert/util/ValueMarshaller.java b/projects/samskivert/src/java/com/samskivert/util/ValueMarshaller.java index 21487dab..33663d30 100644 --- a/projects/samskivert/src/java/com/samskivert/util/ValueMarshaller.java +++ b/projects/samskivert/src/java/com/samskivert/util/ValueMarshaller.java @@ -1,5 +1,5 @@ // -// $Id: ValueMarshaller.java,v 1.3 2002/03/08 02:07:58 mdb Exp $ +// $Id: ValueMarshaller.java,v 1.4 2002/03/08 02:40:02 mdb Exp $ package com.samskivert.util; @@ -42,6 +42,7 @@ public class ValueMarshaller protected static HashMap _parsers; protected static final int[] INT_ARRAY_PROTOTYPE = new int[0]; + protected static final float[] FLOAT_ARRAY_PROTOTYPE = new float[0]; protected static final String[] STRING_ARRAY_PROTOTYPE = new String[0]; static { @@ -82,6 +83,13 @@ public class ValueMarshaller } }); + // and float arrays + _parsers.put(FLOAT_ARRAY_PROTOTYPE.getClass(), new Parser() { + public Object parse (String source) throws Exception { + return StringUtil.parseFloatArray(source); + } + }); + // and string arrays, oh my! _parsers.put(STRING_ARRAY_PROTOTYPE.getClass(), new Parser() { public Object parse (String source) throws Exception {