1 package org.hibernate.engine; 3 4 import org.apache.commons.logging.Log; 5 import org.apache.commons.logging.LogFactory; 6 import org.hibernate.MappingException; 7 import org.hibernate.id.IdentifierGeneratorFactory; 8 9 17 public class VersionValue { 18 19 private static final Log log = LogFactory.getLog(VersionValue.class); 20 21 private final Object value; 22 26 public static final VersionValue NULL = new VersionValue() { 27 public final Boolean isUnsaved(Object version) { 28 log.trace("version unsaved-value strategy NULL"); 29 return version==null ? Boolean.TRUE : Boolean.FALSE; 30 } 31 public Object getDefaultValue(Object currentValue) { 32 return null; 33 } 34 public String toString() { 35 return "VERSION_SAVE_NULL"; 36 } 37 }; 38 42 public static final VersionValue UNDEFINED = new VersionValue() { 43 public final Boolean isUnsaved(Object version) { 44 log.trace("version unsaved-value strategy UNDEFINED"); 45 return version==null ? Boolean.TRUE : null; 46 } 47 public Object getDefaultValue(Object currentValue) { 48 return currentValue; 49 } 50 public String toString() { 51 return "VERSION_UNDEFINED"; 52 } 53 }; 54 58 public static final VersionValue NEGATIVE = new VersionValue() { 59 60 public final Boolean isUnsaved(Object version) throws MappingException { 61 log.trace("version unsaved-value strategy NEGATIVE"); 62 if (version==null) return Boolean.TRUE; 63 if (version instanceof Number ) { 64 return ( (Number ) version ).longValue() < 0l ? Boolean.TRUE : Boolean.FALSE; 65 } 66 else { 67 throw new MappingException("unsaved-value NEGATIVE may only be used with short, int and long types"); 68 } 69 } 70 public Object getDefaultValue(Object currentValue) { 71 return IdentifierGeneratorFactory.createNumber( -1l, currentValue.getClass() ); 72 } 73 public String toString() { 74 return "VERSION_NEGATIVE"; 75 } 76 }; 77 78 protected VersionValue() { 79 this.value = null; 80 } 81 82 87 public VersionValue(Object value) { 88 this.value = value; 89 } 90 91 97 public Boolean isUnsaved(Object version) throws MappingException { 98 if ( log.isTraceEnabled() ) log.trace("version unsaved-value: " + value); 99 return version==null || version.equals(value) ? Boolean.TRUE : Boolean.FALSE; 100 } 101 102 public Object getDefaultValue(Object currentValue) { 103 return value; 104 } 105 106 public String toString() { 107 return "version unsaved-value: " + value; 108 } 109 } | Popular Tags |