001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.common;
012:
013: import com.versant.core.metadata.MDStatics;
014: import com.versant.core.util.OIDObjectInput;
015: import com.versant.core.util.OIDObjectOutput;
016: import com.versant.core.util.FastExternalizable;
017:
018: import java.io.*;
019:
020: /**
021: * This is the base class for classes that hold the differences between two
022: * collections or maps. A subclass of this is stored in the new state when
023: * changes to the the collection are persisted.
024: */
025: public abstract class CollectionDiff implements FastExternalizable,
026: Cloneable {
027:
028: public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
029:
030: public VersantFieldMetaData fmd;
031:
032: /**
033: * This is a new or replaced collection and therefore everything
034: * in the store must be deleted if it does exist.
035: */
036: public static final int STATUS_NEW = 1;
037:
038: public int status;
039:
040: /**
041: * The inserted values.
042: */
043: public Object[] insertedValues;
044:
045: public CollectionDiff() {
046: }
047:
048: public CollectionDiff(VersantFieldMetaData fmd) {
049: this .fmd = fmd;
050: }
051:
052: public String toString() {
053: return "<CollectionDiff: status = " + status
054: + ": amount added ="
055: + (insertedValues != null ? insertedValues.length : -1)
056: + ">";
057: }
058:
059: protected void write(OIDObjectOutput out, int typeCode, boolean pc,
060: Object[] data) throws IOException {
061: int n = data == null ? 0 : data.length;
062: // System.out.println("%%% CollectionDiff.write " + n);
063: out.writeInt(n);
064: if (pc) {
065: for (int i = 0; i < n; i++) {
066: out.write((OID) data[i]);
067: }
068: } else {
069: for (int i = 0; i < n; i++) {
070: out.writeObject(data[i]);
071: }
072: }
073: }
074:
075: protected Object[] read(OIDObjectInput in, int typeCode, boolean pc)
076: throws IOException, ClassNotFoundException {
077: int n = in.readInt();
078: // System.out.println("%%% CollectionDiff.read " + n);
079: if (n <= 0) {
080: return null;
081: }
082: Object[] data = new Object[n];
083: if (pc) {
084: for (int i = 0; i < n; i++) {
085: data[i] = in.readOID();
086: }
087: } else {
088: for (int i = 0; i < n; i++) {
089: data[i] = in.readObject();
090: }
091: }
092: return data;
093: }
094:
095: public void writeExternal(OIDObjectOutput out) throws IOException {
096: out.writeByte(status);
097: write(out, fmd.getElementTypeCode(), fmd.isElementTypePC(),
098: insertedValues);
099: }
100:
101: public void readExternal(OIDObjectInput in) throws IOException,
102: ClassNotFoundException {
103: status = in.readByte();
104: insertedValues = read(in, fmd.getElementTypeCode(), fmd
105: .isElementTypePC());
106: }
107:
108: protected Object clone() throws CloneNotSupportedException {
109: CollectionDiff cloned = (CollectionDiff) super .clone();
110: cloned.insertedValues = new Object[insertedValues.length];
111: System.arraycopy(insertedValues, 0, cloned.insertedValues, 0,
112: insertedValues.length);
113: return cloned;
114: }
115:
116: public void dump() {
117: }
118:
119: }
|