-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathPropertiesHelper.cs
88 lines (78 loc) · 2.38 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
using System;
using System.Collections;
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 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 IDictionary<string, string> ToDictionary(string property, string delim, IDictionary<string, string> properties)
{
IDictionary<string, string> map = new Dictionary<string, string>();
if (properties.ContainsKey(property))
{
string propValue = properties[property];
StringTokenizer tokens = new StringTokenizer(propValue, delim, false);
IEnumerator<string> en = tokens.GetEnumerator();
while (en.MoveNext())
{
string key = en.Current;
string value = en.MoveNext() ? en.Current : String.Empty;
map[key] = value;
}
}
return map;
}
public static string[] ToStringArray(string property, string delim, IDictionary properties)
{
return ToStringArray((string) properties[property], delim);
}
public static string[] ToStringArray(string propValue, string delim)
{
if (propValue != null)
{
return StringHelper.Split(delim, propValue);
}
else
{
return new string[0];
}
}
}
}