001: package app.data.test;
002:
003: import javax.persistence.Embeddable;
004:
005: /**
006: * Id for User Preference
007: */
008: @Embeddable
009: public class UserPreferenceId {
010:
011: String prefKey;
012:
013: Integer userId;
014:
015: /**
016: * Default constructor.
017: */
018: public UserPreferenceId() {
019: }
020:
021: /**
022: * Construct with fields.
023: */
024: public UserPreferenceId(String prefKey, Integer userId) {
025: this .prefKey = prefKey;
026: this .userId = userId;
027: }
028:
029: /**
030: * Return pref key.
031: */
032: public String getPrefKey() {
033: return prefKey;
034: }
035:
036: /**
037: * Set pref key.
038: */
039: public void setPrefKey(String prefKey) {
040: this .prefKey = prefKey;
041: }
042:
043: /**
044: * Return user id.
045: */
046: public Integer getUserId() {
047: return userId;
048: }
049:
050: /**
051: * Set user id.
052: */
053: public void setUserId(Integer userId) {
054: this .userId = userId;
055: }
056:
057: public String toString() {
058: return "prefKey=" + prefKey + "," + "userId=" + userId;
059: }
060:
061: /**
062: * equals by field.
063: */
064: public boolean equals(Object o) {
065: if (o instanceof UserPreferenceId == false) {
066: return false;
067: }
068: if (o == this ) {
069: return true;
070: }
071: return o.hashCode() == hashCode();
072: }
073:
074: /**
075: * store the hashcode so that it doesn't change if
076: * first calculated when fields are null.
077: */
078: private Integer hashCodeValue;
079:
080: /**
081: * Hashcode based on non null fields.
082: */
083: public int hashCode() {
084:
085: if (hashCodeValue != null) {
086: return hashCodeValue.intValue();
087: }
088: // expecting all fields to be null or not null
089: if (prefKey != null && userId != null) {
090:
091: int hc = getClass().hashCode();
092: hc = 31 * (hc + prefKey.hashCode());
093: hc = 31 * (hc + userId.hashCode());
094:
095: hashCodeValue = new Integer(hc);
096: return hc;
097: }
098: // going to be using instance equality from now on
099: int hc = super .hashCode();
100: hashCodeValue = new Integer(hc);
101: return hc;
102: }
103:
104: }
|