001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.objectserver.managedobject;
006:
007: import com.tc.object.ObjectID;
008: import com.tc.object.SerializationUtil;
009: import com.tc.object.dna.api.DNACursor;
010: import com.tc.object.dna.api.DNAWriter;
011: import com.tc.object.dna.api.LogicalAction;
012: import com.tc.objectserver.mgmt.LogicalManagedObjectFacade;
013: import com.tc.objectserver.mgmt.ManagedObjectFacade;
014:
015: import java.io.IOException;
016: import java.io.ObjectInput;
017: import java.io.ObjectOutput;
018: import java.util.Arrays;
019: import java.util.Iterator;
020: import java.util.LinkedHashSet;
021: import java.util.Set;
022:
023: /**
024: * ManagedObjectState for sets.
025: */
026: public class SetManagedObjectState extends LogicalManagedObjectState {
027: protected Set references;
028:
029: SetManagedObjectState(long classID) {
030: super (classID);
031: this .references = new LinkedHashSet(1, 0.75f); // Accommodating LinkedHashSet.
032: }
033:
034: protected SetManagedObjectState(ObjectInput in) throws IOException {
035: super (in);
036: }
037:
038: public void apply(ObjectID objectID, DNACursor cursor,
039: BackReferences includeIDs) throws IOException {
040: while (cursor.next()) {
041: LogicalAction action = cursor.getLogicalAction();
042: int method = action.getMethod();
043: Object[] params = action.getParameters();
044: apply(objectID, method, params, includeIDs);
045: }
046: }
047:
048: protected void apply(ObjectID objectID, int method,
049: Object[] params, BackReferences includeIDs) {
050: switch (method) {
051: case SerializationUtil.ADD:
052: Object v = getValue(params);
053: addChangeToCollector(objectID, v, includeIDs);
054: references.add(v);
055: break;
056: case SerializationUtil.REMOVE:
057: references.remove(params[0]);
058: break;
059: case SerializationUtil.REMOVE_ALL:
060: references.removeAll(Arrays.asList(params));
061: break;
062: case SerializationUtil.CLEAR:
063: references.clear();
064: break;
065: default:
066: throw new AssertionError("Invalid action:" + method);
067: }
068: }
069:
070: private Object getValue(Object[] params) {
071: // hack for trove sets which replace the old set value (java ones do the opposite) clean this up
072: return params.length == 2 ? params[1] : params[0];
073: }
074:
075: private void addChangeToCollector(ObjectID objectID,
076: Object newValue, BackReferences includeIDs) {
077: if (newValue instanceof ObjectID) {
078: getListener().changed(objectID, null, (ObjectID) newValue);
079: includeIDs.addBackReference((ObjectID) newValue, objectID);
080: }
081: }
082:
083: public void dehydrate(ObjectID objectID, DNAWriter writer) {
084: for (Iterator i = references.iterator(); i.hasNext();) {
085: Object value = i.next();
086: writer.addLogicalAction(SerializationUtil.ADD,
087: new Object[] { value });
088: }
089: }
090:
091: protected void addAllObjectReferencesTo(Set refs) {
092: addAllObjectReferencesFromIteratorTo(
093: this .references.iterator(), refs);
094: }
095:
096: public ManagedObjectFacade createFacade(ObjectID objectID,
097: String className, int limit) {
098: final int size = references.size();
099:
100: if (limit < 0) {
101: limit = size;
102: } else {
103: limit = Math.min(limit, size);
104: }
105:
106: Object[] data = new Object[limit];
107:
108: int index = 0;
109: for (Iterator iter = references.iterator(); iter.hasNext()
110: && index < limit; index++) {
111: data[index] = iter.next();
112: }
113:
114: return LogicalManagedObjectFacade.createSetInstance(objectID,
115: className, data, size);
116: }
117:
118: public byte getType() {
119: return SET_TYPE;
120: }
121:
122: protected void basicWriteTo(ObjectOutput out) throws IOException {
123: out.writeInt(references.size());
124: for (Iterator i = references.iterator(); i.hasNext();) {
125: out.writeObject(i.next());
126: }
127: }
128:
129: protected boolean basicEquals(LogicalManagedObjectState o) {
130: SetManagedObjectState mo = (SetManagedObjectState) o;
131: return references.equals(mo.references);
132: }
133:
134: static SetManagedObjectState readFrom(ObjectInput in)
135: throws IOException, ClassNotFoundException {
136: SetManagedObjectState setmo = new SetManagedObjectState(in);
137: int size = in.readInt();
138: Set set = new LinkedHashSet(size, 0.75f);
139: for (int i = 0; i < size; i++) {
140: set.add(in.readObject());
141: }
142: setmo.references = set;
143: return setmo;
144: }
145: }
|