01: /*
02: * Copyright (c) 1998 - 2005 Versant Corporation
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * Versant Corporation - initial API and implementation
10: */
11: package com.versant.core.jdbc.metadata;
12:
13: import java.io.Serializable;
14:
15: /**
16: * An index for a Table.
17: */
18: public class JdbcIndex implements Serializable {
19:
20: public String name;
21: public JdbcColumn[] cols;
22: public boolean unique;
23: public boolean clustered;
24: public String comment;
25:
26: public JdbcIndex() {
27: }
28:
29: public String toString() {
30: StringBuffer s = new StringBuffer();
31: s.append(name);
32: s.append('(');
33: s.append(cols[0]);
34: for (int i = 1; i < cols.length; i++) {
35: s.append(',');
36: s.append(' ');
37: s.append(cols[i]);
38: }
39: s.append(')');
40: if (unique)
41: s.append(" unique");
42: if (clustered)
43: s.append(" clustered");
44: if (cols[0].table != null)
45: s.append(" to table " + cols[0].table.name);
46: return s.toString();
47: }
48:
49: /**
50: * Set the columns for this index and set the partOfIndex flag on each
51: * column.
52: */
53: public void setCols(JdbcColumn[] cols) {
54: this .cols = cols;
55: for (int i = cols.length - 1; i >= 0; i--) {
56: cols[i].partOfIndex = true;
57: }
58: }
59:
60: /**
61: * Do we have the same columns as x
62: */
63: public boolean hasSameColumns(JdbcIndex x) {
64: int n = cols.length;
65: if (n != x.cols.length)
66: return false;
67: for (int i = 0; i < n; i++) {
68: if (!cols[i].equals(x.cols[i]))
69: return false;
70: }
71: return true;
72: }
73:
74: /**
75: * Indexes are equal if they have the same columns.
76: */
77: public boolean equals(Object o) {
78: return o instanceof JdbcIndex && hasSameColumns((JdbcIndex) o);
79: }
80:
81: /**
82: * Hashcode depends on columns.
83: */
84: public int hashCode() {
85: int ans = 0;
86: for (int i = cols.length - 1; i >= 0; i--) {
87: if (cols[i].name != null) {
88: ans += cols[i].name.hashCode() * 29;
89: }
90: }
91: return ans;
92: }
93:
94: }
|