forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPropertiesHelper.cs
90 lines (80 loc) · 2.55 KB
/
PropertiesHelper.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
using System;
using System.Collections.Generic;
namespace NHibernate.Util
{
//Much of this code is taken from Maverick.NET
/// <summary></summary>
public static class PropertiesHelper
{
public static bool GetBoolean(string property, IDictionary<string, string> properties, bool defaultValue)
{
string toParse;
properties.TryGetValue(property, out toParse);
bool result;
return bool.TryParse(toParse, out result) ? result : defaultValue;
}
public static bool GetBoolean(string property, IDictionary<string, string> properties)
{
return GetBoolean(property, properties, false);
}
public static byte? GetByte(string property, IDictionary<string, string> properties, byte? defaultValue)
{
string toParse;
properties.TryGetValue(property, out toParse);
byte result;
return byte.TryParse(toParse, out result) ? result : defaultValue;
}
public static int GetInt32(string property, IDictionary<string, string> properties, int defaultValue)
{
string toParse;
properties.TryGetValue(property, out toParse);
int result;
return int.TryParse(toParse, out result) ? result : defaultValue;
}
public static long GetInt64(string property, IDictionary<string, string> properties, long defaultValue)
{
string toParse;
properties.TryGetValue(property, out toParse);
long result;
return long.TryParse(toParse, out result) ? result : defaultValue;
}
public static string GetString(string property, IDictionary<string, string> properties, string defaultValue)
{
string value;
properties.TryGetValue(property, out value);
if(value == string.Empty)
{
value = null;
}
return value ?? defaultValue;
}
public static TEnum GetEnum<TEnum>(string property, IDictionary<string, string> properties, TEnum defaultValue) where TEnum : struct
{
var enumValue = GetString(property, properties, null);
if (enumValue == null)
{
return defaultValue;
}
return (TEnum) Enum.Parse(typeof(TEnum), enumValue, false);
}
public static IDictionary<string, string> ToDictionary(string property, string delim, IDictionary<string, string> properties)
{
IDictionary<string, string> map = new Dictionary<string, string>();
string propValue;
if (properties.TryGetValue(property, out propValue))
{
var tokens = new StringTokenizer(propValue, delim, false);
using (var en = tokens.GetEnumerator())
{
while (en.MoveNext())
{
var key = en.Current;
var value = en.MoveNext() ? en.Current : string.Empty;
map[key] = value;
}
}
}
return map;
}
}
}