001: /**
002: * Objective Database Abstraction Layer (ODAL)
003: * Copyright (c) 2004, The ODAL Development Group
004: * All rights reserved.
005: * For definition of the ODAL Development Group please refer to LICENCE.txt file
006: *
007: * Distributable under LGPL license.
008: * See terms of license at gnu.org.
009: */package com.completex.objective.components.persistency.key.impl;
010:
011: import com.completex.objective.components.persistency.PersistentObject;
012: import com.completex.objective.components.persistency.key.SimpleNaturalKey;
013:
014: import java.io.Externalizable;
015: import java.io.IOException;
016: import java.io.ObjectInput;
017: import java.io.ObjectOutput;
018: import java.util.Arrays;
019:
020: /**
021: * Compound Natural Key can be used as key for Compound Persistent Objects
022: * @see com.completex.objective.components.persistency.AbstractPersistentObject
023: *
024: * @author Gennady Krizhevsky
025: */
026: public class CompoundNaturalKeyImpl implements Externalizable {
027: static final long serialVersionUID = 1L;
028: private String className;
029: private SimpleNaturalKey[] naturalKeys;
030:
031: public CompoundNaturalKeyImpl() {
032: }
033:
034: public CompoundNaturalKeyImpl(String className,
035: SimpleNaturalKey[] naturalKeys) {
036: this .className = className;
037: this .naturalKeys = naturalKeys;
038: }
039:
040: public CompoundNaturalKeyImpl(Class clazz,
041: SimpleNaturalKey firstKey,
042: PersistentObject[] persistentObjects) {
043: className = clazz.getName();
044: naturalKeys = new SimpleNaturalKey[persistentObjects.length];
045: for (int i = 0; i < persistentObjects.length; i++) {
046: PersistentObject persistentObject = persistentObjects[i];
047: if (firstKey != null) {
048: naturalKeys[i] = firstKey;
049: } else {
050: naturalKeys[i] = resolveToKey(persistentObject);
051: }
052: }
053: }
054:
055: protected SimpleNaturalKey resolveToKey(
056: PersistentObject persistentObject) {
057: if (persistentObject == null) {
058: return SimpleNaturalKey.NULL_KEY;
059: } else {
060: return (SimpleNaturalKey) persistentObject.toKey();
061: }
062: }
063:
064: public boolean equals(Object o) {
065: if (this == o)
066: return true;
067: if (o == null || getClass() != o.getClass())
068: return false;
069: final CompoundNaturalKeyImpl that = (CompoundNaturalKeyImpl) o;
070: if (className != null ? !className.equals(that.className)
071: : that.className != null)
072: return false;
073: return Arrays.equals(naturalKeys, that.naturalKeys);
074: }
075:
076: public int hashCode() {
077: int result = 0;
078: for (int i = 0; i < naturalKeys.length; i++) {
079: result = AbstractSimpleNaturalKey.hashCodeFromValue(result,
080: naturalKeys[i]);
081: }
082: return result;
083: }
084:
085: public String toString() {
086: StringBuffer result = new StringBuffer(className);
087: for (int i = 0; i < naturalKeys.length; i++) {
088: result.append(AbstractSimpleNaturalKey.SEP).append(
089: naturalKeys[i].toString());
090: }
091: return result.toString();
092: }
093:
094: public String getClassName() {
095: return className;
096: }
097:
098: public void setClassName(String className) {
099: this .className = className;
100: }
101:
102: public SimpleNaturalKey[] getNaturalKeys() {
103: return naturalKeys;
104: }
105:
106: public void setNaturalKeys(SimpleNaturalKey[] naturalKeys) {
107: this .naturalKeys = naturalKeys;
108: }
109:
110: public void writeExternal(ObjectOutput out) throws IOException {
111: out.writeObject(className);
112: out.writeObject(naturalKeys);
113: }
114:
115: public void readExternal(ObjectInput in) throws IOException,
116: ClassNotFoundException {
117: className = (String) in.readObject();
118: naturalKeys = (SimpleNaturalKey[]) in.readObject();
119: }
120:
121: }
|