Update Reference Sources to .NET Framework 4.6
[mono.git] / mcs / class / referencesource / System.Data / System / Data / DataError.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="DataError.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>               
5 // <owner current="true" primary="true">Microsoft</owner>
6 // <owner current="true" primary="false">Microsoft</owner>
7 // <owner current="false" primary="false">Microsoft</owner>
8 //------------------------------------------------------------------------------
9
10 namespace System.Data {
11     using System;
12     using System.Diagnostics;
13
14     /// <devdoc>
15     /// <para>Represents an custom error that can be associated with a <see cref='System.Data.DataRow'/>.</para>
16     /// </devdoc>
17    internal sealed class DataError {
18         private string rowError = String.Empty;
19
20         // column-level errors
21         private int count;
22         private ColumnError[] errorList;
23         internal const int initialCapacity = 1;
24
25         internal DataError() {
26         }
27
28         internal DataError(string rowError) {
29             SetText(rowError);
30         }
31
32         internal string Text {
33             get {
34                 return rowError;
35             }
36             set {
37                 SetText(value);
38             }
39         }
40
41         internal bool HasErrors {
42             get {
43                 return(rowError.Length != 0 || count != 0);
44             }
45         }
46
47         //
48         // this method resets the error to the new value.
49         //
50         internal void SetColumnError(DataColumn column, string error) {
51             Debug.Assert(column != null, "Invalid (null) argument");
52             Debug.Assert(column.Table != null, "Invalid (loose) column");
53             if (error == null || error.Length == 0) {
54                 // remove error from the collection
55                 Clear(column);
56             }
57             else {
58                 if (errorList == null) {
59                     errorList = new ColumnError[initialCapacity];
60                 }
61                 int i = IndexOf(column);
62                 errorList[i].column = column;
63                 errorList[i].error = error;
64                 column.errors++;
65                 if (i == count)
66                     count++;
67             }
68         }
69
70         internal string GetColumnError(DataColumn column) {
71             for (int i = 0; i < count; i++) {
72                 if (errorList[i].column == column) {
73                     return errorList[i].error;
74                 }
75             }
76             return String.Empty;
77         }
78
79         internal void Clear(DataColumn column) {
80             if (count == 0)
81                 return;
82
83             for (int i = 0; i < count; i++) {
84                 if (errorList[i].column == column) {
85                     System.Array.Copy(errorList, i+1, errorList, i, count-i-1);
86                     count--;
87                     column.errors--;
88                     Debug.Assert(column.errors >= 0, "missing error counts");
89                 }
90             }
91         }
92
93         internal void Clear() {
94             for (int i = 0; i < count; i++) {
95                 errorList[i].column.errors--;
96                 Debug.Assert(errorList[i].column.errors >= 0, "missing error counts");
97             }
98             count = 0;
99             rowError = String.Empty;
100         }
101
102         internal DataColumn[] GetColumnsInError() {
103             DataColumn[] cols = new DataColumn[count];
104
105             for (int i = 0; i < count; i++) {
106                 cols[i] = errorList[i].column;
107             }
108             return cols;
109         }
110
111         /// <devdoc>
112         /// <para>Sets the error message for the <see cref='System.Data.DataError'/>.</para>
113         /// </devdoc>
114         private void SetText(string errorText) {
115             if (null == errorText) {
116                 errorText = String.Empty;
117             }
118             rowError = errorText;
119         }
120
121         internal int IndexOf (DataColumn column) {
122             // try to find the column
123             for (int i = 0; i < count; i++) {
124                 if (errorList[i].column == column) {
125                     return i;
126                 }
127             }
128
129             if (count >= errorList.Length) {
130                 int newCapacity = Math.Min(count*2, column.Table.Columns.Count);
131                 ColumnError[] biggerList = new ColumnError[newCapacity];
132                 System.Array.Copy(errorList, 0, biggerList, 0, count);
133                 errorList = biggerList;
134             }
135             return count;
136         }
137
138         internal struct ColumnError {
139             internal DataColumn column;
140             internal string error;
141         };
142     }
143 }