KickJava   Java API By Example, From Geeks To Geeks.

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


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.FetchGroup;
15 import com.versant.core.metadata.ModelMetaData;
16 import com.versant.core.metadata.ClassMetaData;
17 import com.versant.core.server.*;
18
19 import java.io.*;
20 import java.util.*;
21
22 import com.versant.core.storagemanager.ApplicationContext;
23 import com.versant.core.util.OIDObjectInput;
24 import com.versant.core.util.OIDObjectOutput;
25 import com.versant.core.util.FastExternalizable;
26
27 /**
28  * This keeps a map of OID -> State and can also track the order that the
29  * OID State pairs are added.
30  */

31 public final class StatesReturned implements StateContainer, FastExternalizable {
32
33     private EntrySet data = new EntrySet();
34     private transient ApplicationContext context;
35     //The first direct oid added.
36
private OID directOid;
37
38     public transient StatesReturned next;
39
40     public StatesReturned() {
41     }
42
43     public StatesReturned(ApplicationContext context) {
44         setContext(context);
45     }
46
47     public ApplicationContext getContext() {
48         return context;
49     }
50
51     public void setContext(ApplicationContext context) {
52         this.context = context;
53     }
54
55     public int size() {
56         return data.size();
57     }
58
59     /**
60      * This Iterator returns Entry{@link com.versant.core.common.EntrySet.Entry} instances each with an OID key and
61      * State value.
62      */

63     public Iterator iterator() {
64         return data.iterator();
65     }
66
67     /**
68      * This Iterator returns OID instances.
69      */

70     public Iterator iteratorForOIDs() {
71         return data.newKeyIterator();
72     }
73
74     /**
75      * This is called to indicate that we are busy resolving the state for
76      * this OID. This is used to stop infinite recursion.
77      */

78     public void visited(OID oid) {
79         addImp(oid, null, false);
80     }
81
82     public void ensureDirect(Object JavaDoc[] oids) {
83         if (oids == null) return;
84         for (int i = 0; i < oids.length; i++) {
85             if (oids[i] == null) break;
86             addImp((OID)oids[i], null, true);
87         }
88     }
89
90     /**
91      * - If contain this oid and the state is null then definitly return false.
92      * This is to avoid recursive fetching.
93      * - If contain the oid and the state is not null then check if the state
94      * contains the fg.
95      * - If not contain this oid then ask delegate stateReceiver
96      */

97     public boolean isStateRequired(OID oid, FetchGroup fetchGroup) {
98         if (data.contains(oid)) {
99             EntrySet.Entry e = data.get(oid);
100             State s = (State) e.getValue();
101             if (s == null) {
102                 return false;
103             }
104             return !s.containsFetchGroup(fetchGroup);
105         }
106         return context == null || context.isStateRequired(oid, fetchGroup);
107     }
108
109     /**
110      * Callback to add indirect state.
111      */

112     public void addState(OID oid, State state) {
113         if (Debug.DEBUG) {
114             if (state == null) {
115                 throw BindingSupportImpl.getInstance().internal("Null state not allowed");
116             }
117         }
118         addIndirect(oid, state);
119     }
120
121     /**
122      * Add an entry to the table. If the key is already present in the table,
123      * this replaces the existing value associated with the key.
124      */

125     public State add(OID key, State value) {
126         addImp(key, value, true);
127         return value;
128     }
129
130     private void addImp(OID oid, State state, boolean directFlag) {
131         if (oid == null) {
132             throw BindingSupportImpl.getInstance().illegalArgument(
133                     "null oid not supported");
134         }
135         if (directFlag && directOid == null) {
136             directOid = oid;
137         }
138
139         EntrySet.Entry e = data.get(oid);
140         if (e == null) {
141             data.add(oid, state, directFlag);
142         } else {
143             State current = (State) e.getValue();
144             if (current == null) {
145                 e.setValue(state);
146             } else if (state != null && current != state) {
147                 current.updateNonFilled(state);
148             }
149             if (directFlag) e.setDirect(true);
150         }
151     }
152
153     public void addIndirect(OID key, State value) {
154         addImp(key, value, false);
155     }
156
157     /**
158      * Check if an entry is present in the table. This method is supplied to
159      * support the use of values matching the reserved not found value.
160      */

161     public boolean containsKey(Object JavaDoc key) {
162         return data.contains((OID) key);
163     }
164
165     /**
166      * Find an entry in the table.
167      */

168     public State get(Object JavaDoc key) {
169         EntrySet.Entry e = data.get((OID) key);
170         if (e == null) return null;
171         return (State) e.getValue();
172     }
173
174     public EntrySet.Entry getEntry(Object JavaDoc key) {
175         return data.get((OID) key);
176     }
177
178     public void clear() {
179         data.clear();
180         directOid = null;
181     }
182
183     /**
184      * Get the first direct OID or null if none.
185      */

186     public OID getDirectOID() {
187         return directOid;
188     }
189
190     public void addIndirectOIDs(CachedQueryResult cacheContainer) {
191         if (cacheContainer.indirectOIDs == null) {
192             cacheContainer.indirectOIDs = new OIDArray();
193         }
194         OIDArray indirectOIDs = cacheContainer.indirectOIDs;
195
196         for (Iterator i = iteratorForOIDs(); i.hasNext(); ) {
197             OID oid = (OID)i.next();
198             indirectOIDs.add(oid);
199         }
200     }
201
202     public void writeExternal(OIDObjectOutput out) throws IOException {
203         int n = data.size();
204         out.writeInt(n);
205         for (Iterator i = iterator(); i.hasNext(); ) {
206             EntrySet.Entry e = (EntrySet.Entry)i.next();
207             OID oid = (OID)e.getKey();
208             State state = (State)e.getValue();
209             if (state == null) {
210                 out.writeShort(-1);
211                 out.write(oid);
212             } else if (state == NULLState.NULL_STATE) {
213                 out.writeShort(-2);
214                 out.write(oid);
215             } else {
216                 out.writeShort(state.getClassIndex());
217                 oid.resolve(state);
218                 out.writeWithoutCMD(oid);
219                 state.writeExternal(out);
220             }
221             out.writeBoolean(e.direct);
222         }
223         out.write(directOid);
224     }
225
226     public void readExternal(OIDObjectInput in) throws IOException,
227             ClassNotFoundException JavaDoc {
228         ModelMetaData jmd = in.getModelMetaData();
229         int n = in.readInt();
230         for (int i = 0; i < n; i++) {
231             OID oid;
232             State state;
233             int ci = in.readShort();
234             switch (ci) {
235                 case -1:
236                     oid = in.readOID();
237                     state = null;
238                     break;
239                 case -2:
240                     oid = in.readOID();
241                     state = NULLState.NULL_STATE;
242                     break;
243                 default:
244                     ClassMetaData cmd = jmd.classes[ci];
245                     oid = in.readOID(cmd);
246                     state = cmd.createState();
247                     state.readExternal(in);
248             };
249             data.add(oid, state, in.readBoolean());
250         }
251         directOid = in.readOID();
252     }
253
254     public void dump() {
255         System.out.println("-- StatesReturned");
256         System.out.println("Map:");
257         for (Iterator i = iterator(); i.hasNext(); ) {
258             EntrySet.Entry e = (EntrySet.Entry)i.next();
259             OID oid = (OID)e.getKey();
260             State state = (State)e.getValue();
261             System.out.println(oid.toStringImp() + " -> " + state + " direct " + e.isDirect());
262         }
263         System.out.println("---");
264     }
265
266 }
267
Popular Tags