KickJava   Java API By Example, From Geeks To Geeks.

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


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.ClassMetaData;
15 import com.versant.core.metadata.ModelMetaData;
16 import com.versant.core.util.OIDObjectOutput;
17 import com.versant.core.util.OIDObjectInput;
18
19 import java.io.*;
20 import java.util.Comparator JavaDoc;
21 import java.util.Arrays JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 /**
25  * Collection of OIDs and States to be deleted. If this is constucted with
26  * keepStates == false then then states array is null and added states will
27  * be silently dropped. If keepStates == true then the array will be not
28  * null but the entry for a given OID may still be null if this information
29  * was not available. In this case the datastore will have to read the
30  * object prior to deleting it.
31  */

32 public final class DeletePacket {
33
34     private static final int INITIAL_CAPACITY = 20;
35     private static final int GROW_FACTOR = 2;
36
37     private ModelMetaData jmd;
38     private boolean keepStates;
39     public OID[] oids = new OID[INITIAL_CAPACITY];
40     public State[] states;
41
42     private int size;
43
44     /**
45      * This is for Externalizable.
46      */

47     public DeletePacket() {
48     }
49
50     public DeletePacket(ModelMetaData jmd) {
51         this.jmd =jmd;
52         keepStates = jmd.sendStateOnDelete;
53         if (keepStates) states = new State[INITIAL_CAPACITY];
54     }
55
56     /**
57      * Add a state to the container to be deleted.
58      */

59     public void add(OID oid, State state) {
60         if (Debug.DEBUG) {
61             if (oid.isNew()) {
62                 BindingSupportImpl.getInstance().internal("oid is new: " + oid);
63             }
64             // make sure untyped OIDs are not added
65
if (oid.getAvailableClassMetaData() == null) {
66                 BindingSupportImpl.getInstance().internal("oid is untyped: " +
67                         oid);
68             }
69         }
70         resize();
71         if (keepStates) states[size] = state;
72         oids[size++] = oid;
73     }
74
75     /**
76      * The number of entries in the container.
77      */

78     public int size() {
79         return size;
80     }
81
82     /**
83      * Is the container empty?
84      */

85     public boolean isEmpty() {
86         return size == 0;
87     }
88
89     /**
90      * Check if the arrays needs to grow some more.
91      */

92     private void resize() {
93         int length = oids.length;
94         if (length == size) {
95             int growTo = (length + 1) * GROW_FACTOR;
96             OID[] tmpOIDs = new OID[growTo];
97             System.arraycopy(oids, 0, tmpOIDs, 0, length);
98             oids = tmpOIDs;
99             if (keepStates) {
100                 State[] tmpStates = new State[growTo];
101                 System.arraycopy(states, 0, tmpStates, 0, length);
102                 states = tmpStates;
103             }
104         }
105     }
106
107     /**
108      * Clear the container for reuse.
109      */

110     public void clear() {
111         oids = new OID[INITIAL_CAPACITY];
112         if (keepStates) {
113             states = new State[INITIAL_CAPACITY];
114         }
115         size = 0;
116     }
117
118     public boolean isKeepStates() {
119         return keepStates;
120     }
121
122     /**
123      * Sort the OIDs. This is a NOP if keepStates is true.
124      */

125     public void sortOIDs(Comparator JavaDoc comp) {
126         if (keepStates) return;
127         states = null;
128         if (size <= 1) return;
129         if (size == 2) {
130             if (comp.compare(oids[0], oids[1]) > 0) {
131                 OID t = oids[0];
132                 oids[0] = oids[1];
133                 oids[1] = t;
134             }
135             return;
136         }
137         Arrays.sort(oids, 0, size, comp);
138     }
139
140     public void dump() {
141         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("DeleteOIDStateContainer: ");
142         for (int i = 0; i < oids.length; i++) {
143             OID oid = oids[i];
144             if (oid == null) break;
145             sb.append("\nOID = " + oid.toSString());
146             if (keepStates) {
147                 sb.append("\nState = " + states[i]);
148             }
149             sb.append("\nNext");
150         }
151         System.out.println(sb.toString());
152     }
153
154     public void writeExternal(OIDObjectOutput out) throws IOException {
155         out.writeBoolean(keepStates);
156         out.writeInt(size);
157         if (keepStates) {
158             for (int i = 0; i < size; i++) {
159                 oids[i].resolve(states[i]);
160                 out.writeShort(states[i].getClassIndex());
161                 out.writeWithoutCMD(oids[i]);
162                 states[i].writeExternal(out);
163             }
164         } else {
165             for (int i = 0; i < size; i++) {
166                 out.write(oids[i]);
167             }
168         }
169     }
170
171     public void readExternal(OIDObjectInput in) throws IOException,
172             ClassNotFoundException JavaDoc {
173         jmd = in.getModelMetaData();
174         keepStates = in.readBoolean();
175         size = in.readInt();
176         oids = new OID[size];
177         if (keepStates) {
178             states = new State[size];
179             for (int i = 0; i < size; i++) {
180                 ClassMetaData cmd = jmd.classes[in.readShort()];
181                 oids[i] = in.readOID(cmd);
182                 states[i] = cmd.createState();
183                 states[i].readExternal(in);
184             }
185         } else {
186             for (int i = 0; i < size; i++) {
187                 oids[i] = in.readOID();
188             }
189         }
190     }
191
192     /**
193      * Iterate over the OIDs.
194      */

195     public Iterator JavaDoc iterator() {
196         return new Iter();
197     }
198
199     public class Iter implements Iterator JavaDoc {
200
201         private int pos;
202
203         public boolean hasNext() {
204             return pos < size - 1;
205         }
206
207         public Object JavaDoc next() {
208             return oids[pos++];
209         }
210
211         public void remove() {
212             throw new UnsupportedOperationException JavaDoc();
213         }
214
215     }
216
217 }
218
Popular Tags