1 package org.hibernate; 3 4 import java.io.Serializable ; 5 import java.util.HashMap ; 6 import java.util.Map ; 7 8 import org.hibernate.type.VersionType; 9 10 16 public abstract class ReplicationMode implements Serializable { 17 private final String name; 18 private static final Map INSTANCES = new HashMap (); 19 20 public ReplicationMode(String name) { 21 this.name=name; 22 } 23 public String toString() { 24 return name; 25 } 26 public abstract boolean shouldOverwriteCurrentVersion(Object entity, Object currentVersion, Object newVersion, VersionType versionType); 27 30 public static final ReplicationMode EXCEPTION = new ReplicationMode("EXCEPTION") { 31 public boolean shouldOverwriteCurrentVersion(Object entity, Object currentVersion, Object newVersion, VersionType versionType) { 32 throw new AssertionFailure("should not be called"); 33 } 34 }; 35 38 public static final ReplicationMode IGNORE = new ReplicationMode("IGNORE") { 39 public boolean shouldOverwriteCurrentVersion(Object entity, Object currentVersion, Object newVersion, VersionType versionType) { 40 return false; 41 } 42 }; 43 46 public static final ReplicationMode OVERWRITE = new ReplicationMode("OVERWRITE") { 47 public boolean shouldOverwriteCurrentVersion(Object entity, Object currentVersion, Object newVersion, VersionType versionType) { 48 return true; 49 } 50 }; 51 54 public static final ReplicationMode LATEST_VERSION = new ReplicationMode("LATEST_VERSION") { 55 public boolean shouldOverwriteCurrentVersion(Object entity, Object currentVersion, Object newVersion, VersionType versionType) { 56 if (versionType==null) return true; return versionType.getComparator().compare(currentVersion, newVersion) <= 0; 58 } 59 }; 60 61 static { 62 INSTANCES.put( LATEST_VERSION.name, LATEST_VERSION ); 63 INSTANCES.put( IGNORE.name, IGNORE ); 64 INSTANCES.put( OVERWRITE.name, OVERWRITE ); 65 INSTANCES.put( EXCEPTION.name, EXCEPTION ); 66 } 67 68 private Object readResolve() { 69 return INSTANCES.get(name); 70 } 71 72 } 73 74 75 76 77 78 79 | Popular Tags |