KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > common > CollectionDiff


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.common;
13
14 import com.versant.core.metadata.MDStatics;
15 import com.versant.core.util.OIDObjectInput;
16 import com.versant.core.util.OIDObjectOutput;
17 import com.versant.core.util.FastExternalizable;
18
19 import java.io.*;
20
21
22 /**
23  * This is the base class for classes that hold the differences between two
24  * collections or maps. A subclass of this is stored in the new state when
25  * changes to the the collection are persisted.
26  */

27 public abstract class CollectionDiff implements FastExternalizable, Cloneable JavaDoc {
28
29     public static final Object JavaDoc[] EMPTY_OBJECT_ARRAY = new Object JavaDoc[0];
30
31     public VersantFieldMetaData fmd;
32
33     /**
34      * This is a new or replaced collection and therefore everything
35      * in the store must be deleted if it does exist.
36      */

37     public static final int STATUS_NEW = 1;
38
39     public int status;
40
41     /**
42      * The inserted values.
43      */

44     public Object JavaDoc[] insertedValues;
45
46     public CollectionDiff() {
47     }
48
49     public CollectionDiff(VersantFieldMetaData fmd) {
50         this.fmd = fmd;
51     }
52
53     public String JavaDoc toString() {
54         return "<CollectionDiff: status = " + status + ": amount added ="
55                 + (insertedValues != null ? insertedValues.length : -1) + ">";
56     }
57
58     protected void write(OIDObjectOutput out, int typeCode, boolean pc,
59             Object JavaDoc[] data)
60             throws IOException {
61         int n = data == null ? 0 : data.length;
62 // System.out.println("%%% CollectionDiff.write " + n);
63
out.writeInt(n);
64         if (pc) {
65             for (int i = 0; i < n; i++) {
66                 out.write((OID)data[i]);
67             }
68         } else {
69             for (int i = 0; i < n; i++) {
70                 out.writeObject(data[i]);
71             }
72         }
73     }
74
75     protected Object JavaDoc[] read(OIDObjectInput in, int typeCode, boolean pc)
76             throws IOException, ClassNotFoundException JavaDoc {
77         int n = in.readInt();
78 // System.out.println("%%% CollectionDiff.read " + n);
79
if (n <= 0) {
80             return null;
81         }
82         Object JavaDoc[] data = new Object JavaDoc[n];
83         if (pc) {
84             for (int i = 0; i < n; i++) {
85                 data[i] = in.readOID();
86             }
87         } else {
88             for (int i = 0; i < n; i++) {
89                 data[i] = in.readObject();
90             }
91         }
92         return data;
93     }
94
95     public void writeExternal(OIDObjectOutput out) throws IOException {
96         out.writeByte(status);
97         write(out, fmd.getElementTypeCode(), fmd.isElementTypePC(),
98                 insertedValues);
99     }
100
101     public void readExternal(OIDObjectInput in) throws IOException,
102             ClassNotFoundException JavaDoc {
103         status = in.readByte();
104         insertedValues = read(in, fmd.getElementTypeCode(),
105                 fmd.isElementTypePC());
106     }
107
108     protected Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
109         CollectionDiff cloned = (CollectionDiff)super.clone();
110         cloned.insertedValues = new Object JavaDoc[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 }
120
Popular Tags