KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > managedobject > SetManagedObjectState


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.objectserver.managedobject;
6
7 import com.tc.object.ObjectID;
8 import com.tc.object.SerializationUtil;
9 import com.tc.object.dna.api.DNACursor;
10 import com.tc.object.dna.api.DNAWriter;
11 import com.tc.object.dna.api.LogicalAction;
12 import com.tc.objectserver.mgmt.LogicalManagedObjectFacade;
13 import com.tc.objectserver.mgmt.ManagedObjectFacade;
14
15 import java.io.IOException JavaDoc;
16 import java.io.ObjectInput JavaDoc;
17 import java.io.ObjectOutput JavaDoc;
18 import java.util.Arrays JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.LinkedHashSet JavaDoc;
21 import java.util.Set JavaDoc;
22
23 /**
24  * ManagedObjectState for sets.
25  */

26 public class SetManagedObjectState extends LogicalManagedObjectState {
27   protected Set JavaDoc references;
28
29   SetManagedObjectState(long classID) {
30     super(classID);
31     this.references = new LinkedHashSet JavaDoc(1, 0.75f); // Accommodating LinkedHashSet.
32
}
33
34   protected SetManagedObjectState(ObjectInput JavaDoc in) throws IOException JavaDoc {
35     super(in);
36   }
37
38   public void apply(ObjectID objectID, DNACursor cursor, BackReferences includeIDs) throws IOException JavaDoc {
39     while (cursor.next()) {
40       LogicalAction action = cursor.getLogicalAction();
41       int method = action.getMethod();
42       Object JavaDoc[] params = action.getParameters();
43       apply(objectID, method, params, includeIDs);
44     }
45   }
46
47   protected void apply(ObjectID objectID, int method, Object JavaDoc[] params, BackReferences includeIDs) {
48     switch (method) {
49       case SerializationUtil.ADD:
50         Object JavaDoc v = getValue(params);
51         addChangeToCollector(objectID, v, includeIDs);
52         references.add(v);
53         break;
54       case SerializationUtil.REMOVE:
55         references.remove(params[0]);
56         break;
57       case SerializationUtil.REMOVE_ALL:
58         references.removeAll(Arrays.asList(params));
59         break;
60       case SerializationUtil.CLEAR:
61         references.clear();
62         break;
63       default:
64         throw new AssertionError JavaDoc("Invalid action:" + method);
65     }
66   }
67
68   private Object JavaDoc getValue(Object JavaDoc[] params) {
69     // hack for trove sets which replace the old set value (java ones do the opposite) clean this up
70
return params.length == 2 ? params[1] : params[0];
71   }
72
73   private void addChangeToCollector(ObjectID objectID, Object JavaDoc newValue, BackReferences includeIDs) {
74     if (newValue instanceof ObjectID) {
75       getListener().changed(objectID, null, (ObjectID) newValue);
76       includeIDs.addBackReference((ObjectID) newValue, objectID);
77     }
78   }
79
80   public void dehydrate(ObjectID objectID, DNAWriter writer) {
81     for (Iterator JavaDoc i = references.iterator(); i.hasNext();) {
82       Object JavaDoc value = i.next();
83       writer.addLogicalAction(SerializationUtil.ADD, new Object JavaDoc[] { value });
84     }
85   }
86
87   protected void addAllObjectReferencesTo(Set JavaDoc refs) {
88     addAllObjectReferencesFromIteratorTo(this.references.iterator(), refs);
89   }
90
91   public ManagedObjectFacade createFacade(ObjectID objectID, String JavaDoc className, int limit) {
92     final int size = references.size();
93
94     if (limit < 0) {
95       limit = size;
96     } else {
97       limit = Math.min(limit, size);
98     }
99
100     Object JavaDoc[] data = new Object JavaDoc[limit];
101
102     int index = 0;
103     for (Iterator JavaDoc iter = references.iterator(); iter.hasNext() && index < limit; index++) {
104       data[index] = iter.next();
105     }
106
107     return LogicalManagedObjectFacade.createSetInstance(objectID, className, data, size);
108   }
109
110   public byte getType() {
111     return SET_TYPE;
112   }
113
114   protected void basicWriteTo(ObjectOutput JavaDoc out) throws IOException JavaDoc {
115     out.writeInt(references.size());
116     for (Iterator JavaDoc i = references.iterator(); i.hasNext();) {
117       out.writeObject(i.next());
118     }
119   }
120
121   protected boolean basicEquals(LogicalManagedObjectState o) {
122     SetManagedObjectState mo = (SetManagedObjectState) o;
123     return references.equals(mo.references);
124   }
125
126   static SetManagedObjectState readFrom(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
127     SetManagedObjectState setmo = new SetManagedObjectState(in);
128     int size = in.readInt();
129     Set JavaDoc set = new LinkedHashSet JavaDoc(size, 0.75f);
130     for (int i = 0; i < size; i++) {
131       set.add(in.readObject());
132     }
133     setmo.references = set;
134     return setmo;
135   }
136 }
137
Popular Tags