KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: IdentifierValue.java,v 1.1 2005/06/05 04:31:33 oneovthafew Exp $
2
package org.hibernate.engine;
3
4 import java.io.Serializable JavaDoc;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8
9 /**
10  * A strategy for determining if an identifier value is an identifier 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 IdentifierValue {
18
19     private static final Log log = LogFactory.getLog(IdentifierValue.class);
20     
21     private final Serializable JavaDoc value;
22     
23     /**
24      * Always assume the transient instance is newly instantiated
25      */

26     public static final IdentifierValue ANY = new IdentifierValue() {
27         public final Boolean JavaDoc isUnsaved(Serializable JavaDoc id) {
28             log.trace("id unsaved-value strategy ANY");
29             return Boolean.TRUE;
30         }
31         public Serializable JavaDoc getDefaultValue(Serializable JavaDoc currentValue) {
32             return currentValue;
33         }
34         public String JavaDoc toString() {
35             return "SAVE_ANY";
36         }
37     };
38     
39     /**
40      * Never assume the transient instance is newly instantiated
41      */

42     public static final IdentifierValue NONE = new IdentifierValue() {
43         public final Boolean JavaDoc isUnsaved(Serializable JavaDoc id) {
44             log.trace("id unsaved-value strategy NONE");
45             return Boolean.FALSE;
46         }
47         public Serializable JavaDoc getDefaultValue(Serializable JavaDoc currentValue) {
48             return currentValue;
49         }
50         public String JavaDoc toString() {
51             return "SAVE_NONE";
52         }
53     };
54     
55     /**
56      * Assume the transient instance is newly instantiated if the identifier
57      * is null.
58      */

59     public static final IdentifierValue NULL = new IdentifierValue() {
60         public final Boolean JavaDoc isUnsaved(Serializable JavaDoc id) {
61             log.trace("id unsaved-value strategy NULL");
62             return id==null ? Boolean.TRUE : Boolean.FALSE;
63         }
64         public Serializable JavaDoc getDefaultValue(Serializable JavaDoc currentValue) {
65             return null;
66         }
67         public String JavaDoc toString() {
68             return "SAVE_NULL";
69         }
70     };
71     
72     /**
73      * Assume nothing.
74      */

75     public static final IdentifierValue UNDEFINED = new IdentifierValue() {
76         public final Boolean JavaDoc isUnsaved(Serializable JavaDoc id) {
77             log.trace("id unsaved-value strategy UNDEFINED");
78             return null;
79         }
80         public Serializable JavaDoc getDefaultValue(Serializable JavaDoc currentValue) {
81             return null;
82         }
83         public String JavaDoc toString() {
84             return "UNDEFINED";
85         }
86     };
87     
88     protected IdentifierValue() {
89         this.value = null;
90     }
91     
92     /**
93      * Assume the transient instance is newly instantiated if
94      * its identifier is null or equal to <tt>value</tt>
95      */

96     public IdentifierValue(Serializable JavaDoc value) {
97         this.value = value;
98     }
99     
100     /**
101      * Does the given identifier belong to a new instance?
102      */

103     public Boolean JavaDoc isUnsaved(Serializable JavaDoc id) {
104         if ( log.isTraceEnabled() ) log.trace("id unsaved-value: " + value);
105         return id==null || id.equals(value) ? Boolean.TRUE : Boolean.FALSE;
106     }
107     
108     public Serializable JavaDoc getDefaultValue(Serializable JavaDoc currentValue) {
109         return value;
110     }
111     
112     public String JavaDoc toString() {
113         return "identifier unsaved-value: " + value;
114     }
115 }
Popular Tags