Add [Category ("NotWorking")] to failing test.
[mono.git] / mcs / class / corlib / System.Collections.Generic / Comparer.cs
1 //
2 // Comparer
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                         
63                         if (x == null)
64                                 return y == null ? 0 : -1;
65                         if (y == null)
66                                 return 1;
67                         
68                         if (x is T && y is T)
69                                 return Compare ((T) x, (T) y);
70                         
71                         throw new ArgumentException ();
72                 }
73         
74                 [Serializable]
75                 sealed class DefaultComparer : Comparer<T>
76                 {
77                         public override int Compare (T x, T y)
78                         {
79                                 // `null' is less than any other ref type
80                                 if (x == null)
81                                         return y == null ? 0 : -1;
82                                 else if (y == null)
83                                         return 1;
84         
85                                 if (x is IComparable<T>)
86                                         return ((IComparable<T>) x).CompareTo (y);
87                                 else if (x is IComparable)
88                                         return ((IComparable) x).CompareTo (y);
89                                 else
90                                         throw new ArgumentException ("does not implement right interface");
91                         }
92                 }
93         }
94         
95         [Serializable]
96         sealed class GenericComparer<T> : Comparer<T> where T : IComparable<T>
97         {
98                 public override int Compare (T x, T y)
99                 {
100                         // `null' is less than any other ref type
101                         if (x == null)
102                                 return y == null ? 0 : -1;
103                         if (y == null)
104                                 return 1;
105                         
106                         return x.CompareTo (y);
107                 }
108         }
109 #if NET_4_5
110         [Serializable]
111         sealed class ComparisonComparer<T> : Comparer<T>
112         {
113                 readonly Comparison<T> comparison;
114
115                 public ComparisonComparer (Comparison<T> comparison)
116                 {
117                         this.comparison = comparison;
118                 }
119
120                 public override int Compare (T x, T y)
121                 {
122                         return comparison (x, y);
123                 }
124         }
125 #endif
126 }