* ComparerFactory.cs: marked internal
[mono.git] / mcs / class / System.Data / System.Data.Common / ComparerFactory.cs
1 using System;
2 using System.Collections;
3
4 namespace System.Data.Common
5 {
6         /// <summary>
7         /// Summary description for ComparerFactory.
8         /// </summary>
9         internal class DBComparerFactory
10         {
11                 private static IComparer comparableComparer = new ComparebleComparer();
12                 private static IComparer ignoreCaseComparer = new IgnoreCaseComparer();
13                 private static IComparer caseComparer = new CaseComparer();
14                 private static IComparer byteArrayComparer = new ByteArrayComparer();
15                 private static Type icomparerType = typeof (IComparable);
16
17                 public static IComparer GetComparer (Type type, bool ignoreCase)
18                 {
19                         if (type == typeof (string)) {
20                                 if (ignoreCase)
21                                         return ignoreCaseComparer;
22                                 return caseComparer;
23                         }
24                         if (icomparerType.IsAssignableFrom(type))
25                                 return comparableComparer;
26                         if (type == typeof (byte[]))
27                                 return byteArrayComparer;
28                         return null;
29                 }
30
31                 class ComparebleComparer :IComparer
32                 {
33                         #region IComparer Members
34
35                         public int Compare(object x, object y)
36                         {
37                                 if (x == DBNull.Value) {
38                                         if (y == DBNull.Value) 
39                                                 return 0;
40
41                                         return -1;
42                                 }
43
44                                 if (y == DBNull.Value) 
45                                         return 1;
46                                 
47                                 return ((IComparable)x).CompareTo (y);
48                         }
49
50                         #endregion
51                 }
52
53                 class CaseComparer : IComparer
54                 {
55                         #region IComparer Members
56
57                         public int Compare(object x, object y)
58                         {
59                                 if (x == DBNull.Value) {
60                                         if (y == DBNull.Value) 
61                                                 return 0;
62
63                                         return -1;
64                                 }
65
66                                 if (y == DBNull.Value) 
67                                         return 1;
68                                 
69                                 return String.Compare ((string)x, (string)y, false);
70                         }
71
72                         #endregion
73                 }
74
75                 class IgnoreCaseComparer : IComparer
76                 {
77                         #region IComparer Members
78
79                         public int Compare(object x, object y)
80                         {
81                                 if (x == DBNull.Value) {
82                                         if (y == DBNull.Value) 
83                                                 return 0;
84
85                                         return -1;
86                                 }
87
88                                 if (y == DBNull.Value) 
89                                         return 1;
90                                 
91                                 return String.Compare ((string)x, (string)y, true);
92                         }
93
94                         #endregion
95                 }
96
97                 class ByteArrayComparer : IComparer
98                 {
99                         #region IComparer Members
100
101                         public int Compare(object x, object y)
102                         {
103                                 if (x == DBNull.Value) {
104                                         if (y == DBNull.Value) 
105                                                 return 0;
106
107                                         return -1;
108                                 }
109
110                                 if (y == DBNull.Value) 
111                                         return 1;
112                                 
113                                 byte[] o1 = (byte[])x;
114                                 byte[] o2 = (byte[])y;
115                                 int len  = o1.Length;
116                                 int lenb = o2.Length;
117
118                                 for (int i = 0; ; i++) {
119                                         int a = 0;
120                                         int b = 0;
121
122                                         if (i < len) {
123                                                 a = o1[i];
124                                         } 
125                                         else if (i >= lenb) {
126                                                 return 0;
127                                         }
128
129                                         if (i < lenb) {
130                                                 b = o2[i];
131                                         }
132
133                                         if (a > b) {
134                                                 return 1;
135                                         }
136
137                                         if (b > a) {
138                                                 return -1;
139                                         }
140                                 }
141                         }
142
143                         #endregion
144                 }
145         }
146 }