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.util.Assert; 13 14 import java.io.IOException ; 15 import java.io.ObjectInput ; 16 import java.io.ObjectOutput ; 17 import java.util.ArrayList ; 18 import java.util.Collection ; 19 import java.util.Iterator ; 20 import java.util.LinkedHashSet ; 21 import java.util.List ; 22 import java.util.Set ; 23 24 27 public class TreeSetManagedObjectState extends SetManagedObjectState { 28 private static final String COMPARATOR_FIELDNAME = TreeMapManagedObjectState.COMPARATOR_FIELDNAME; 29 30 private ObjectID comparator = null; 31 32 public TreeSetManagedObjectState(long classID) { 33 super(classID); 34 } 35 36 protected TreeSetManagedObjectState(ObjectInput in) throws IOException { 37 super(in); 38 } 39 40 public void apply(ObjectID objectID, DNACursor cursor, BackReferences includeIDs) throws IOException { 41 if (!cursor.next()) { return; } 42 43 Object action = cursor.getAction(); 44 if (action instanceof PhysicalAction) { 45 PhysicalAction pa = (PhysicalAction) action; 46 Assert.assertEquals(COMPARATOR_FIELDNAME, pa.getFieldName()); 47 this.comparator = (ObjectID) pa.getObject(); 48 } else { 49 LogicalAction la = (LogicalAction) action; 50 int method = la.getMethod(); 51 Object [] params = la.getParameters(); 52 super.apply(objectID, method, params, includeIDs); 53 } 54 55 while (cursor.next()) { 56 LogicalAction la = cursor.getLogicalAction(); 57 int method = la.getMethod(); 58 Object [] params = la.getParameters(); 59 super.apply(objectID, method, params, includeIDs); 60 } 61 } 62 63 public void dehydrate(ObjectID objectID, DNAWriter writer) { 64 if (comparator != null) { 65 writer.addPhysicalAction(COMPARATOR_FIELDNAME, comparator); 66 } 67 super.dehydrate(objectID, writer); 68 } 69 70 protected Collection getAllReferences() { 71 List allReferences = new ArrayList (references); 72 if (comparator != null) { 73 allReferences.add(comparator); 74 } 75 return allReferences; 76 } 77 78 public ManagedObjectFacade createFacade(ObjectID objectID, String className, int limit) { 80 return super.createFacade(objectID, className, limit); 81 } 82 83 protected void basicWriteTo(ObjectOutput out) throws IOException { 84 if (comparator == null) { 85 out.writeBoolean(false); 86 } else { 87 out.writeBoolean(true); 88 out.writeLong(comparator.toLong()); 89 } 90 out.writeInt(references.size()); 91 for (Iterator i = references.iterator(); i.hasNext();) { 92 out.writeObject(i.next()); 93 } 94 } 95 96 protected boolean basicEquals(Object other) { 97 TreeSetManagedObjectState mo = (TreeSetManagedObjectState) other; 98 return (comparator == mo.comparator || (comparator != null && comparator.equals(mo.comparator))) 99 && references.equals(mo.references); 100 } 101 102 static SetManagedObjectState readFrom(ObjectInput in) throws IOException , ClassNotFoundException { 103 TreeSetManagedObjectState tsm = new TreeSetManagedObjectState(in); 104 ObjectID comparator; 105 if (in.readBoolean()) { 106 comparator = new ObjectID(in.readLong()); 107 } else { 108 comparator = null; 109 } 110 int size = in.readInt(); 111 Set set = new LinkedHashSet (size, 0.75f); 112 for (int i = 0; i < size; i++) { 113 set.add(in.readObject()); 114 } 115 tsm.comparator = comparator; 116 tsm.references = set; 117 return tsm; 118 } 119 120 public byte getType() { 121 return TREE_SET_TYPE; 122 } 123 } 124 | Popular Tags |