~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to src/org/hibernate/engine/CollectionKey.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//$Id: CollectionKey.java 9194 2006-02-01 19:59:07Z steveebersole $
 
2
package org.hibernate.engine;
 
3
 
 
4
import org.hibernate.EntityMode;
 
5
import org.hibernate.persister.collection.CollectionPersister;
 
6
import org.hibernate.pretty.MessageHelper;
 
7
import org.hibernate.type.Type;
 
8
 
 
9
 
 
10
 
 
11
import java.io.Serializable;
 
12
import java.io.ObjectOutputStream;
 
13
import java.io.IOException;
 
14
import java.io.ObjectInputStream;
 
15
 
 
16
/**
 
17
 * Uniquely identifies a collection instance in a particular session.
 
18
 *
 
19
 * @author Gavin King
 
20
 */
 
21
public final class CollectionKey implements Serializable {
 
22
 
 
23
        private final String role;
 
24
        private final Serializable key;
 
25
        private final Type keyType;
 
26
        private final SessionFactoryImplementor factory;
 
27
        private final int hashCode;
 
28
        private EntityMode entityMode;
 
29
 
 
30
        public CollectionKey(CollectionPersister persister, Serializable key, EntityMode em) {
 
31
                this( persister.getRole(), key, persister.getKeyType(), em, persister.getFactory() );
 
32
        }
 
33
 
 
34
        private CollectionKey(
 
35
                        String role,
 
36
                Serializable key,
 
37
                Type keyType,
 
38
                EntityMode entityMode,
 
39
                SessionFactoryImplementor factory) {
 
40
                this.role = role;
 
41
                this.key = key;
 
42
                this.keyType = keyType;
 
43
                this.entityMode = entityMode;
 
44
                this.factory = factory;
 
45
                this.hashCode = generateHashCode(); //cache the hashcode
 
46
        }
 
47
 
 
48
        public boolean equals(Object other) {
 
49
                CollectionKey that = (CollectionKey) other;
 
50
                return that.role.equals(role) &&
 
51
                       keyType.isEqual(that.key, key, entityMode, factory);
 
52
        }
 
53
 
 
54
        public int generateHashCode() {
 
55
                int result = 17;
 
56
                result = 37 * result + role.hashCode();
 
57
                result = 37 * result + keyType.getHashCode(key, entityMode, factory);
 
58
                return result;
 
59
        }
 
60
 
 
61
        public int hashCode() {
 
62
                return hashCode;
 
63
        }
 
64
 
 
65
        public String getRole() {
 
66
                return role;
 
67
        }
 
68
 
 
69
        public Serializable getKey() {
 
70
                return key;
 
71
        }
 
72
 
 
73
        public String toString() {
 
74
                return "CollectionKey" +
 
75
                       MessageHelper.collectionInfoString( factory.getCollectionPersister(role), key, factory );
 
76
        }
 
77
 
 
78
        /**
 
79
         * Custom serialization routine used during serialization of a
 
80
         * Session/PersistenceContext for increased performance.
 
81
         *
 
82
         * @param oos The stream to which we should write the serial data.
 
83
         * @throws java.io.IOException
 
84
         */
 
85
        void serialize(ObjectOutputStream oos) throws IOException {
 
86
                oos.writeObject( role );
 
87
                oos.writeObject( key );
 
88
                oos.writeObject( keyType );
 
89
                oos.writeObject( entityMode.toString() );
 
90
        }
 
91
 
 
92
        /**
 
93
         * Custom deserialization routine used during deserialization of a
 
94
         * Session/PersistenceContext for increased performance.
 
95
         *
 
96
         * @param ois The stream from which to read the entry.
 
97
         * @param session The session being deserialized.
 
98
         * @return The deserialized CollectionKey
 
99
         * @throws IOException
 
100
         * @throws ClassNotFoundException
 
101
         */
 
102
        static CollectionKey deserialize(
 
103
                        ObjectInputStream ois,
 
104
                SessionImplementor session) throws IOException, ClassNotFoundException {
 
105
                return new CollectionKey(
 
106
                                ( String ) ois.readObject(),
 
107
                        ( Serializable ) ois.readObject(),
 
108
                        ( Type ) ois.readObject(),
 
109
                        EntityMode.parse( ( String ) ois.readObject() ),
 
110
                        session.getFactory()
 
111
                );
 
112
        }
 
113
}
 
 
b'\\ No newline at end of file'