Menu

[r34]: / tjacobs / util / PrimaryKey.java  Maximize  Restore  History

Download this file

91 lines (76 with data), 2.5 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package tjacobs.util;
/**
* The reason d'etre for PrimaryKey is to make overriding equals, hashCode, and compareTo simpler and easier to
* implement so that they are consistent with one another in data object classes.
* @author tom_jacobs
*
* @param <T>
*/
public interface PrimaryKey<T> {
public Object[] getPrimaryKey();
public boolean equals(T obj);
public int hashCode();
public interface ComparablePrimaryKey<T> extends Comparable<T>, PrimaryKey<T> {
@SuppressWarnings("unchecked")
public Comparable[] getPrimaryKey();
}
public static class Impl {
@SuppressWarnings("unchecked")
public static final boolean equalsImpl(PrimaryKey key1, PrimaryKey key2) {
Object[] data1 = key1.getPrimaryKey();
Object[] data2 = key2.getPrimaryKey();
//don't check length equal - let that get thrown as an exception
try {
for (int i = 0; i < data1.length; i++) {
if (data1[i] == null && data2[i] == null) continue;
if (data1[i] == null || data2[i] == null) return false;
if (!data1[i].equals(data2[i])) return false;
}
}
catch (ArrayIndexOutOfBoundsException ex) {
//throw new Error("Class: " + key1.getClass() + " has invalid implementation of getPrimaryKey");
ex.printStackTrace();
return false;
}
return true;
}
@SuppressWarnings("unchecked")
public static final int compareTo(ComparablePrimaryKey key1, ComparablePrimaryKey key2) {
Comparable[] data1 = key1.getPrimaryKey();
Comparable[] data2 = key2.getPrimaryKey();
for (int i = 0; i < data1.length; i++) {
System.out.println("comparing: " + data1[i] + " to " + data2[i]);
int compare = data1[i].compareTo(data2[i]);
if (compare != 0) {
System.out.println("returning: " + compare);
return compare;
}
}
return 0;
}
@SuppressWarnings("unchecked")
public static final int hashCode(PrimaryKey key) {
Object[] data1 = key.getPrimaryKey();
long hashCode = 0;
for (int i= 0; i < data1.length; i++) {
hashCode += data1[i].hashCode();
}
return (int) hashCode;
}
/*
* model equals, hashCode, compareTo methods
public boolean equals(Object o) {
if (o.getClass() != getClass()) {
return false;
}
return PrimaryKey.Impl.equalsImpl( this, o);
}
public int hashCode() {
return PrimaryKey.Impl.hashCode(this);
}
public int compareTo(PrimaryKey obj) {
return PrimaryKey.Impl.compareTo(this, obj);
}
*/
}
}