KickJava   Java API By Example, From Geeks To Geeks.

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


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.SerializationUtil;
8 import com.tc.object.dna.api.DNACursor;
9 import com.tc.object.dna.api.DNAWriter;
10 import com.tc.object.dna.api.LogicalAction;
11 import com.tc.object.dna.api.PhysicalAction;
12 import com.tc.objectserver.mgmt.ManagedObjectFacade;
13 import com.tc.util.Assert;
14
15 import java.io.IOException JavaDoc;
16 import java.io.ObjectInput JavaDoc;
17 import java.io.ObjectOutput JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.LinkedHashMap JavaDoc;
20 import java.util.Map.Entry;
21
22 /**
23  * state for maps
24  */

25 public class LinkedHashMapManagedObjectState extends PartialMapManagedObjectState {
26   private static final String JavaDoc ACCESS_ORDER_FIELDNAME = "java.util.LinkedHashMap.accessOrder";
27   private boolean accessOrder = false;
28
29   // TODO:: Come back and make this partial too
30
LinkedHashMapManagedObjectState(long classID) {
31     super(classID, new LinkedHashMap JavaDoc(1));
32   }
33   
34   protected LinkedHashMapManagedObjectState(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     if (!cursor.next()) { return; }
40     Object JavaDoc action = cursor.getAction();
41     if (action instanceof PhysicalAction) {
42       PhysicalAction physicalAction = (PhysicalAction) action;
43       Assert.assertEquals(ACCESS_ORDER_FIELDNAME, physicalAction.getFieldName());
44       setAccessOrder(physicalAction.getObject());
45     } else {
46       LogicalAction logicalAction = (LogicalAction) action;
47       int method = logicalAction.getMethod();
48       Object JavaDoc[] params = logicalAction.getParameters();
49       applyMethod(objectID, includeIDs, method, params);
50     }
51
52     while (cursor.next()) {
53       LogicalAction logicalAction = cursor.getLogicalAction();
54       int method = logicalAction.getMethod();
55       Object JavaDoc[] params = logicalAction.getParameters();
56       applyMethod(objectID, includeIDs, method, params);
57     }
58   }
59
60   protected void applyMethod(ObjectID objectID, BackReferences includeIDS, int method, Object JavaDoc[] params) {
61     switch (method) {
62       case SerializationUtil.GET:
63         ((LinkedHashMap JavaDoc) references).get(params[0]);
64         break;
65       default:
66         super.applyMethod(objectID, includeIDS, method, params);
67     }
68   }
69
70   private void setAccessOrder(Object JavaDoc accessOrderObject) {
71     try {
72       Assert.assertTrue(accessOrderObject instanceof Boolean JavaDoc);
73       accessOrder = ((Boolean JavaDoc) accessOrderObject).booleanValue();
74       references = new LinkedHashMap JavaDoc(1, 0.75f, accessOrder);
75     } catch (Exception JavaDoc e) {
76       throw new RuntimeException JavaDoc(e);
77     }
78   }
79
80   public void dehydrate(ObjectID objectID, DNAWriter writer) {
81     writer.addPhysicalAction(ACCESS_ORDER_FIELDNAME, new Boolean JavaDoc(accessOrder));
82     super.dehydrate(objectID, writer);
83   }
84
85   // TODO: The Facade does not include the access order.
86
public ManagedObjectFacade createFacade(ObjectID objectID, String JavaDoc className, int limit) {
87     return super.createFacade(objectID, className, limit);
88   }
89
90   public byte getType() {
91     return LINKED_HASHMAP_TYPE;
92   }
93   
94   //TODO:: Until partial collections support is enabled for this class
95
protected void basicWriteTo(ObjectOutput JavaDoc out) throws IOException JavaDoc {
96     out.writeBoolean(accessOrder);
97     out.writeInt(references.size());
98     for (Iterator JavaDoc i = references.entrySet().iterator(); i.hasNext();) {
99       Entry entry = (Entry) i.next();
100       out.writeObject(entry.getKey());
101       out.writeObject(entry.getValue());
102     }
103   }
104
105   // TODO:: Until CollectionsPersistor saves retrieves data in references map.
106
static MapManagedObjectState readFrom(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
107     LinkedHashMapManagedObjectState linkedsmo = new LinkedHashMapManagedObjectState(in);
108     linkedsmo.accessOrder = in.readBoolean();
109     int size = in.readInt();
110     LinkedHashMap JavaDoc map = new LinkedHashMap JavaDoc(size, 0.75f, linkedsmo.accessOrder);
111     for (int i = 0; i < size; i++) {
112       Object JavaDoc key = in.readObject();
113       Object JavaDoc value = in.readObject();
114       map.put(key, value);
115     }
116     linkedsmo.setMap(map);
117     return linkedsmo;
118   }
119   
120 }
Popular Tags