(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / System.Data / System.Data.Common / ComparerFactory.cs
blob406f69dbbed270311b5f356d21240d4040a8bca8
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.
24 using System;
25 using System.Collections;
27 namespace System.Data.Common
29 /// <summary>
30 /// Summary description for ComparerFactory.
31 /// </summary>
32 internal class DBComparerFactory
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);
40 public static IComparer GetComparer (Type type, bool ignoreCase)
42 if (type == typeof (string)) {
43 if (ignoreCase)
44 return ignoreCaseComparer;
45 return caseComparer;
47 if (icomparerType.IsAssignableFrom(type))
48 return comparableComparer;
49 if (type == typeof (byte[]))
50 return byteArrayComparer;
51 return null;
54 class ComparebleComparer :IComparer
56 #region IComparer Members
58 public int Compare(object x, object y)
60 if (x == DBNull.Value) {
61 if (y == DBNull.Value)
62 return 0;
64 return -1;
67 if (y == DBNull.Value)
68 return 1;
70 return ((IComparable)x).CompareTo (y);
73 #endregion
76 class CaseComparer : IComparer
78 #region IComparer Members
80 public int Compare(object x, object y)
82 if (x == DBNull.Value) {
83 if (y == DBNull.Value)
84 return 0;
86 return -1;
89 if (y == DBNull.Value)
90 return 1;
92 return String.Compare ((string)x, (string)y, false);
95 #endregion
98 class IgnoreCaseComparer : IComparer
100 #region IComparer Members
102 public int Compare(object x, object y)
104 if (x == DBNull.Value) {
105 if (y == DBNull.Value)
106 return 0;
108 return -1;
111 if (y == DBNull.Value)
112 return 1;
114 return String.Compare ((string)x, (string)y, true);
117 #endregion
120 class ByteArrayComparer : IComparer
122 #region IComparer Members
124 public int Compare(object x, object y)
126 if (x == DBNull.Value) {
127 if (y == DBNull.Value)
128 return 0;
130 return -1;
133 if (y == DBNull.Value)
134 return 1;
136 byte[] o1 = (byte[])x;
137 byte[] o2 = (byte[])y;
138 int len = o1.Length;
139 int lenb = o2.Length;
141 for (int i = 0; ; i++) {
142 int a = 0;
143 int b = 0;
145 if (i < len) {
146 a = o1[i];
148 else if (i >= lenb) {
149 return 0;
152 if (i < lenb) {
153 b = o2[i];
156 if (a > b) {
157 return 1;
160 if (b > a) {
161 return -1;
166 #endregion