01: package liquibase.database.structure;
02:
03: import liquibase.util.StringUtils;
04:
05: import java.util.ArrayList;
06: import java.util.List;
07:
08: public class PrimaryKey implements DatabaseObject,
09: Comparable<PrimaryKey> {
10: private String name;
11: private List<String> columnNames = new ArrayList<String>();
12: private Table table;
13:
14: public String getName() {
15: return name;
16: }
17:
18: public void setName(String name) {
19: this .name = name;
20: }
21:
22: public String getColumnNames() {
23: return StringUtils.join(columnNames, ", ");
24: }
25:
26: public void addColumnName(int position, String columnName) {
27: if (position >= columnNames.size()) {
28: for (int i = columnNames.size() - 1; i < position; i++) {
29: this .columnNames.add(null);
30: }
31: }
32: this .columnNames.set(position, columnName);
33: }
34:
35: public Table getTable() {
36: return table;
37: }
38:
39: public void setTable(Table table) {
40: this .table = table;
41: }
42:
43: public int compareTo(PrimaryKey o) {
44: int returnValue = this .getTable().getName().compareTo(
45: o.getTable().getName());
46: if (returnValue == 0) {
47: returnValue = this .getColumnNames().compareTo(
48: o.getColumnNames());
49: }
50: // if (returnValue == 0) {
51: // returnValue = this.getName().compareTo(o.getName());
52: // }
53:
54: return returnValue;
55: }
56:
57: public boolean equals(Object o) {
58: if (this == o)
59: return true;
60: if (o == null || getClass() != o.getClass())
61: return false;
62:
63: PrimaryKey that = (PrimaryKey) o;
64:
65: return !(getColumnNames() != null ? !getColumnNames()
66: .equalsIgnoreCase(that.getColumnNames()) : that
67: .getColumnNames() != null)
68: && !(getTable().getName() != null ? !getTable()
69: .getName().equalsIgnoreCase(
70: that.getTable().getName()) : that
71: .getTable().getName() != null);
72:
73: }
74:
75: public int hashCode() {
76: int result;
77: result = (getColumnNames() != null ? getColumnNames()
78: .toUpperCase().hashCode() : 0);
79: result = 31
80: * result
81: + (table.getName() != null ? table.getName()
82: .toUpperCase().hashCode() : 0);
83: return result;
84: }
85:
86: public String toString() {
87: return getName() + " on " + getTable().getName() + "("
88: + getColumnNames() + ")";
89: }
90:
91: public List<String> getColumnNamesAsList() {
92: return columnNames;
93: }
94: }
|