001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.objectserver.managedobject;
005:
006: import com.tc.object.ObjectID;
007: import com.tc.object.dna.api.DNACursor;
008: import com.tc.object.dna.api.DNAWriter;
009: import com.tc.object.dna.api.LogicalAction;
010: import com.tc.object.dna.api.PhysicalAction;
011: import com.tc.objectserver.mgmt.ManagedObjectFacade;
012: import com.tc.text.PrettyPrintable;
013: import com.tc.text.PrettyPrinter;
014: import com.tc.util.Assert;
015:
016: import java.io.IOException;
017: import java.io.ObjectInput;
018: import java.io.ObjectOutput;
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.Map;
022: import java.util.Set;
023: import java.util.Map.Entry;
024:
025: /**
026: * state for tree maps
027: */
028: public class TreeMapManagedObjectState extends MapManagedObjectState
029: implements PrettyPrintable {
030: static final String COMPARATOR_FIELDNAME = "java.util.TreeMap.comparator";
031: private ObjectID comparator = null;
032:
033: TreeMapManagedObjectState(long classID) {
034: super (classID, new HashMap());
035: }
036:
037: protected TreeMapManagedObjectState(ObjectInput in)
038: throws IOException {
039: super (in);
040: }
041:
042: public void apply(ObjectID objectID, DNACursor cursor,
043: BackReferences includeIDs) throws IOException {
044: while (cursor.next()) {
045: Object action = cursor.getAction();
046: if (action instanceof PhysicalAction) {
047: PhysicalAction pa = (PhysicalAction) action;
048: Assert.assertEquals(COMPARATOR_FIELDNAME, pa
049: .getFieldName());
050: this .comparator = (ObjectID) pa.getObject();
051: getListener().changed(objectID, null, comparator);
052: includeIDs.addBackReference(comparator, objectID);
053: } else {
054: LogicalAction la = (LogicalAction) action;
055: int method = la.getMethod();
056: Object[] params = la.getParameters();
057: applyMethod(objectID, includeIDs, method, params);
058: }
059: }
060: }
061:
062: public void dehydrate(ObjectID objectID, DNAWriter writer) {
063: if (comparator != null) {
064: writer.addPhysicalAction(COMPARATOR_FIELDNAME, comparator);
065: }
066: super .dehydrate(objectID, writer);
067: }
068:
069: protected void addAllObjectReferencesTo(Set refs) {
070: super .addAllObjectReferencesTo(refs);
071: if (comparator != null) {
072: refs.add(comparator);
073: }
074: }
075:
076: public PrettyPrinter prettyPrint(PrettyPrinter out) {
077: PrettyPrinter rv = out;
078: out = out.println("TreeMapManagedObjectState")
079: .duplicateAndIndent();
080: out.indent().println("references: " + references);
081: out.indent().println("comparator: " + comparator);
082: return rv;
083: }
084:
085: public ManagedObjectFacade createFacade(ObjectID objectID,
086: String className, int limit) {
087: // XXX: This facade is incomplete...it doesn't include the comparator field
088: return super .createFacade(objectID, className, limit);
089: }
090:
091: public byte getType() {
092: return TREE_MAP_TYPE;
093: }
094:
095: protected void basicWriteTo(ObjectOutput out) throws IOException {
096: if (comparator == null) {
097: out.writeBoolean(false);
098: } else {
099: out.writeBoolean(true);
100: out.writeLong(comparator.toLong());
101: }
102: out.writeInt(references.size());
103: for (Iterator i = references.entrySet().iterator(); i.hasNext();) {
104: Entry entry = (Entry) i.next();
105: out.writeObject(entry.getKey());
106: out.writeObject(entry.getValue());
107: }
108: }
109:
110: protected boolean basicEquals(LogicalManagedObjectState o) {
111: TreeMapManagedObjectState mo = (TreeMapManagedObjectState) o;
112: return (comparator == mo.comparator || (comparator != null && comparator
113: .equals(mo.comparator)))
114: && super .basicEquals(o);
115: }
116:
117: static MapManagedObjectState readFrom(ObjectInput in)
118: throws IOException, ClassNotFoundException {
119: TreeMapManagedObjectState tm = new TreeMapManagedObjectState(in);
120: ObjectID comparator;
121: if (in.readBoolean()) {
122: comparator = new ObjectID(in.readLong());
123: } else {
124: comparator = null;
125: }
126: int size = in.readInt();
127: Map map = new HashMap(size);
128: for (int i = 0; i < size; i++) {
129: map.put(in.readObject(), in.readObject());
130: }
131: tm.references = map;
132: tm.comparator = comparator;
133:
134: return tm;
135: }
136: }
|