KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: Versioning.java,v 1.4 2005/02/13 11:49:58 oneovthafew Exp $
2
package org.hibernate.engine;
3
4 import org.apache.commons.logging.Log;
5 import org.apache.commons.logging.LogFactory;
6
7 import org.hibernate.HibernateException;
8 import org.hibernate.persister.entity.EntityPersister;
9 import org.hibernate.type.VersionType;
10
11 /**
12  * Utility methods for managing versions and timestamps
13  * @author Gavin King
14  */

15 public final class Versioning {
16
17     private Versioning() {}
18
19     private static final Log log = LogFactory.getLog(Versioning.class);
20
21     /**
22      * Increment the given version number
23      */

24     public static Object JavaDoc increment(Object JavaDoc version, VersionType versionType) {
25         Object JavaDoc next = versionType.next(version);
26         if ( log.isTraceEnabled() ) log.trace("Incrementing: " + version + " to " + next);
27         return next;
28     }
29
30     /**
31      * Create an initial version number
32      */

33     private static Object JavaDoc seed(VersionType versionType) {
34         Object JavaDoc seed = versionType.seed();
35         if ( log.isTraceEnabled() ) log.trace("Seeding: " + seed);
36         return seed;
37     }
38
39     /**
40      * Seed the given instance state snapshot with an initial version number
41      */

42     public static boolean seedVersion(Object JavaDoc[] fields, int versionProperty, VersionType versionType) {
43         Object JavaDoc initialVersion = fields[versionProperty];
44         if (
45             initialVersion==null ||
46             // This next bit is to allow for both unsaved-value="negative"
47
// and for "older" behavior where version number did not get
48
// seeded if it was already set in the object
49
// TODO: shift it into unsaved-value strategy
50
( (initialVersion instanceof Number JavaDoc) && ( (Number JavaDoc) initialVersion ).longValue()<0 )
51         ) {
52             fields[versionProperty] = seed(versionType);
53             return true;
54         }
55         else {
56             if ( log.isTraceEnabled() ) log.trace( "using initial version: " + initialVersion );
57             return false;
58         }
59     }
60
61     private static Object JavaDoc getVersion(Object JavaDoc[] fields, int versionProperty) {
62         return fields[versionProperty];
63     }
64
65     private static void setVersion(Object JavaDoc[] fields, Object JavaDoc version, int versionProperty) {
66         fields[versionProperty] = version;
67     }
68
69     /**
70      * Set the version number of the given instance state snapshot
71      */

72     public static void setVersion(Object JavaDoc[] fields, Object JavaDoc version, EntityPersister persister) {
73         setVersion( fields, version, persister.getVersionProperty() );
74     }
75
76     /**
77      * Get the version number of the given instance state snapshot
78      */

79     public static Object JavaDoc getVersion(Object JavaDoc[] fields, EntityPersister persister) throws HibernateException {
80         return persister.isVersioned() ? getVersion( fields, persister.getVersionProperty() ) : null;
81     }
82
83     /**
84      * Do we need to increment the version number, given the dirty properties?
85      */

86     public static boolean isVersionIncrementRequired(
87         final int[] dirtyProperties,
88         final boolean hasDirtyCollections,
89         final boolean[] propertyVersionability
90     ) {
91         if (hasDirtyCollections) return true;
92         for ( int i=0; i<dirtyProperties.length; i++) {
93             if ( propertyVersionability[ dirtyProperties[i] ] ) return true;
94         }
95         return false;
96     }
97
98     public static final int OPTIMISTIC_LOCK_NONE = -1;
99     public static final int OPTIMISTIC_LOCK_ALL = 2;
100     public static final int OPTIMISTIC_LOCK_DIRTY = 1;
101     public static final int OPTIMISTIC_LOCK_VERSION = 0;
102
103 }
104
105
106
107
108
109
110
Popular Tags