Merge pull request #820 from brendanzagaeski/master
[mono.git] / mcs / class / corlib / System.Collections.Generic / Comparer.cs
1 //
2 // Comparer.cs
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@ximian.com)
6 //      Marek Safar (marek.safar@gmail.com)
7 //
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 // Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Runtime.InteropServices;
33
34 namespace System.Collections.Generic {
35         [Serializable]
36         public abstract class Comparer<T> : IComparer<T>, IComparer
37         {
38                 static readonly Comparer <T> _default = typeof (IComparable<T>).IsAssignableFrom (typeof (T)) ?
39                         (Comparer<T>) Activator.CreateInstance (typeof (GenericComparer <>).MakeGenericType (typeof (T))) :
40                         new DefaultComparer ();
41                 
42                 public abstract int Compare (T x, T y);
43         
44                 public static Comparer<T> Default {
45                         get {
46                                 return _default;
47                         }
48                 }
49
50 #if NET_4_5
51                 public static Comparer<T> Create (Comparison<T> comparison)
52                 {
53                         if (comparison == null)
54                                 throw new ArgumentNullException ("comparison");
55
56                         return new ComparisonComparer<T> (comparison);
57                 }
58 #endif
59
60                 int IComparer.Compare (object x, object y)
61                 {
62                         if (x == y)
63                                 return 0;
64                         if (x == null)
65                                 return y == null ? 0 : -1;
66                         if (y == null)
67                                 return 1;
68                         
69                         if (x is T && y is T)
70                                 return Compare ((T) x, (T) y);
71                         
72                         throw new ArgumentException ();
73                 }
74         
75                 [Serializable]
76                 sealed class DefaultComparer : Comparer<T>
77                 {
78                         public override int Compare (T x, T y)
79                         {
80                                 // `null' is less than any other ref type
81                                 if (x == null)
82                                         return y == null ? 0 : -1;
83                                 if (y == null)
84                                         return 1;
85         
86                                 var i = x as IComparable;
87                                 if (i != null)
88                                         return i.CompareTo (y);
89
90                                 i = y as IComparable;
91                                 if (i != null)
92                                         return -i.CompareTo (x);
93
94                                 throw new ArgumentException ("At least one argument has to implement IComparable interface");
95                         }
96                 }
97         }
98         
99         [Serializable]
100         sealed class GenericComparer<T> : Comparer<T> where T : IComparable<T>
101         {
102                 public override int Compare (T x, T y)
103                 {
104                         // `null' is less than any other ref type
105                         if (x == null)
106                                 return y == null ? 0 : -1;
107                         if (y == null)
108                                 return 1;
109                         
110                         return x.CompareTo (y);
111                 }
112         }
113 #if NET_4_5
114         [Serializable]
115         sealed class ComparisonComparer<T> : Comparer<T>
116         {
117                 readonly Comparison<T> comparison;
118
119                 public ComparisonComparer (Comparison<T> comparison)
120                 {
121                         this.comparison = comparison;
122                 }
123
124                 public override int Compare (T x, T y)
125                 {
126                         return comparison (x, y);
127                 }
128         }
129 #endif
130 }