KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

27 public class TreeSetManagedObjectState extends SetManagedObjectState {
28   private static final String JavaDoc COMPARATOR_FIELDNAME = TreeMapManagedObjectState.COMPARATOR_FIELDNAME;
29
30   private ObjectID comparator = null;
31
32   public TreeSetManagedObjectState(long classID) {
33     super(classID);
34   }
35
36   protected TreeSetManagedObjectState(ObjectInput JavaDoc in) throws IOException JavaDoc {
37     super(in);
38   }
39
40   public void apply(ObjectID objectID, DNACursor cursor, BackReferences includeIDs) throws IOException JavaDoc {
41     if (!cursor.next()) { return; }
42
43     Object JavaDoc action = cursor.getAction();
44     if (action instanceof PhysicalAction) {
45       PhysicalAction pa = (PhysicalAction) action;
46       Assert.assertEquals(COMPARATOR_FIELDNAME, pa.getFieldName());
47       this.comparator = (ObjectID) pa.getObject();
48     } else {
49       LogicalAction la = (LogicalAction) action;
50       int method = la.getMethod();
51       Object JavaDoc[] params = la.getParameters();
52       super.apply(objectID, method, params, includeIDs);
53     }
54
55     while (cursor.next()) {
56       LogicalAction la = cursor.getLogicalAction();
57       int method = la.getMethod();
58       Object JavaDoc[] params = la.getParameters();
59       super.apply(objectID, method, params, includeIDs);
60     }
61   }
62
63   public void dehydrate(ObjectID objectID, DNAWriter writer) {
64     if (comparator != null) {
65       writer.addPhysicalAction(COMPARATOR_FIELDNAME, comparator);
66     }
67     super.dehydrate(objectID, writer);
68   }
69
70   protected Collection JavaDoc getAllReferences() {
71     List JavaDoc allReferences = new ArrayList JavaDoc(references);
72     if (comparator != null) {
73       allReferences.add(comparator);
74     }
75     return allReferences;
76   }
77
78   // TODO This Facade has not included the comparator.
79
public ManagedObjectFacade createFacade(ObjectID objectID, String JavaDoc className, int limit) {
80     return super.createFacade(objectID, className, limit);
81   }
82
83   protected void basicWriteTo(ObjectOutput JavaDoc out) throws IOException JavaDoc {
84     if (comparator == null) {
85       out.writeBoolean(false);
86     } else {
87       out.writeBoolean(true);
88       out.writeLong(comparator.toLong());
89     }
90     out.writeInt(references.size());
91     for (Iterator JavaDoc i = references.iterator(); i.hasNext();) {
92       out.writeObject(i.next());
93     }
94   }
95
96   protected boolean basicEquals(Object JavaDoc other) {
97     TreeSetManagedObjectState mo = (TreeSetManagedObjectState) other;
98     return (comparator == mo.comparator || (comparator != null && comparator.equals(mo.comparator)))
99            && references.equals(mo.references);
100   }
101
102   static SetManagedObjectState readFrom(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
103     TreeSetManagedObjectState tsm = new TreeSetManagedObjectState(in);
104     ObjectID comparator;
105     if (in.readBoolean()) {
106       comparator = new ObjectID(in.readLong());
107     } else {
108       comparator = null;
109     }
110     int size = in.readInt();
111     Set JavaDoc set = new LinkedHashSet JavaDoc(size, 0.75f);
112     for (int i = 0; i < size; i++) {
113       set.add(in.readObject());
114     }
115     tsm.comparator = comparator;
116     tsm.references = set;
117     return tsm;
118   }
119
120   public byte getType() {
121     return TREE_SET_TYPE;
122   }
123 }
124
Popular Tags