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:
@@ -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
|
// samskivert library - useful routines for java programs
|
||||||
// Copyright (C) 2001 Michael Bayne
|
// Copyright (C) 2001 Michael Bayne
|
||||||
@@ -422,6 +422,34 @@ public class StringUtil
|
|||||||
return vals;
|
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
|
* Parses an array of strings from a single string. The array should
|
||||||
* be represented as a bare list of strings separated by commas, for
|
* 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;
|
package com.samskivert.util;
|
||||||
|
|
||||||
@@ -42,6 +42,7 @@ public class ValueMarshaller
|
|||||||
protected static HashMap _parsers;
|
protected static HashMap _parsers;
|
||||||
|
|
||||||
protected static final int[] INT_ARRAY_PROTOTYPE = new int[0];
|
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];
|
protected static final String[] STRING_ARRAY_PROTOTYPE = new String[0];
|
||||||
|
|
||||||
static {
|
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!
|
// and string arrays, oh my!
|
||||||
_parsers.put(STRING_ARRAY_PROTOTYPE.getClass(), new Parser() {
|
_parsers.put(STRING_ARRAY_PROTOTYPE.getClass(), new Parser() {
|
||||||
public Object parse (String source) throws Exception {
|
public Object parse (String source) throws Exception {
|
||||||
|
|||||||
Reference in New Issue
Block a user