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.objectserver.mgmt.FacadeUtil; 12 import com.tc.objectserver.mgmt.LogicalManagedObjectFacade; 13 import com.tc.objectserver.mgmt.ManagedObjectFacade; 14 import com.tc.objectserver.mgmt.MapEntryFacade; 15 import com.tc.objectserver.mgmt.MapEntryFacadeImpl; 16 import com.tc.text.PrettyPrintable; 17 import com.tc.text.PrettyPrinter; 18 19 import java.io.IOException ; 20 import java.io.ObjectInput ; 21 import java.io.ObjectOutput ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.Set ; 25 import java.util.Map.Entry; 26 27 30 public class MapManagedObjectState extends LogicalManagedObjectState implements PrettyPrintable { 31 protected Map references; 32 33 protected MapManagedObjectState(long classID, Map map) { 34 super(classID); 35 references = map; 36 } 37 38 protected MapManagedObjectState(ObjectInput in) throws IOException { 39 super(in); 40 } 41 42 public void apply(ObjectID objectID, DNACursor cursor, BackReferences includeIDs) throws IOException { 43 while (cursor.next()) { 44 LogicalAction action = cursor.getLogicalAction(); 45 int method = action.getMethod(); 46 Object [] params = action.getParameters(); 47 applyMethod(objectID, includeIDs, method, params); 48 } 49 } 50 51 protected void applyMethod(ObjectID objectID, BackReferences includeIDs, int method, Object [] params) { 52 switch (method) { 53 case SerializationUtil.PUT: 54 55 mapPreProcess(params); 56 Object key = getKey(params); 57 Object value = getValue(params); 58 references.put(key, value); 59 if (key instanceof ObjectID) { 60 ObjectID v = (ObjectID) key; 61 getListener().changed(objectID, null, v); 62 addBackReferenceForKey(includeIDs, v, objectID); 63 } 64 if (value instanceof ObjectID) { 65 ObjectID v = (ObjectID) value; 66 getListener().changed(objectID, null, v); 67 addBackReferenceForValue(includeIDs, v, objectID); 68 } 69 break; 70 case SerializationUtil.REMOVE: 71 references.remove(params[0]); 72 break; 73 case SerializationUtil.CLEAR: 74 references.clear(); 75 break; 76 default: 77 throw new AssertionError ("Invalid action:" + method); 78 } 79 80 } 81 82 protected void addBackReferenceForKey(BackReferences includeIDs, ObjectID key, ObjectID map) { 83 includeIDs.addBackReference(key, map); 84 } 85 86 protected void addBackReferenceForValue(BackReferences includeIDs, ObjectID value, ObjectID map) { 87 includeIDs.addBackReference(value, map); 88 } 89 90 91 private Object getKey(Object [] params) { 92 return params.length == 3 ? params[1] : params[0]; 94 } 95 96 private Object getValue(Object [] params) { 97 return params.length == 3 ? params[2] : params[1]; 99 } 100 101 private void mapPreProcess(Object [] params) { 102 if (params.length == 3) { 104 references.remove(params[0]); 105 } 106 } 107 108 public void dehydrate(ObjectID objectID, DNAWriter writer) { 109 for (Iterator i = references.entrySet().iterator(); i.hasNext();) { 110 Entry entry = (Entry) i.next(); 111 Object key = entry.getKey(); 112 Object value = entry.getValue(); 113 writer.addLogicalAction(SerializationUtil.PUT, new Object [] { key, value }); 114 } 115 } 116 117 protected void addAllObjectReferencesTo(Set refs) { 118 addAllObjectReferencesFromIteratorTo(this.references.keySet().iterator(), refs); 119 addAllObjectReferencesFromIteratorTo(this.references.values().iterator(), refs); 120 } 121 122 public PrettyPrinter prettyPrint(PrettyPrinter out) { 123 PrettyPrinter rv = out; 124 out = out.println("MapManagedObjectState").duplicateAndIndent(); 125 out.indent().println("references: " + references); 126 return rv; 127 } 128 129 public ManagedObjectFacade createFacade(ObjectID objectID, String className, int limit) { 130 final int size = references.size(); 131 132 if (limit < 0) { 133 limit = size; 134 } else { 135 limit = Math.min(limit, size); 136 } 137 138 MapEntryFacade[] data = new MapEntryFacade[limit]; 139 140 int index = 0; 141 142 for (Iterator i = references.entrySet().iterator(); i.hasNext() && index < limit; index++) { 143 Entry entry = (Entry) i.next(); 144 Object key = FacadeUtil.processValue(entry.getKey()); 145 Object value = FacadeUtil.processValue(entry.getValue()); 146 data[index] = new MapEntryFacadeImpl(key, value); 147 } 148 149 return LogicalManagedObjectFacade.createMapInstance(objectID, className, data, size); 150 } 151 152 protected void basicWriteTo(ObjectOutput out) throws IOException { 153 if (false) throw new IOException (); 155 } 156 157 public void setMap(Map map) { 158 if (this.references != null) { throw new AssertionError ("The references map is already set ! " + references); } 159 this.references = map; 160 } 161 162 public Map getMap() { 163 return references; 164 } 165 166 static MapManagedObjectState readFrom(ObjectInput in) throws IOException , ClassNotFoundException { 168 if (false) { 169 throw new ClassNotFoundException (); 172 } 173 return new MapManagedObjectState(in); 174 } 175 176 public byte getType() { 177 return MAP_TYPE; 178 } 179 180 protected boolean basicEquals(LogicalManagedObjectState o) { 181 MapManagedObjectState mmo = (MapManagedObjectState) o; 182 return references.equals(mmo.references); 183 } 184 } | Popular Tags |