[runtime] Overwrite stacktrace for exception on re-throw. Fixes #1856.
[mono.git] / mcs / class / System.Data / System.Data.Common / ComparerFactory.cs
1
2 //
3 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
12 // 
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 // 
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 //
24 using System;
25 using System.Collections;
26
27 namespace System.Data.Common
28 {
29         /// <summary>
30         /// Summary description for ComparerFactory.
31         /// </summary>
32         internal class DBComparerFactory
33         {
34                 private static IComparer comparableComparer = new ComparebleComparer();
35                 private static IComparer ignoreCaseComparer = new IgnoreCaseComparer();
36                 private static IComparer caseComparer = new CaseComparer();
37                 private static IComparer byteArrayComparer = new ByteArrayComparer();
38                 private static Type icomparerType = typeof (IComparable);
39
40                 public static IComparer GetComparer (Type type, bool ignoreCase)
41                 {
42                         if (type == typeof (string)) {
43                                 if (ignoreCase)
44                                         return ignoreCaseComparer;
45                                 return caseComparer;
46                         }
47                         if (icomparerType.IsAssignableFrom(type))
48                                 return comparableComparer;
49                         if (type == typeof (byte[]))
50                                 return byteArrayComparer;
51                         return null;
52                 }
53
54                 class ComparebleComparer :IComparer
55                 {
56                         #region IComparer Members
57
58                         public int Compare(object x, object y)
59                         {
60                                 if (x == DBNull.Value) {
61                                         if (y == DBNull.Value) 
62                                                 return 0;
63
64                                         return -1;
65                                 }
66
67                                 if (y == DBNull.Value) 
68                                         return 1;
69                                 
70                                 return ((IComparable)x).CompareTo (y);
71                         }
72
73                         #endregion
74                 }
75
76                 class CaseComparer : IComparer
77                 {
78                         #region IComparer Members
79
80                         public int Compare(object x, object y)
81                         {
82                                 if (x == DBNull.Value) {
83                                         if (y == DBNull.Value) 
84                                                 return 0;
85
86                                         return -1;
87                                 }
88
89                                 if (y == DBNull.Value) 
90                                         return 1;
91                                 
92                                 return String.Compare ((string)x, (string)y, false);
93                         }
94
95                         #endregion
96                 }
97
98                 class IgnoreCaseComparer : IComparer
99                 {
100                         #region IComparer Members
101
102                         public int Compare(object x, object y)
103                         {
104                                 if (x == DBNull.Value) {
105                                         if (y == DBNull.Value) 
106                                                 return 0;
107
108                                         return -1;
109                                 }
110
111                                 if (y == DBNull.Value) 
112                                         return 1;
113                                 
114                                 return String.Compare ((string)x, (string)y, true);
115                         }
116
117                         #endregion
118                 }
119
120                 class ByteArrayComparer : IComparer
121                 {
122                         #region IComparer Members
123
124                         public int Compare(object x, object y)
125                         {
126                                 if (x == DBNull.Value) {
127                                         if (y == DBNull.Value) 
128                                                 return 0;
129
130                                         return -1;
131                                 }
132
133                                 if (y == DBNull.Value) 
134                                         return 1;
135                                 
136                                 byte[] o1 = (byte[])x;
137                                 byte[] o2 = (byte[])y;
138                                 int len  = o1.Length;
139                                 int lenb = o2.Length;
140
141                                 for (int i = 0; ; i++) {
142                                         int a = 0;
143                                         int b = 0;
144
145                                         if (i < len) {
146                                                 a = o1[i];
147                                         } 
148                                         else if (i >= lenb) {
149                                                 return 0;
150                                         }
151
152                                         if (i < lenb) {
153                                                 b = o2[i];
154                                         }
155
156                                         if (a > b) {
157                                                 return 1;
158                                         }
159
160                                         if (b > a) {
161                                                 return -1;
162                                         }
163                                 }
164                         }
165
166                         #endregion
167                 }
168         }
169 }