Merge pull request #1624 from esdrubal/getprocesstimes
[mono.git] / mcs / class / System.Data.DataSetExtensions / System.Data / DataRowComparer_1.cs
1 //
2 // DataRowComparer_1.cs
3 //
4 // Author:
5 //   Atsushi Enomoto  <atsushi@ximian.com>
6 //
7 // Copyright (C) 2008 Novell, Inc. http://www.novell.com
8 //
9
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.Collections.Generic;
33
34 namespace System.Data
35 {
36         public sealed class DataRowComparer<TRow> : IEqualityComparer<TRow> where TRow : DataRow
37         {
38                 static readonly DataRowComparer<TRow> default_instance = new DataRowComparer<TRow> ();
39
40                 public static DataRowComparer<TRow> Default {
41                         get { return default_instance; }
42                 }
43
44                 private DataRowComparer ()
45                 {
46                 }
47
48                 // LAMESPEC: neither of the parameters throws ArgumentNullException if it's null
49                 public bool Equals (TRow leftRow, TRow rightRow)
50                 {
51                         if (object.ReferenceEquals (leftRow, rightRow))
52                                 return true;
53
54                         if (leftRow == null || rightRow == null)
55                                 return false;
56
57                         int columnCount = leftRow.Table.Columns.Count;
58                         if (columnCount != rightRow.Table.Columns.Count)
59                                 return false;
60
61                         for (int i = 0; i < columnCount; i++)
62                                 if (!ColumnsEqual (leftRow [i], rightRow [i]))
63                                         return false;
64                         return true;
65                 }
66
67                 bool ColumnsEqual (object leftCol, object rightCol)
68                 {
69                         if (object.ReferenceEquals (leftCol, rightCol))
70                                 return true;
71
72                         if (leftCol == null || rightCol == null)
73                                 return false;
74
75                         var vt = leftCol as System.ValueType;
76                         if (vt != null && vt.Equals (rightCol))
77                                 return true;
78
79                         return leftCol.Equals (rightCol);
80                 }
81                 
82                 public int GetHashCode (TRow row)
83                 {
84                         if (row == null)
85                                 throw new ArgumentNullException ("row");
86
87                         DataTable table = row.Table;
88                         if (table == null)
89                                 throw new ArgumentException ("The source DataRow objects does not belong to a DataTable.");
90
91                         DataColumnCollection columns = table.Columns;
92                         int columnCount = columns.Count;
93                         if (columnCount == 0)
94                                 return 0;
95
96                         int ret = 0;
97                         object o;
98                         for (int i = 0; i < columnCount; i++) {
99                                 o = row [i];
100                                 if (o == null)
101                                         continue;
102
103                                 ret ^= o.GetHashCode ();
104                         }
105                                 
106                         return ret;
107                 }
108         }
109 }