1 4 package com.tc.object.applicator; 5 6 import com.tc.logging.TCLogger; 7 import com.tc.logging.TCLogging; 8 import com.tc.object.ClientObjectManager; 9 import com.tc.object.LiteralValues; 10 import com.tc.object.ObjectID; 11 import com.tc.object.TCObject; 12 import com.tc.object.dna.impl.DNAEncoding; 13 import com.tc.util.Assert; 14 15 import java.util.Map ; 16 17 public abstract class BaseApplicator implements ChangeApplicator { 18 19 private static final TCLogger logger = TCLogging.getLogger(BaseApplicator.class); 20 private static final LiteralValues literals = new LiteralValues(); 21 22 protected final DNAEncoding encoding; 23 24 protected BaseApplicator(DNAEncoding encoding) { 25 this.encoding = encoding; 26 } 27 28 protected final Object getDehydratableObject(Object pojo, ClientObjectManager objectManager) { 29 if (pojo == null) { 30 return ObjectID.NULL_ID; 31 } else if (literals.isLiteralInstance(pojo)) { 32 return pojo; 33 } else { 34 TCObject tcObject = objectManager.lookupExistingOrNull(pojo); 35 if (tcObject == null) { 36 logger 39 .warn("Not dehydrating object of type " + pojo.getClass().getName() + "@" + System.identityHashCode(pojo)); 40 return null; 41 } 42 return tcObject.getObjectID(); 43 } 44 } 45 46 protected final boolean isLiteralInstance(Object pojo) { 47 return literals.isLiteralInstance(pojo); 48 } 49 50 protected boolean isPortableReference(Class c) { 51 return !literals.isLiteral(c.getName()); 52 } 53 54 protected Object createParentIfNecessary(Map visited, ClientObjectManager objectManager, Map cloned, Object v) { 55 return objectManager.createParentCopyInstanceIfNecessary(visited, cloned, v); 56 } 57 58 protected Object createCopyIfNecessary(ClientObjectManager objectManager, Map visited, Map cloned, Object originalValue) { 59 Object copyKey; 60 if (originalValue == null || isLiteralInstance(originalValue)) { 61 copyKey = originalValue; 62 } else if (visited.containsKey(originalValue)) { 63 Assert.eval(visited.get(originalValue) != null); 64 copyKey = visited.get(originalValue); 65 } else { 66 Assert.eval(!isLiteralInstance(originalValue)); 67 copyKey = objectManager.createNewCopyInstance(originalValue, createParentIfNecessary(visited, objectManager, cloned, originalValue)); 68 69 visited.put(originalValue, copyKey); 70 cloned.put(originalValue, copyKey); 71 } 72 return copyKey; 73 } 74 75 } 76 | Popular Tags |