Added support for parsing arrays of floats.

git-svn-id: https://samskivert.googlecode.com/svn/trunk@642 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2002-03-08 02:40:02 +00:00
parent dda33209b5
commit 9392e2ccfd
2 changed files with 38 additions and 2 deletions
@@ -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:
*
* <pre>
* 25.0, .5, 1, 0.99
* </pre>
*
* 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
@@ -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 {