01: /*
02: * Created on Oct 14, 2004
03: */
04: package net.sourceforge.orbroker;
05:
06: import java.util.Arrays;
07:
08: /**
09: * @author Nils Kilden-Pedersen
10: */
11: final class ResultKey {
12:
13: private static final Object[] EMPTY_KEY = new Object[0];
14:
15: private static int buildHashCode(String name, Object[] values) {
16: int hash = 0;
17: for (int i = 0; i < values.length; i++) {
18: hash = 37 * hash + values[i].hashCode();
19: }
20: return hash + name.hashCode();
21: }
22:
23: private static String buildToString(String name, Object[] values) {
24: StringBuffer buffer = new StringBuffer();
25: buffer.append(name).append("(");
26: for (int i = 0; i < values.length; i++) {
27: if (i > 0) {
28: buffer.append(", ");
29: }
30: buffer.append(values[i]);
31: }
32: buffer.append(")");
33: return buffer.toString();
34: }
35:
36: private final int hash;
37: private final String resultObjectName;
38: private final String toString;
39: private final Object[] values;
40:
41: ResultKey(final String resultObjectName) {
42: this .resultObjectName = resultObjectName;
43: this .values = EMPTY_KEY;
44: this .hash = 0;
45: this .toString = buildToString(resultObjectName, this .values);
46: }
47:
48: ResultKey(final String resultObjectName, final Object[] values) {
49: assert (values != null && values.length > 0) : "Invalid value array passed. Is either null or of 0 length.";
50: this .resultObjectName = resultObjectName;
51: this .values = values;
52: this .hash = buildHashCode(resultObjectName, values);
53: this .toString = buildToString(resultObjectName, values);
54: }
55:
56: /**
57: * A valid key is one that is not defined as "*".
58: * @return true if no key exists
59: */
60: boolean isValidKey() {
61: return this .values.length > 0;
62: }
63:
64: /**
65: * @inheritDoc
66: * @see java.lang.Object#equals(java.lang.Object)
67: */
68: public boolean equals(Object obj) {
69: return equals((ResultKey) obj);
70: }
71:
72: /**
73: * Compare two keys
74: * @param comp
75: * @return true, if equal
76: * @see java.lang.Object#equals(java.lang.Object)
77: */
78: public boolean equals(ResultKey comp) {
79: return this .resultObjectName.equals(comp.resultObjectName)
80: && Arrays.equals(this .values, comp.values);
81: }
82:
83: /**
84: * @inheritDoc
85: * @see java.lang.Object#hashCode()
86: */
87: public int hashCode() {
88: return this .hash;
89: }
90:
91: /**
92: * @inheritDoc
93: * @see java.lang.Object#toString()
94: */
95: public String toString() {
96: return this.toString;
97: }
98: }
|