forked from connamara/quickfixn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDictionary.cs
executable file
·273 lines (238 loc) · 8.59 KB
/
Dictionary.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
using QuickFix.Fields.Converters;
namespace QuickFix
{
/// <summary>
/// Name/value pairs used for specifying groups of settings
/// </summary>
public class Dictionary : System.Collections.IEnumerable
{
#region Private Members
private string name_;
private System.Collections.Generic.Dictionary<string, string> data_ = new System.Collections.Generic.Dictionary<string, string>();
#endregion
#region Properties
public string Name
{
get { return name_; }
private set { name_ = value; }
}
public int Count
{
get { return data_.Count; }
}
/// <summary>
/// Alias for Count
/// </summary>
public int Size
{
get { return Count; }
}
#endregion
#region Public Methods
public Dictionary()
{ }
public Dictionary(string name)
: this(name, new System.Collections.Generic.Dictionary<string,string>())
{ }
public Dictionary(Dictionary d)
: this(d.name_, d.data_)
{ }
public Dictionary(string name, System.Collections.Generic.Dictionary<string,string> data)
{
name_ = name;
data_ = new System.Collections.Generic.Dictionary<string, string>(data);
}
public string GetString(string key)
{
string val = "";
if (!data_.TryGetValue(key.ToUpper(), out val))
throw new ConfigError("No value for key: " + key);
return val;
}
public String GetString(string key, bool capitalize)
{
string s = GetString(key);
return capitalize ? s.ToUpper() : s;
}
public int GetInt(string key)
{
try
{
return Convert.ToInt32(GetString(key));
}
catch (FormatException)
{
throw new ConfigError("Incorrect data type");
}
catch (QuickFIXException)
{
throw new ConfigError("No value for key: " + key);
}
}
public long GetLong(string key)
{
try
{
return Convert.ToInt64(GetString(key));
}
catch(FormatException)
{
throw new ConfigError("Incorrect data type");
}
catch(QuickFIXException)
{
throw new ConfigError("No value for key: " + key);
}
}
public double GetDouble(string key)
{
try
{
return Convert.ToDouble(GetString(key), System.Globalization.CultureInfo.InvariantCulture.NumberFormat);
}
catch (FormatException)
{
throw new ConfigError("Incorrect data type");
}
catch (QuickFIXException)
{
throw new ConfigError("No value for key: " + key);
}
}
public bool GetBool(string key)
{
try
{
return Fields.Converters.BoolConverter.Convert(GetString(key));
}
catch (FormatException)
{
throw new ConfigError("Incorrect data type");
}
catch (QuickFIXException)
{
throw new ConfigError("No value for key: " + key);
}
}
public System.DayOfWeek GetDay(string key)
{
string abbr = GetString(key).Substring(0, 2).ToUpper();
switch(abbr)
{
case "SU": return System.DayOfWeek.Sunday;
case "MO": return System.DayOfWeek.Monday;
case "TU": return System.DayOfWeek.Tuesday;
case "WE": return System.DayOfWeek.Wednesday;
case "TH": return System.DayOfWeek.Thursday;
case "FR": return System.DayOfWeek.Friday;
case "SA": return System.DayOfWeek.Saturday;
default: throw new ConfigError("Illegal value " + GetString(key) + " for " + key);
}
}
public TimeStampPrecision GetTimeStampPrecision( string key )
{
string precision = GetString( key ).ToUpper();
switch( precision )
{
case "SECOND":
return TimeStampPrecision.Second;
case "MILLISECOND":
case "MILLI":
return TimeStampPrecision.Millisecond;
case "MICROSECOND":
case "MICRO":
return TimeStampPrecision.Microsecond;
default: throw new ConfigError( "Illegal value " + GetString( key ) + " for " + key );
}
}
public void SetString(string key, string val)
{
data_[key.ToUpper()] = val;
}
public void SetLong(string key, long val)
{
SetString(key, Convert.ToString(val));
}
public void SetDouble(string key, double val)
{
SetString(key, Convert.ToString(val, System.Globalization.CultureInfo.InvariantCulture.NumberFormat));
}
public void SetBool(string key, bool val)
{
SetString(key, Fields.Converters.BoolConverter.Convert(val));
}
public void SetDay(string key, System.DayOfWeek val)
{
switch(val)
{
case System.DayOfWeek.Sunday: SetString(key, "SU"); break;
case System.DayOfWeek.Monday: SetString(key, "MO"); break;
case System.DayOfWeek.Tuesday: SetString(key, "TU"); break;
case System.DayOfWeek.Wednesday: SetString(key, "WE"); break;
case System.DayOfWeek.Thursday: SetString(key, "TH"); break;
case System.DayOfWeek.Friday: SetString(key, "FR"); break;
case System.DayOfWeek.Saturday: SetString(key, "SA"); break;
default: throw new ConfigError("Illegal value " + val + " for " + key);
}
}
public bool Has(string key)
{
return data_.ContainsKey(key.ToUpper());
}
public void Merge(Dictionary toMerge)
{
foreach (System.Collections.Generic.KeyValuePair<string, string> entry in toMerge.data_)
if(!data_.ContainsKey(entry.Key))
data_[entry.Key] = entry.Value;
}
#endregion
#region Public Overrides
/// <summary>
/// Test Dictionary objects for equality.
/// Dictionaries are deemed to be equal if their names and dictionary contents are the same
/// </summary>
/// <param name="other">Dictionary to compare against</param>
/// <returns>true if the two Dictionary objects are the same in terms of contents, else false</returns>
public override bool Equals(object other)
{
//Check whether the compared objects reference the same data.
if (Object.ReferenceEquals(this, other))
return true;
//Check whether the compared object is null.
if (Object.ReferenceEquals(other, null))
return false;
//Check whether the names and dictionary contents are the same
var otherDict = (Dictionary)other;
if (Name != otherDict.Name || Count != otherDict.Count)
return false;
// Could use LINQ query here, but this is probably faster!
string otherDictValue = null;
foreach (var kvp in data_)
if (!otherDict.data_.TryGetValue(kvp.Key, out otherDictValue) || otherDictValue != kvp.Value)
return false;
return true;
}
/// <summary>
/// Generate hash code for the Dictionary.
/// If Equals() returns true for a compared object,
/// then GetHashCode() must return the same value for this object and the compared object.
/// </summary>
/// <returns>hash code</returns>
public override int GetHashCode()
{
int nameHash = Object.ReferenceEquals(Name, null) ? 1 : Name.GetHashCode();
return nameHash + 100 * Count;
}
#endregion
#region IEnumerable Members
public System.Collections.IEnumerator GetEnumerator()
{
return data_.GetEnumerator();
}
#endregion
}
}