KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > engine > VersionValue


1 //$Id: VersionValue.java,v 1.1 2005/06/05 04:31:33 oneovthafew Exp $
2
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 /**
10  * A strategy for determining if a version value is an version of
11  * a new transient instance or a previously persistent transient instance.
12  * The strategy is determined by the <tt>unsaved-value</tt> attribute in
13  * the mapping file.
14  *
15  * @author Gavin King
16  */

17 public class VersionValue {
18
19     private static final Log log = LogFactory.getLog(VersionValue.class);
20
21     private final Object JavaDoc value;
22     /**
23      * Assume the transient instance is newly instantiated if the version
24      * is null, otherwise assume it is a detached instance.
25      */

26     public static final VersionValue NULL = new VersionValue() {
27         public final Boolean JavaDoc isUnsaved(Object JavaDoc version) {
28             log.trace("version unsaved-value strategy NULL");
29             return version==null ? Boolean.TRUE : Boolean.FALSE;
30         }
31         public Object JavaDoc getDefaultValue(Object JavaDoc currentValue) {
32             return null;
33         }
34         public String JavaDoc toString() {
35             return "VERSION_SAVE_NULL";
36         }
37     };
38     /**
39      * Assume the transient instance is newly instantiated if the version
40      * is null, otherwise defer to the identifier unsaved-value.
41      */

42     public static final VersionValue UNDEFINED = new VersionValue() {
43         public final Boolean JavaDoc isUnsaved(Object JavaDoc version) {
44             log.trace("version unsaved-value strategy UNDEFINED");
45             return version==null ? Boolean.TRUE : null;
46         }
47         public Object JavaDoc getDefaultValue(Object JavaDoc currentValue) {
48             return currentValue;
49         }
50         public String JavaDoc toString() {
51             return "VERSION_UNDEFINED";
52         }
53     };
54     /**
55      * Assume the transient instance is newly instantiated if the version
56      * is negative, otherwise assume it is a detached instance.
57      */

58     public static final VersionValue NEGATIVE = new VersionValue() {
59     
60         public final Boolean JavaDoc isUnsaved(Object JavaDoc version) throws MappingException {
61             log.trace("version unsaved-value strategy NEGATIVE");
62             if (version==null) return Boolean.TRUE;
63             if (version instanceof Number JavaDoc) {
64                 return ( (Number JavaDoc) 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 JavaDoc getDefaultValue(Object JavaDoc currentValue) {
71             return IdentifierGeneratorFactory.createNumber( -1l, currentValue.getClass() );
72         }
73         public String JavaDoc toString() {
74             return "VERSION_NEGATIVE";
75         }
76     };
77     
78     protected VersionValue() {
79         this.value = null;
80     }
81
82     /**
83      * Assume the transient instance is newly instantiated if
84      * its version is null or equal to <tt>value</tt>
85      * @param value value to compare to
86      */

87     public VersionValue(Object JavaDoc value) {
88         this.value = value;
89     }
90     
91     /**
92      * Does the given version belong to a new instance?
93      *
94      * @param version version to check
95      * @return true is unsaved, false is saved, null is undefined
96      */

97     public Boolean JavaDoc isUnsaved(Object JavaDoc 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 JavaDoc getDefaultValue(Object JavaDoc currentValue) {
103         return value;
104     }
105     
106     public String JavaDoc toString() {
107         return "version unsaved-value: " + value;
108     }
109 }
Popular Tags