1
1
using System ;
2
2
using System . Collections ;
3
+ using System . Collections . Generic ;
3
4
using NHibernate . Type ;
4
- using NHibernate . Util ;
5
5
6
6
namespace NHibernate . Engine
7
7
{
8
- /// <summary></summary>
8
+ /// <summary> An ordered pair of a value and its Hibernate type. </summary>
9
9
[ Serializable ]
10
10
public sealed class TypedValue
11
11
{
12
- private IType type ;
13
- private object value ;
14
-
15
- /// <summary>
16
- ///
17
- /// </summary>
18
- /// <param name="type"></param>
19
- /// <param name="value"></param>
20
- public TypedValue ( IType type , object value )
12
+ // Because NH-875 we have a different implementation
13
+ // The DefaultComparer is the comparrer used in H3.2.5
14
+ // The ParameterListComparer is the comparer introduced in NH to fix NH-845
15
+
16
+ private readonly IType type ;
17
+ private readonly object value ;
18
+ private readonly IEqualityComparer < TypedValue > comparer ;
19
+
20
+ public TypedValue ( IType type , object value , EntityMode entityMode )
21
21
{
22
22
this . type = type ;
23
23
this . value = value ;
24
+ ICollection values = value as ICollection ;
25
+ if ( ! type . IsCollectionType && values != null )
26
+ comparer = new ParameterListComparer ( entityMode ) ;
27
+ else
28
+ comparer = new DefaultComparer ( entityMode ) ;
24
29
}
25
30
26
- /// <summary></summary>
27
31
public object Value
28
32
{
29
33
get { return value ; }
30
- set { this . value = value ; }
31
34
}
32
35
33
- /// <summary></summary>
34
36
public IType Type
35
37
{
36
38
get { return type ; }
37
- set { type = value ; }
38
39
}
39
40
40
41
public override int GetHashCode ( )
41
42
{
42
- unchecked
43
- {
44
- int result = 17 ;
45
- result = 37 * result + type . GetHashCode ( ) ;
46
- result = 37 * result + ( value == null ? 0 : value . GetHashCode ( ) ) ;
47
- return result ;
48
- }
43
+ return comparer . GetHashCode ( this ) ;
49
44
}
50
45
51
46
public override bool Equals ( object obj )
52
47
{
53
- TypedValue that = obj as TypedValue ;
54
- if ( that == null )
48
+ return comparer . Equals ( this , obj as TypedValue ) ;
49
+ }
50
+
51
+ public override string ToString ( )
52
+ {
53
+ return value == null ? "null" : value . ToString ( ) ;
54
+ }
55
+
56
+ [ Serializable ]
57
+ private class ParameterListComparer : IEqualityComparer < TypedValue >
58
+ {
59
+ private readonly EntityMode entityMode ;
60
+
61
+ public ParameterListComparer ( EntityMode entityMode )
55
62
{
56
- return false ;
63
+ this . entityMode = entityMode ;
57
64
}
58
65
59
- if ( ! that . type . Equals ( type ) )
66
+ public bool Equals ( TypedValue x , TypedValue y )
60
67
{
61
- return false ;
68
+ if ( y == null ) return false ;
69
+ if ( ! x . type . ReturnedClass . Equals ( y . type . ReturnedClass ) )
70
+ return false ;
71
+ return IsEquals ( x . type , x . value as ICollection , y . value as ICollection ) ;
62
72
}
63
73
64
- if ( value is ICollection && that . value is ICollection )
74
+ public int GetHashCode ( TypedValue obj )
65
75
{
66
- return CollectionHelper . CollectionEquals ( ( ICollection ) value , ( ICollection ) that . value ) ;
76
+ return GetHashCode ( obj . type , obj . value as ICollection ) ;
67
77
}
68
- else
78
+
79
+ private int GetHashCode ( IType type , ICollection values )
80
+ {
81
+ if ( values == null )
82
+ return 0 ;
83
+
84
+ unchecked
85
+ {
86
+ int result = 0 ;
87
+
88
+ foreach ( object obj in values )
89
+ result += obj == null ? 0 : type . GetHashCode ( obj , entityMode ) ;
90
+
91
+ return result ;
92
+ }
93
+ }
94
+
95
+ private bool IsEquals ( IType type , ICollection x , ICollection y )
69
96
{
70
- return Equals ( that . value , value ) ;
97
+ if ( x == y )
98
+ return true ;
99
+
100
+ if ( x == null || y == null )
101
+ return false ;
102
+
103
+ if ( x . Count != y . Count )
104
+ return false ;
105
+
106
+ IEnumerator xe = x . GetEnumerator ( ) ;
107
+ IEnumerator ye = y . GetEnumerator ( ) ;
108
+
109
+ while ( xe . MoveNext ( ) )
110
+ {
111
+ ye . MoveNext ( ) ;
112
+ if ( ! type . IsEqual ( xe . Current , ye . Current , entityMode ) )
113
+ return false ;
114
+ }
115
+
116
+ return true ;
71
117
}
72
118
}
73
119
74
- public override string ToString ( )
120
+ [ Serializable ]
121
+ private class DefaultComparer : IEqualityComparer < TypedValue >
75
122
{
76
- if ( value == null )
123
+ private readonly EntityMode entityMode ;
124
+
125
+ public DefaultComparer ( EntityMode entityMode )
126
+ {
127
+ this . entityMode = entityMode ;
128
+ }
129
+
130
+ public bool Equals ( TypedValue x , TypedValue y )
131
+ {
132
+ if ( y == null ) return false ;
133
+ if ( ! x . type . ReturnedClass . Equals ( y . type . ReturnedClass ) )
134
+ return false ;
135
+ return x . type . IsEqual ( y . value , x . value , entityMode ) ;
136
+ }
137
+
138
+ public int GetHashCode ( TypedValue obj )
77
139
{
78
- return " null" ;
140
+ return obj . value == null ? 0 : obj . type . GetHashCode ( obj . value , entityMode ) ;
79
141
}
80
- return value . ToString ( ) ;
81
142
}
82
143
}
83
144
}
0 commit comments