01: /*
02: * Created on Oct 11, 2004
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: /**
13: * @author Antranig Basman (amb26@ponder.org.uk)
14: *
15: */
16:
17: // QQQQQ diabolically inefficient. Need to replace parse method with reader
18: // from CharWrap directly.
19: public class intArrayParser implements LeafObjectParser {
20: public static intArrayParser instance = new intArrayParser();
21:
22: public Object parse(String string) {
23: try {
24: PushbackRIS lr = new PushbackRIS(new StringRIS(string));
25: int size = LexUtil.readInt(lr);
26: int[] togo = new int[size];
27: LexUtil.expect(lr, ":");
28: for (int i = 0; i < size; ++i) {
29: LexUtil.skipWhite(lr);
30: try {
31: togo[i] = LexUtil.readInt(lr);
32: } catch (Exception e) {
33: UniversalRuntimeException.accumulate(e,
34: "Error reading integer vector at position "
35: + i + " of expected " + size);
36: }
37: }
38: LexUtil.skipWhite(lr);
39: LexUtil.expectEmpty(lr);
40: return togo;
41: } catch (Exception e) {
42: throw UniversalRuntimeException.accumulate(e,
43: "Error reading integer vector");
44: }
45:
46: }
47:
48: public String render(Object torendero) {
49: int[] torender = (int[]) torendero;
50: CharWrap renderinto = new CharWrap(torender.length * 5);
51: renderinto.append(Integer.toString(torender.length) + ": ");
52: for (int i = 0; i < torender.length; ++i) {
53: renderinto.append(Integer.toString(torender[i]) + " ");
54: }
55: return renderinto.toString();
56: }
57:
58: public Object copy(Object tocopyo) {
59: int[] tocopy = (int[]) tocopyo;
60: int[] copy = new int[tocopy.length];
61: System.arraycopy(tocopy, 0, copy, 0, tocopy.length);
62: return copy;
63: }
64: }
|