forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDistinctRootEntityResultTransformer.cs
63 lines (52 loc) · 1.35 KB
/
DistinctRootEntityResultTransformer.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
using System;
using System.Collections;
using Iesi.Collections;
using log4net;
namespace NHibernate.Transform
{
public class DistinctRootEntityResultTransformer : IResultTransformer
{
private static readonly ILog log = LogManager.GetLogger( typeof( DistinctRootEntityResultTransformer ) );
private static readonly IHashCodeProvider IdentityHashCodeProvider = new HashCodeProvider.IdentityHashCodeProvider();
internal sealed class Identity
{
internal readonly object entity;
internal Identity( object entity )
{
this.entity = entity;
}
public override bool Equals(object other)
{
Identity that = (Identity) other;
return entity == that.entity;
}
public override int GetHashCode()
{
return IdentityHashCodeProvider.GetHashCode( entity );
}
}
public object TransformTuple(object[] tuple, string[] aliases)
{
return tuple[ tuple.Length - 1 ];
}
public IList TransformList(IList list)
{
IList result = new ArrayList();
ISet distinct = new HashedSet();
for( int i = 0; i < list.Count; i++ )
{
object entity = list[ i ];
if( distinct.Add( new Identity( entity ) ) )
{
result.Add( entity );
}
}
if( log.IsDebugEnabled )
{
log.Debug( string.Format( "transformed: {0} rows to: {1} distinct results",
list.Count, result.Count ) );
}
return result;
}
}
}