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.dna.api.DNACursor;
009: import com.tc.object.dna.api.DNAWriter;
010: import com.tc.object.dna.api.LogicalAction;
011: import com.tc.object.dna.api.PhysicalAction;
012: import com.tc.objectserver.mgmt.ManagedObjectFacade;
013: import com.tc.util.Assert;
014:
015: import java.io.IOException;
016: import java.io.ObjectInput;
017: import java.io.ObjectOutput;
018: import java.util.Iterator;
019: import java.util.LinkedHashSet;
020: import java.util.Set;
021:
022: /**
023: * ManagedObjectState for sets.
024: */
025: public class TreeSetManagedObjectState extends SetManagedObjectState {
026: private static final String COMPARATOR_FIELDNAME = TreeMapManagedObjectState.COMPARATOR_FIELDNAME;
027:
028: private ObjectID comparator = null;
029:
030: public TreeSetManagedObjectState(long classID) {
031: super (classID);
032: }
033:
034: protected TreeSetManagedObjectState(ObjectInput in)
035: throws IOException {
036: super (in);
037: }
038:
039: public void apply(ObjectID objectID, DNACursor cursor,
040: BackReferences includeIDs) throws IOException {
041: if (!cursor.next()) {
042: return;
043: }
044:
045: Object action = cursor.getAction();
046: if (action instanceof PhysicalAction) {
047: PhysicalAction pa = (PhysicalAction) action;
048: Assert
049: .assertEquals(COMPARATOR_FIELDNAME, pa
050: .getFieldName());
051: this .comparator = (ObjectID) pa.getObject();
052: getListener().changed(objectID, null, comparator);
053: includeIDs.addBackReference(comparator, objectID);
054: } else {
055: LogicalAction la = (LogicalAction) action;
056: int method = la.getMethod();
057: Object[] params = la.getParameters();
058: super .apply(objectID, method, params, includeIDs);
059: }
060:
061: while (cursor.next()) {
062: LogicalAction la = cursor.getLogicalAction();
063: int method = la.getMethod();
064: Object[] params = la.getParameters();
065: super .apply(objectID, method, params, includeIDs);
066: }
067: }
068:
069: public void dehydrate(ObjectID objectID, DNAWriter writer) {
070: if (comparator != null) {
071: writer.addPhysicalAction(COMPARATOR_FIELDNAME, comparator);
072: }
073: super .dehydrate(objectID, writer);
074: }
075:
076: protected void addAllObjectReferencesTo(Set refs) {
077: super .addAllObjectReferencesTo(refs);
078: if (comparator != null) {
079: refs.add(comparator);
080: }
081: }
082:
083: // TODO This Facade has not included the comparator.
084: public ManagedObjectFacade createFacade(ObjectID objectID,
085: String className, int limit) {
086: return super .createFacade(objectID, className, limit);
087: }
088:
089: protected void basicWriteTo(ObjectOutput out) throws IOException {
090: if (comparator == null) {
091: out.writeBoolean(false);
092: } else {
093: out.writeBoolean(true);
094: out.writeLong(comparator.toLong());
095: }
096: out.writeInt(references.size());
097: for (Iterator i = references.iterator(); i.hasNext();) {
098: out.writeObject(i.next());
099: }
100: }
101:
102: protected boolean basicEquals(Object other) {
103: TreeSetManagedObjectState mo = (TreeSetManagedObjectState) other;
104: return (comparator == mo.comparator || (comparator != null && comparator
105: .equals(mo.comparator)))
106: && references.equals(mo.references);
107: }
108:
109: static SetManagedObjectState readFrom(ObjectInput in)
110: throws IOException, ClassNotFoundException {
111: TreeSetManagedObjectState tsm = new TreeSetManagedObjectState(
112: in);
113: ObjectID comparator;
114: if (in.readBoolean()) {
115: comparator = new ObjectID(in.readLong());
116: } else {
117: comparator = null;
118: }
119: int size = in.readInt();
120: Set set = new LinkedHashSet(size, 0.75f);
121: for (int i = 0; i < size; i++) {
122: set.add(in.readObject());
123: }
124: tsm.comparator = comparator;
125: tsm.references = set;
126: return tsm;
127: }
128:
129: public byte getType() {
130: return TREE_SET_TYPE;
131: }
132: }
|