01: /*
02: * Created on Nov 25, 2005
03: */
04: package uk.org.ponder.conversion;
05:
06: import uk.org.ponder.streamutil.read.LexUtil;
07: import uk.org.ponder.streamutil.read.PushbackRIS;
08: import uk.org.ponder.streamutil.read.StringRIS;
09: import uk.org.ponder.stringutil.CharWrap;
10: import uk.org.ponder.util.UniversalRuntimeException;
11:
12: // QQQQQ diabolically inefficient. Need to replace parse method with reader
13: // from CharWrap directly.
14: public class doubleArrayParser implements LeafObjectParser {
15: public Object parse(String string) {
16: try {
17: PushbackRIS lr = new PushbackRIS(new StringRIS(string));
18: int size = LexUtil.readInt(lr);
19: double[] togo = new double[size];
20: LexUtil.expect(lr, ":");
21: for (int i = 0; i < size; ++i) {
22: LexUtil.skipWhite(lr);
23: try {
24: togo[i] = LexUtil.readDouble(lr);
25: } catch (Exception e) {
26: UniversalRuntimeException.accumulate(e,
27: "Error reading double vector at position "
28: + i + " of expected " + size);
29: }
30: }
31: LexUtil.skipWhite(lr);
32: LexUtil.expectEmpty(lr);
33: return togo;
34: } catch (Exception e) {
35: throw UniversalRuntimeException.accumulate(e,
36: "Error reading double vector");
37: }
38: }
39:
40: public String render(Object torendero) {
41: double[] torender = (double[]) torendero;
42: CharWrap renderinto = new CharWrap(torender.length * 15);
43: renderinto.append(Integer.toString(torender.length) + ": ");
44: for (int i = 0; i < torender.length; ++i) {
45: renderinto.append(Double.toString(torender[i]) + " ");
46: }
47: return renderinto.toString();
48: }
49:
50: public Object copy(Object tocopyo) {
51: double[] tocopy = (double[]) tocopyo;
52: double[] copy = new double[tocopy.length];
53: System.arraycopy(tocopy, 0, copy, 0, tocopy.length);
54: return copy;
55: }
56: }
|