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.LogicalManagedObjectFacade; 12 import com.tc.objectserver.mgmt.ManagedObjectFacade; 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.Iterator ; 19 import java.util.Set ; 20 21 24 public class ListManagedObjectState extends LogicalManagedObjectState { 25 private ArrayList references; 26 27 ListManagedObjectState(ObjectInput in) throws IOException { 28 super(in); 29 } 30 31 protected ListManagedObjectState(long classID) { 32 super(classID); 33 references = new ArrayList (1); 34 } 35 36 public void apply(ObjectID objectID, DNACursor cursor, BackReferences includeIDs) throws IOException { 37 while (cursor.next()) { 38 LogicalAction action = cursor.getLogicalAction(); 39 int method = action.getMethod(); 40 Object [] params = action.getParameters(); 41 42 switch (method) { 43 case SerializationUtil.ADD: 44 case SerializationUtil.ADD_LAST: 45 addChangeToCollector(objectID, params[0], includeIDs); 46 references.add(params[0]); 47 break; 48 case SerializationUtil.ADD_FIRST: 49 addChangeToCollector(objectID, params[0], includeIDs); 50 references.add(0, params[0]); 51 break; 52 case SerializationUtil.INSERT_AT: 53 case SerializationUtil.ADD_AT: 54 addChangeToCollector(objectID, params[1], includeIDs); 55 int ai = Math.min(((Integer ) params[0]).intValue(), references.size()); 56 if (references.size() < ai) { 57 references.add(params[1]); 58 } else { 59 references.add(ai, params[1]); 60 } 61 break; 62 case SerializationUtil.REMOVE: 63 references.remove(params[0]); 64 break; 65 case SerializationUtil.REMOVE_AT: 66 int index = ((Integer ) params[0]).intValue(); 67 if (references.size() > index) { 68 references.remove(index); 69 } 70 break; 71 case SerializationUtil.REMOVE_RANGE: { 72 int size = references.size(); 73 int fromIndex = ((Integer ) params[0]).intValue(); 74 int toIndex = ((Integer ) params[1]).intValue(); 75 int removeIndex = fromIndex; 76 if (size > fromIndex && size >= toIndex) { 77 while (fromIndex++ < toIndex) { 78 references.remove(removeIndex); 79 } 80 } 81 } 82 break; 83 case SerializationUtil.CLEAR: 84 references.clear(); 85 break; 86 case SerializationUtil.SET_ELEMENT: 87 case SerializationUtil.SET: 88 addChangeToCollector(objectID, params[1], includeIDs); 89 int si = Math.min(((Integer ) params[0]).intValue(), references.size()); 90 if (references.size() <= si) { 91 references.add(params[1]); 92 } else { 93 references.set(si, params[1]); 94 } 95 break; 96 case SerializationUtil.REMOVE_FIRST: 97 if (references.size() > 0) { 98 references.remove(0); 99 } 100 break; 101 case SerializationUtil.REMOVE_LAST: 102 int size = references.size(); 103 if (size > 0) { 104 references.remove(size - 1); 105 } 106 break; 107 case SerializationUtil.SET_SIZE: 108 int setSize = ((Integer ) params[0]).intValue(); 109 int listSize = references.size(); 110 111 if (listSize < setSize) { 112 for (int i = listSize; i < setSize; i++) { 113 references.add(ObjectID.NULL_ID); 114 } 115 } else if (listSize > setSize) { 116 for (int i = listSize; i != setSize; i--) { 117 references.remove(i - 1); 118 } 119 } 120 break; 121 case SerializationUtil.TRIM_TO_SIZE: 122 break; 124 default: 125 throw new AssertionError ("Invalid method:" + method + " state:" + this); 126 } 127 } 128 } 129 130 private void addChangeToCollector(ObjectID objectID, Object newValue, BackReferences includeIDs) { 131 if (newValue instanceof ObjectID) { 132 getListener().changed(objectID, null, (ObjectID) newValue); 133 includeIDs.addBackReference((ObjectID) newValue, objectID); 134 } 135 } 136 137 protected void addAllObjectReferencesTo(Set refs) { 138 addAllObjectReferencesFromIteratorTo(references.iterator(), refs); 139 } 140 141 public void dehydrate(ObjectID objectID, DNAWriter writer) { 142 for (Iterator i = references.iterator(); i.hasNext();) { 143 Object value = i.next(); 144 writer.addLogicalAction(SerializationUtil.ADD, new Object [] { value }); 145 } 146 } 147 148 public String toString() { 149 return "ListManagedStateObject(" + references + ")"; 150 } 151 152 public ManagedObjectFacade createFacade(ObjectID objectID, String className, int limit) { 153 final int size = references.size(); 154 155 if (limit < 0) { 156 limit = size; 157 } else { 158 limit = Math.min(limit, size); 159 } 160 161 Object [] data = new Object [limit]; 162 163 int index = 0; 164 for (Iterator iter = references.iterator(); iter.hasNext() && index < limit; index++) { 165 data[index] = iter.next(); 166 } 167 168 return LogicalManagedObjectFacade.createListInstance(objectID, className, data, size); 169 } 170 171 public byte getType() { 172 return LIST_TYPE; 173 } 174 175 protected void basicWriteTo(ObjectOutput out) throws IOException { 176 out.writeInt(references.size()); 177 for (Iterator i = references.iterator(); i.hasNext();) { 178 out.writeObject(i.next()); 179 } 180 } 181 182 protected boolean basicEquals(LogicalManagedObjectState o) { 183 ListManagedObjectState mo = (ListManagedObjectState) o; 184 return references.equals(mo.references); 185 } 186 187 static ListManagedObjectState readFrom(ObjectInput in) throws IOException , ClassNotFoundException { 188 ListManagedObjectState listmo = new ListManagedObjectState(in); 189 int size = in.readInt(); 190 ArrayList list = new ArrayList (size); 191 for (int i = 0; i < size; i++) { 192 list.add(in.readObject()); 193 } 194 listmo.references = list; 195 return listmo; 196 } 197 198 } | Popular Tags |