1 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 ; 17 import java.io.ObjectInput ; 18 import java.io.ObjectOutput ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 import java.util.Set ; 23 import java.util.Map.Entry; 24 25 28 public class TreeMapManagedObjectState extends MapManagedObjectState implements PrettyPrintable { 29 static final String COMPARATOR_FIELDNAME = "java.util.TreeMap.comparator"; 30 private ObjectID comparator = null; 31 32 TreeMapManagedObjectState(long classID) { 33 super(classID, new HashMap ()); 34 } 35 36 protected TreeMapManagedObjectState(ObjectInput in) throws IOException { 37 super(in); 38 } 39 40 public void apply(ObjectID objectID, DNACursor cursor, BackReferences includeIDs) throws IOException { 41 while (cursor.next()) { 42 Object 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 [] 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 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 className, int limit) { 79 return super.createFacade(objectID,className, limit); 81 } 82 83 public byte getType() { 84 return TREE_MAP_TYPE; 85 } 86 87 protected void basicWriteTo(ObjectOutput out) throws IOException { 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 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 in) throws IOException , ClassNotFoundException { 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 map = new HashMap (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 |