KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > cache > CacheKey


1 //$Id: CacheKey.java,v 1.5 2005/05/11 22:11:53 oneovthafew Exp $
2
package org.hibernate.cache;
3
4 import java.io.Serializable JavaDoc;
5
6 import org.hibernate.EntityMode;
7 import org.hibernate.engine.SessionFactoryImplementor;
8 import org.hibernate.type.Type;
9
10 /**
11  * Allows multiple entity classes / collection roles to be
12  * stored in the same cache region. Also allows for composite
13  * keys which do not properly implement equals()/hashCode().
14  *
15  * @author Gavin King
16  */

17 public class CacheKey implements Serializable JavaDoc {
18     private final Serializable JavaDoc key;
19     private final Type type;
20     private final String JavaDoc entityOrRoleName;
21     private final EntityMode entityMode;
22     private final int hashCode;
23     
24     /**
25      * Construct a new key for a collection or entity instance.
26      * Note that an entity name should always be the root entity
27      * name, not a subclass entity name.
28      */

29     public CacheKey(
30             final Serializable JavaDoc id,
31             final Type type,
32             final String JavaDoc entityOrRoleName,
33             final EntityMode entityMode,
34             final SessionFactoryImplementor factory
35     ) {
36         this.key = id;
37         this.type = type;
38         this.entityOrRoleName = entityOrRoleName;
39         this.entityMode = entityMode;
40         hashCode = type.getHashCode(key, entityMode, factory);
41     }
42
43     //Mainly for OSCache
44
public String JavaDoc toString() {
45         return entityOrRoleName + '#' + key.toString();//"CacheKey#" + type.toString(key, sf);
46
}
47
48     public boolean equals(Object JavaDoc other) {
49         if ( !(other instanceof CacheKey) ) return false;
50         CacheKey that = (CacheKey) other;
51         return type.isEqual(key, that.key, entityMode) &&
52             entityOrRoleName.equals(that.entityOrRoleName);
53     }
54
55     public int hashCode() {
56         return hashCode;
57     }
58     
59     public Serializable JavaDoc getKey() {
60         return key;
61     }
62     
63     public String JavaDoc getEntityOrRoleName() {
64         return entityOrRoleName;
65     }
66
67 }
68
Popular Tags