01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.objectserver.managedobject;
05:
06: import com.tc.object.ObjectID;
07:
08: import java.io.IOException;
09: import java.io.ObjectInput;
10: import java.io.ObjectOutput;
11: import java.util.Collection;
12: import java.util.Collections;
13: import java.util.HashSet;
14: import java.util.Iterator;
15: import java.util.Set;
16:
17: public abstract class LogicalManagedObjectState extends
18: AbstractManagedObjectState {
19:
20: private final long classID;
21:
22: public LogicalManagedObjectState(long classID) {
23: this .classID = classID;
24: }
25:
26: protected LogicalManagedObjectState(ObjectInput in)
27: throws IOException {
28: this .classID = in.readLong();
29: }
30:
31: protected abstract void addAllObjectReferencesTo(Set refs);
32:
33: protected final void addAllObjectReferencesFromIteratorTo(
34: Iterator i, Set refs) {
35: for (; i.hasNext();) {
36: Object o = i.next();
37: if (o instanceof ObjectID) {
38: refs.add(o);
39: }
40: }
41: }
42:
43: public final Set getObjectReferences() {
44: HashSet refs = new HashSet();
45: addAllObjectReferencesTo(refs);
46: return refs;
47: }
48:
49: protected Set getObjectReferencesFrom(Collection refs) {
50: if (refs == null || refs.size() == 0) {
51: return Collections.EMPTY_SET;
52: }
53: Set results = new HashSet(refs.size());
54: for (Iterator i = refs.iterator(); i.hasNext();) {
55: Object o = i.next();
56: if (o instanceof ObjectID) {
57: results.add(o);
58: }
59: }
60: return results;
61: }
62:
63: // XXX:: This default behavior needs to be overridden by class that needs specific behavior (like MapManagedObjectState)
64: public void addObjectReferencesTo(ManagedObjectTraverser traverser) {
65: traverser.addRequiredObjectIDs(getObjectReferences());
66: }
67:
68: public final void writeTo(ObjectOutput out) throws IOException {
69: out.writeLong(classID);
70: basicWriteTo(out);
71: }
72:
73: protected abstract void basicWriteTo(ObjectOutput out)
74: throws IOException;
75:
76: public final String getClassName() {
77: return getStateFactory().getClassName(classID);
78: }
79:
80: public final String getLoaderDescription() {
81: return getStateFactory().getLoaderDescription(classID);
82: }
83:
84: protected final boolean basicEquals(AbstractManagedObjectState o) {
85: LogicalManagedObjectState lmo = ((LogicalManagedObjectState) o);
86: return lmo.classID == classID && basicEquals(lmo);
87: }
88:
89: protected abstract boolean basicEquals(LogicalManagedObjectState o);
90: }
|