01: /*
02: * Created on Oct 14, 2004
03: */
04: package net.sourceforge.orbroker;
05:
06: import java.util.StringTokenizer;
07:
08: /**
09: * @author Nils Kilden-Pedersen
10: */
11: final class ResultKeyDef {
12:
13: private static Column[] buildKeyColumns(
14: final String delimitedColumns) {
15: StringTokenizer tokens = new StringTokenizer(delimitedColumns,
16: " ,;:+\r\n");
17: Column[] columns = new Column[tokens.countTokens()];
18: for (int i = 0; i < columns.length; i++) {
19: String columnName = tokens.nextToken();
20: columns[i] = new Column(columnName, Object.class);
21: }
22: return columns;
23: }
24:
25: private final Column[] columns;
26:
27: private final String resultObjectName;
28:
29: ResultKeyDef(final String delimitedColumns,
30: final String resultObjectName) {
31: this .resultObjectName = resultObjectName;
32: if (delimitedColumns.trim().equals("*")) {
33: this .columns = null;
34: return;
35: }
36: this .columns = buildKeyColumns(delimitedColumns);
37: if (this .columns.length == 0) {
38: throw new ConfigurationException(
39: "No columns given for key.");
40: }
41: }
42:
43: /**
44: * Get column values for key columns.
45: * If any column is null, return a null array.
46: * @param row
47: * @return column values
48: */
49: private Object[] getDefinedColumns(ResultRow row) {
50: Object[] values = new Object[this .columns.length];
51: for (int i = 0; i < values.length; i++) {
52: values[i] = this .columns[i].getValue(row);
53: if (values[i] == null) {
54: return null;
55: }
56: }
57: return values;
58: }
59:
60: /**
61: * Get key values
62: *
63: * @param row
64: * @return key or null, if any column was null.
65: */
66: ResultKey getResultKey(ResultRow row) {
67: if (this .columns == null) {
68: return new ResultKey(this .resultObjectName);
69: }
70: Object[] values = getDefinedColumns(row);
71: if (values == null) {
72: return null;
73: }
74: return new ResultKey(this .resultObjectName, values);
75: }
76:
77: /**
78: * @inheritDoc
79: * @see java.lang.Object#toString()
80: */
81: public String toString() {
82: StringBuffer buffer = new StringBuffer();
83: buffer.append("Key columns:");
84: for (int i = 0; i < this .columns.length; i++) {
85: buffer.append(" ").append(this.columns[i].getName());
86: }
87: return buffer.toString();
88: }
89: }
|