KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > metadata > JdbcIndex


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc.metadata;
13
14 import java.io.Serializable JavaDoc;
15
16 /**
17  * An index for a Table.
18  */

19 public class JdbcIndex implements Serializable JavaDoc {
20
21     public String JavaDoc name;
22     public JdbcColumn[] cols;
23     public boolean unique;
24     public boolean clustered;
25     public String JavaDoc comment;
26
27     public JdbcIndex() {
28     }
29
30     public String JavaDoc toString() {
31         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
32         s.append(name);
33         s.append('(');
34         s.append(cols[0]);
35         for (int i = 1; i < cols.length; i++) {
36             s.append(',');
37             s.append(' ');
38             s.append(cols[i]);
39         }
40         s.append(')');
41         if (unique) s.append(" unique");
42         if (clustered) s.append(" clustered");
43         if (cols[0].table != null) s.append(" to table " + cols[0].table.name);
44         return s.toString();
45     }
46
47     /**
48      * Set the columns for this index and set the partOfIndex flag on each
49      * column.
50      */

51     public void setCols(JdbcColumn[] cols) {
52         this.cols = cols;
53         for (int i = cols.length - 1; i >= 0; i--) {
54             cols[i].partOfIndex = true;
55         }
56     }
57
58     /**
59      * Do we have the same columns as x
60      */

61     public boolean hasSameColumns(JdbcIndex x) {
62         int n = cols.length;
63         if (n != x.cols.length) return false;
64         for (int i = 0; i < n; i++) {
65             if (!cols[i].equals(x.cols[i])) return false;
66         }
67         return true;
68     }
69
70     /**
71      * Indexes are equal if they have the same columns.
72      */

73     public boolean equals(Object JavaDoc o) {
74         return o instanceof JdbcIndex && hasSameColumns((JdbcIndex)o);
75     }
76
77     /**
78      * Hashcode depends on columns.
79      */

80     public int hashCode() {
81         int ans = 0;
82         for (int i = cols.length - 1; i >= 0; i--) {
83             if (cols[i].name != null) {
84                 ans += cols[i].name.hashCode() * 29;
85             }
86         }
87         return ans;
88     }
89
90 }
91
92
93
Popular Tags