1 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 ; 16 import java.io.ObjectInput ; 17 import java.io.ObjectOutput ; 18 import java.util.Iterator ; 19 import java.util.LinkedHashMap ; 20 import java.util.Map.Entry; 21 22 25 public class LinkedHashMapManagedObjectState extends PartialMapManagedObjectState { 26 private static final String ACCESS_ORDER_FIELDNAME = "java.util.LinkedHashMap.accessOrder"; 27 private boolean accessOrder = false; 28 29 LinkedHashMapManagedObjectState(long classID) { 31 super(classID, new LinkedHashMap (1)); 32 } 33 34 protected LinkedHashMapManagedObjectState(ObjectInput in) throws IOException { 35 super(in); 36 } 37 38 public void apply(ObjectID objectID, DNACursor cursor, BackReferences includeIDs) throws IOException { 39 if (!cursor.next()) { return; } 40 Object 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 [] 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 [] params = logicalAction.getParameters(); 56 applyMethod(objectID, includeIDs, method, params); 57 } 58 } 59 60 protected void applyMethod(ObjectID objectID, BackReferences includeIDS, int method, Object [] params) { 61 switch (method) { 62 case SerializationUtil.GET: 63 ((LinkedHashMap ) references).get(params[0]); 64 break; 65 default: 66 super.applyMethod(objectID, includeIDS, method, params); 67 } 68 } 69 70 private void setAccessOrder(Object accessOrderObject) { 71 try { 72 Assert.assertTrue(accessOrderObject instanceof Boolean ); 73 accessOrder = ((Boolean ) accessOrderObject).booleanValue(); 74 references = new LinkedHashMap (1, 0.75f, accessOrder); 75 } catch (Exception e) { 76 throw new RuntimeException (e); 77 } 78 } 79 80 public void dehydrate(ObjectID objectID, DNAWriter writer) { 81 writer.addPhysicalAction(ACCESS_ORDER_FIELDNAME, new Boolean (accessOrder)); 82 super.dehydrate(objectID, writer); 83 } 84 85 public ManagedObjectFacade createFacade(ObjectID objectID, String className, int limit) { 87 return super.createFacade(objectID, className, limit); 88 } 89 90 public byte getType() { 91 return LINKED_HASHMAP_TYPE; 92 } 93 94 protected void basicWriteTo(ObjectOutput out) throws IOException { 96 out.writeBoolean(accessOrder); 97 out.writeInt(references.size()); 98 for (Iterator 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 static MapManagedObjectState readFrom(ObjectInput in) throws IOException , ClassNotFoundException { 107 LinkedHashMapManagedObjectState linkedsmo = new LinkedHashMapManagedObjectState(in); 108 linkedsmo.accessOrder = in.readBoolean(); 109 int size = in.readInt(); 110 LinkedHashMap map = new LinkedHashMap (size, 0.75f, linkedsmo.accessOrder); 111 for (int i = 0; i < size; i++) { 112 Object key = in.readObject(); 113 Object value = in.readObject(); 114 map.put(key, value); 115 } 116 linkedsmo.setMap(map); 117 return linkedsmo; 118 } 119 120 } | Popular Tags |