01: /*
02: * User: Michael Rettig
03: * Date: Aug 31, 2002
04: * Time: 6:27:37 PM
05: */
06: package net.sourceforge.jaxor;
07:
08: import net.sourceforge.jaxor.api.ValueHolder;
09: import net.sourceforge.jaxor.api.ObjectHolder;
10: import net.sourceforge.jaxor.util.ObjectHolderImpl;
11: import net.sourceforge.jaxor.util.ObjectUtils;
12:
13: import java.io.Serializable;
14:
15: /**
16: * This class represents the collection of the keys and values for
17: * each of the AttributeMapper instances which are part of the
18: * Entity's primary key.
19: */
20:
21: public class PrimaryKeySet implements Serializable {
22: private final ObjectHolder[] _fields;
23:
24: public PrimaryKeySet(ObjectHolder adapt) {
25: this (new ObjectHolder[] { adapt });
26: }
27:
28: public PrimaryKeySet(ObjectHolder[] fields) {
29: _fields = fields;
30: }
31:
32: public boolean equals(Object obj) {
33: if (obj instanceof PrimaryKeySet)
34: return equals((PrimaryKeySet) obj);
35: return false;
36: }
37:
38: public boolean equals(PrimaryKeySet obj) {
39: if (_fields.length != obj._fields.length)
40: return false;
41: for (int i = 0; i < _fields.length; i++) {
42: ObjectHolder field = _fields[i];
43: if (!ObjectUtils.equals(field.getValue(), obj._fields[i]
44: .getValue()))
45: return false;
46: }
47: return true;
48: }
49:
50: public int hashCode() {
51: int hash = 0;
52: for (int i = 0; i < _fields.length; i++) {
53: ObjectHolder field = _fields[i];
54: Object value = field.getValue();
55: if (value != null)
56: hash += value.hashCode();
57: }
58: return hash;
59: }
60:
61: public int size() {
62: return _fields.length;
63: }
64:
65: public ObjectHolder[] getFields() {
66: return _fields;
67: }
68:
69: public PrimaryKeySet deepCopy() {
70: return (PrimaryKeySet) clone();
71: }
72:
73: protected Object clone() {
74: ObjectHolder[] clones = new ObjectHolder[size()];
75: for (int i = 0; i < _fields.length; i++) {
76: clones[i] = new ObjectHolderImpl(_fields[i].getValue());
77: }
78: return new PrimaryKeySet(clones);
79: }
80:
81: }
|