forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollectionKey.cs
74 lines (65 loc) · 1.74 KB
/
CollectionKey.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using NHibernate.Engine;
using NHibernate.Impl;
using NHibernate.Persister.Collection;
using NHibernate.Type;
namespace NHibernate.Engine
{
/// <summary>
/// Uniquely identifies a collection instance in a particular session.
/// </summary>
[Serializable]
public sealed class CollectionKey
{
private readonly string role;
private readonly object key;
private readonly IType keyType;
[NonSerialized] private readonly ISessionFactoryImplementor factory;
private readonly int hashCode;
private readonly EntityMode entityMode;
public CollectionKey(ICollectionPersister persister, object key, EntityMode entityMode)
: this(persister.Role, key, persister.KeyType, entityMode, persister.Factory)
{
}
private CollectionKey(string role, object key, IType keyType, EntityMode entityMode, ISessionFactoryImplementor factory)
{
this.role = role;
this.key = key;
this.keyType = keyType;
this.entityMode = entityMode;
this.factory = factory;
hashCode = GenerateHashCode(); //cache the hashcode
}
public override bool Equals(object obj)
{
CollectionKey that = (CollectionKey)obj;
return that.role.Equals(role) && keyType.IsEqual(that.key, key, entityMode, factory);
}
public override int GetHashCode()
{
return hashCode;
}
private int GenerateHashCode()
{
int result = 17;
unchecked
{
result = 37 * result + role.GetHashCode();
result = 37 * result + keyType.GetHashCode(key, entityMode, factory);
}
return result;
}
public string Role
{
get { return role; }
}
public object Key
{
get { return key; }
}
public override string ToString()
{
return "CollectionKey" + MessageHelper.InfoString(factory.GetCollectionPersister(role), key, factory);
}
}
}