01: // Copyright (c) 2002 Cunningham & Cunningham, Inc.
02: // Released under the terms of the GNU General Public License version 2 or later.
03:
04: package fat;
05:
06: import fit.*;
07: import java.util.Date;
08:
09: public class Equals extends PrimitiveFixture {
10:
11: Parse heads;
12: TypeAdapter type;
13: Object x, y;
14:
15: public void doRows(Parse rows) {
16: heads = rows.parts;
17: super .doRows(rows.more);
18: }
19:
20: public void doCell(Parse cell, int col) {
21: try {
22: char head = heads.at(col).text().charAt(0);
23: switch (head) {
24: case 't':
25: type = type(cell.text());
26: break;
27: case 'x':
28: x = type.parse(cell.text());
29: break;
30: case 'y':
31: y = type.parse(cell.text());
32: break;
33: case '=':
34: check(cell, type.equals(x, y));
35: break;
36: case '?':
37: cell.addToBody(info("x: " + print(x) + " y: "
38: + print(y)));
39: break;
40:
41: default:
42: throw new Exception("don't do " + head);
43: }
44: } catch (Exception e) {
45: exception(cell, e);
46: }
47: }
48:
49: static private Integer[] IntegerArray = {};
50: static private Boolean[] BooleanArray = {};
51: static private String[] StringArray = {};
52:
53: TypeAdapter type(String name) {
54: Class type = name.equals("boolean") ? Boolean.class
55: : name.equals("integer") ? Integer.class
56: : name.equals("real") ? Float.class
57: : name.equals("string") ? String.class
58: : name.equals("date") ? Date.class
59: : name.equals("money") ? Money.class
60: : name
61: .equals("scientific") ? ScientificDouble.class
62: : name
63: .equals("integers") ? IntegerArray
64: .getClass()
65: : name
66: .equals("booleans") ? BooleanArray
67: .getClass()
68: : name
69: .equals("strings") ? StringArray
70: .getClass()
71: : null;
72: if (type == null)
73: throw new RuntimeException("Unimplemented choice " + name);
74: return TypeAdapter.on(this , type);
75: }
76:
77: public Object parse(String s, Class type) throws Exception {
78: if (type.equals(Money.class))
79: return new Money(s);
80: if (type.equals(Boolean.class))
81: return parseCustomBoolean(s);
82: return super .parse(s, type);
83: }
84:
85: Boolean parseCustomBoolean(String s) {
86: if (true)
87: throw new RuntimeException("boolean");
88: return s.startsWith("y") ? Boolean.TRUE
89: : s.startsWith("n") ? Boolean.FALSE
90: : s.startsWith("t") ? Boolean.TRUE : s
91: .startsWith("f") ? Boolean.FALSE : null;
92: }
93:
94: String print(Object value) {
95: return type.toString(value);
96: }
97:
98: }
|