Fix handling of null in the non generic implementation of EqualityComparer. Fixes...
[mono.git] / mcs / class / corlib / System.Collections.Generic / EqualityComparer.cs
1 //
2 // Comparer
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@ximian.com)
6 //
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.Runtime.InteropServices;
31
32 namespace System.Collections.Generic {
33         [Serializable]
34         public abstract class EqualityComparer <T> : IEqualityComparer, IEqualityComparer <T> {
35                 
36                 static EqualityComparer ()
37                 {
38                         if (typeof (T) == typeof (string)){
39                                 _default = (EqualityComparer<T>) (object) new InternalStringComparer ();
40                                 return;
41                         }
42                         if (typeof (IEquatable <T>).IsAssignableFrom (typeof (T)))
43                                 _default = (EqualityComparer <T>) Activator.CreateInstance (typeof (GenericEqualityComparer <>).MakeGenericType (typeof (T)));
44                         else
45                                 _default = new DefaultComparer ();
46                 }
47                 
48                 public abstract int GetHashCode (T obj);
49                 public abstract bool Equals (T x, T y);
50         
51                 static readonly EqualityComparer <T> _default;
52                 
53                 public static EqualityComparer <T> Default {
54                         get {
55                                 return _default;
56                         }
57                 }
58
59                 int IEqualityComparer.GetHashCode (object obj)
60                 {
61                         if (obj == null)
62                                 return 0;
63
64                         if (!(obj is T))
65                                 throw new ArgumentException ("Argument is not compatible", "obj");
66
67                         return GetHashCode ((T)obj);
68                 }
69
70                 bool IEqualityComparer.Equals (object x, object y)
71                 {
72                         if (x == y)
73                                 return true;
74
75                         if (x == null || y == null)
76                                 return false;
77
78                         if (!(x is T))
79                                 throw new ArgumentException ("Argument is not compatible", "x");
80                         if (!(y is T))
81                                 throw new ArgumentException ("Argument is not compatible", "y");
82                         return Equals ((T)x, (T)y);
83                 }
84                 
85                 [Serializable]
86                 sealed class DefaultComparer : EqualityComparer<T> {
87         
88                         public override int GetHashCode (T obj)
89                         {
90                                 if (obj == null)
91                                         return 0;
92                                 return obj.GetHashCode ();
93                         }
94         
95                         public override bool Equals (T x, T y)
96                         {
97                                 if (x == null)
98                                         return y == null;
99
100                                 return x.Equals (y);
101                         }
102                 }
103         }
104         
105         [Serializable]
106         sealed class InternalStringComparer : EqualityComparer<string> {
107         
108                 public override int GetHashCode (string obj)
109                 {
110                         if (obj == null)
111                                 return 0;
112                         return obj.GetHashCode ();
113                 }
114         
115                 public override bool Equals (string x, string y)
116                 {
117                         if (x == null)
118                                 return y == null;
119
120                         if ((object) x == (object) y)
121                                 return true;
122                                 
123                         return x.Equals (y);
124                 }
125         }
126
127         [Serializable]
128         sealed class GenericEqualityComparer <T> : EqualityComparer <T> where T : IEquatable <T> {
129
130                 public override int GetHashCode (T obj)
131                 {
132                         if (obj == null)
133                                 return 0;
134                         return obj.GetHashCode ();
135                 }
136
137                 public override bool Equals (T x, T y)
138                 {
139                         if (x == null)
140                                 return y == null;
141                         
142                         return x.Equals (y);
143                 }
144         }
145 }