1 5 package com.tc.objectserver.managedobject; 6 7 import com.tc.object.ObjectID; 8 import com.tc.object.SerializationUtil; 9 import com.tc.object.dna.api.DNACursor; 10 import com.tc.object.dna.api.DNAWriter; 11 import com.tc.object.dna.api.LogicalAction; 12 import com.tc.objectserver.mgmt.LogicalManagedObjectFacade; 13 import com.tc.objectserver.mgmt.ManagedObjectFacade; 14 15 import java.io.IOException ; 16 import java.io.ObjectInput ; 17 import java.io.ObjectOutput ; 18 import java.util.Arrays ; 19 import java.util.Iterator ; 20 import java.util.LinkedHashSet ; 21 import java.util.Set ; 22 23 26 public class SetManagedObjectState extends LogicalManagedObjectState { 27 protected Set references; 28 29 SetManagedObjectState(long classID) { 30 super(classID); 31 this.references = new LinkedHashSet (1, 0.75f); } 33 34 protected SetManagedObjectState(ObjectInput in) throws IOException { 35 super(in); 36 } 37 38 public void apply(ObjectID objectID, DNACursor cursor, BackReferences includeIDs) throws IOException { 39 while (cursor.next()) { 40 LogicalAction action = cursor.getLogicalAction(); 41 int method = action.getMethod(); 42 Object [] params = action.getParameters(); 43 apply(objectID, method, params, includeIDs); 44 } 45 } 46 47 protected void apply(ObjectID objectID, int method, Object [] params, BackReferences includeIDs) { 48 switch (method) { 49 case SerializationUtil.ADD: 50 Object v = getValue(params); 51 addChangeToCollector(objectID, v, includeIDs); 52 references.add(v); 53 break; 54 case SerializationUtil.REMOVE: 55 references.remove(params[0]); 56 break; 57 case SerializationUtil.REMOVE_ALL: 58 references.removeAll(Arrays.asList(params)); 59 break; 60 case SerializationUtil.CLEAR: 61 references.clear(); 62 break; 63 default: 64 throw new AssertionError ("Invalid action:" + method); 65 } 66 } 67 68 private Object getValue(Object [] params) { 69 return params.length == 2 ? params[1] : params[0]; 71 } 72 73 private void addChangeToCollector(ObjectID objectID, Object newValue, BackReferences includeIDs) { 74 if (newValue instanceof ObjectID) { 75 getListener().changed(objectID, null, (ObjectID) newValue); 76 includeIDs.addBackReference((ObjectID) newValue, objectID); 77 } 78 } 79 80 public void dehydrate(ObjectID objectID, DNAWriter writer) { 81 for (Iterator i = references.iterator(); i.hasNext();) { 82 Object value = i.next(); 83 writer.addLogicalAction(SerializationUtil.ADD, new Object [] { value }); 84 } 85 } 86 87 protected void addAllObjectReferencesTo(Set refs) { 88 addAllObjectReferencesFromIteratorTo(this.references.iterator(), refs); 89 } 90 91 public ManagedObjectFacade createFacade(ObjectID objectID, String className, int limit) { 92 final int size = references.size(); 93 94 if (limit < 0) { 95 limit = size; 96 } else { 97 limit = Math.min(limit, size); 98 } 99 100 Object [] data = new Object [limit]; 101 102 int index = 0; 103 for (Iterator iter = references.iterator(); iter.hasNext() && index < limit; index++) { 104 data[index] = iter.next(); 105 } 106 107 return LogicalManagedObjectFacade.createSetInstance(objectID, className, data, size); 108 } 109 110 public byte getType() { 111 return SET_TYPE; 112 } 113 114 protected void basicWriteTo(ObjectOutput out) throws IOException { 115 out.writeInt(references.size()); 116 for (Iterator i = references.iterator(); i.hasNext();) { 117 out.writeObject(i.next()); 118 } 119 } 120 121 protected boolean basicEquals(LogicalManagedObjectState o) { 122 SetManagedObjectState mo = (SetManagedObjectState) o; 123 return references.equals(mo.references); 124 } 125 126 static SetManagedObjectState readFrom(ObjectInput in) throws IOException , ClassNotFoundException { 127 SetManagedObjectState setmo = new SetManagedObjectState(in); 128 int size = in.readInt(); 129 Set set = new LinkedHashSet (size, 0.75f); 130 for (int i = 0; i < size; i++) { 131 set.add(in.readObject()); 132 } 133 setmo.references = set; 134 return setmo; 135 } 136 } 137 | Popular Tags |