KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.PrettyPrintable;
13 import com.tc.text.PrettyPrinter;
14 import com.tc.util.Assert;
15
16 import java.io.IOException JavaDoc;
17 import java.io.ObjectInput JavaDoc;
18 import java.io.ObjectOutput JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23 import java.util.Map.Entry;
24
25 /**
26  * state for tree maps
27  */

28 public class TreeMapManagedObjectState extends MapManagedObjectState implements PrettyPrintable {
29   static final String JavaDoc COMPARATOR_FIELDNAME = "java.util.TreeMap.comparator";
30   private ObjectID comparator = null;
31
32   TreeMapManagedObjectState(long classID) {
33     super(classID, new HashMap JavaDoc());
34   }
35
36   protected TreeMapManagedObjectState(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     while (cursor.next()) {
42       Object JavaDoc action = cursor.getAction();
43       if (action instanceof PhysicalAction) {
44         PhysicalAction pa = (PhysicalAction) action;
45         Assert.assertEquals(COMPARATOR_FIELDNAME, pa.getFieldName());
46         this.comparator = (ObjectID) pa.getObject();
47       } else {
48         LogicalAction la = (LogicalAction) action;
49         int method = la.getMethod();
50         Object JavaDoc[] params = la.getParameters();
51         applyMethod(objectID, includeIDs, method, params);
52       }
53     }
54   }
55
56   public void dehydrate(ObjectID objectID, DNAWriter writer) {
57     if (comparator != null) {
58       writer.addPhysicalAction(COMPARATOR_FIELDNAME, comparator);
59     }
60     super.dehydrate(objectID, writer);
61   }
62
63   protected void addAllObjectReferencesTo(Set JavaDoc refs) {
64     super.addAllObjectReferencesTo(refs);
65     if (comparator != null) {
66       refs.add(comparator);
67     }
68   }
69
70   public PrettyPrinter prettyPrint(PrettyPrinter out) {
71     PrettyPrinter rv = out;
72     out = out.println("TreeMapManagedObjectState").duplicateAndIndent();
73     out.indent().println("references: " + references);
74     out.indent().println("comparator: " + comparator);
75     return rv;
76   }
77
78   public ManagedObjectFacade createFacade(ObjectID objectID, String JavaDoc className, int limit) {
79     // XXX: This facade is incomplete...it doesn't include the comparator field
80
return super.createFacade(objectID,className, limit);
81   }
82
83   public byte getType() {
84     return TREE_MAP_TYPE;
85   }
86
87   protected void basicWriteTo(ObjectOutput JavaDoc out) throws IOException JavaDoc {
88     if (comparator == null) {
89       out.writeBoolean(false);
90     } else {
91       out.writeBoolean(true);
92       out.writeLong(comparator.toLong());
93     }
94     out.writeInt(references.size());
95     for (Iterator JavaDoc i = references.entrySet().iterator(); i.hasNext();) {
96       Entry entry = (Entry) i.next();
97       out.writeObject(entry.getKey());
98       out.writeObject(entry.getValue());
99     }
100   }
101
102   protected boolean basicEquals(LogicalManagedObjectState o) {
103     TreeMapManagedObjectState mo = (TreeMapManagedObjectState) o;
104     return (comparator == mo.comparator || (comparator != null && comparator.equals(mo.comparator)))
105            && super.basicEquals(o);
106   }
107
108   static MapManagedObjectState readFrom(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
109     TreeMapManagedObjectState tm = new TreeMapManagedObjectState(in);
110     ObjectID comparator;
111     if (in.readBoolean()) {
112       comparator = new ObjectID(in.readLong());
113     } else {
114       comparator = null;
115     }
116     int size = in.readInt();
117     Map JavaDoc map = new HashMap JavaDoc(size);
118     for (int i = 0; i < size; i++) {
119       map.put(in.readObject(), in.readObject());
120     }
121     tm.references = map;
122     tm.comparator = comparator;
123     
124     return tm;
125   }
126 }
127
Popular Tags