forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollectionPrinter.cs
96 lines (85 loc) · 1.92 KB
/
CollectionPrinter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace NHibernate.Util
{
/// <summary>
/// Utility class implementing ToString for collections. All <c>ToString</c>
/// overloads call <c>element.ToString()</c>.
/// </summary>
/// <remarks>
/// To print collections of entities or typed values, use
/// <see cref="NHibernate.Impl.Printer" />.
/// </remarks>
public static class CollectionPrinter
{
private static void AppendNullOrValue(StringBuilder builder, object value)
{
if (value == null)
{
builder.Append("null");
}
else
{
builder
.Append("'")
.Append(value)
.Append("'");
}
}
public static string ToString(IDictionary dictionary)
{
StringBuilder result = new StringBuilder();
result.Append("{");
bool first = true;
foreach (DictionaryEntry de in dictionary)
{
if (!first)
{
result.Append(", ");
}
AppendNullOrValue(result, de.Key);
result.Append("=");
AppendNullOrValue(result, de.Value);
first = false;
}
result.Append("}");
return result.ToString();
}
public static string ToString(IDictionary<string, string> dictionary)
{
StringBuilder result = new StringBuilder();
result.Append("{");
bool first = true;
foreach (KeyValuePair<string, string> de in dictionary)
{
if (!first)
result.Append(", ");
AppendNullOrValue(result, de.Key);
result.Append("=");
AppendNullOrValue(result, de.Value);
first = false;
}
result.Append("}");
return result.ToString();
}
public static string ToString(IEnumerable elements)
{
StringBuilder result = new StringBuilder();
result.Append("[");
bool first = true;
foreach (object item in elements)
{
if (!first)
{
result.Append(", ");
}
AppendNullOrValue(result, item);
first = false;
}
result.Append("]");
return result.ToString();
}
}
}