forked from connamara/quickfixn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFieldMap.cs
393 lines (339 loc) · 12.6 KB
/
FieldMap.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuickFix.Fields;
namespace QuickFix
{
public class FieldMap : IEnumerable<KeyValuePair<int, Fields.IField>>
{
/// <summary>
/// Default constructor
/// </summary>
public FieldMap()
{
_fields = new SortedDictionary<int, Fields.IField>(); /// FIXME sorted dict is a hack to get quasi-correct field order
_groups = new Dictionary<int, List<Group>>();
this.RepeatedTags = new List<Fields.IField>();
}
/// <summary>
/// Constructor with field order
/// </summary>
/// <param name="fieldOrd"></param>
public FieldMap(int[] fieldOrd)
: this()
{
_fieldOrder = fieldOrd;
}
/// <summary>
/// FIXME this should probably make a deeper copy
/// </summary>
/// <param name="src">The QuickFix.FieldMap to copy</param>
/// <returns>A copy of the given QuickFix.FieldMap</returns>
public FieldMap(FieldMap src)
{
this._fieldOrder = src._fieldOrder;
this._fields = new SortedDictionary<int, Fields.IField>(src._fields);
this._groups = new Dictionary<int, List<Group>>();
foreach (KeyValuePair<int, List<Group>> g in src._groups)
this._groups.Add(g.Key, new List<Group>(g.Value));
this.RepeatedTags = new List<Fields.IField>(src.RepeatedTags);
}
/// <summary>
/// FieldOrder Property
/// order of field tags as an integer array
/// </summary>
public int[] FieldOrder
{
get { return _fieldOrder; }
private set { _fieldOrder = value; }
}
/// <summary>
/// QuickFIX-CPP compat, see FieldOrder property
/// </summary>
/// <returns>field order integer array</returns>
public int[] getFieldOrder()
{
return _fieldOrder;
}
public bool RemoveField(int field)
{
return _fields.Remove(field);
}
/// <summary>
/// set field in the fieldmap
/// will overwrite field if it exists
/// </summary>
public void setField(Fields.IField field)
{
_fields[field.Tag] = field;
}
/// <summary>
/// Set field with overwrite flag
/// </summary>
/// <param name="field"></param>
/// <param name="overwrite">will overwrite existing field if set to true</param>
public bool setField(Fields.IField field, bool overwrite)
{
if (_fields.ContainsKey(field.Tag) && !overwrite)
return false;
setField(field);
return true;
}
public void getField(Fields.BooleanField field)
{
if (_fields.ContainsKey(field.Tag))
field.Obj = ((Fields.BooleanField)_fields[field.Tag]).Obj;
else
throw new FieldNotFoundException(field.Tag);
}
public void getField(Fields.StringField field)
{
if (_fields.ContainsKey(field.Tag))
field.Obj = ((Fields.StringField)_fields[field.Tag]).Obj;
else
throw new FieldNotFoundException(field.Tag);
}
public void getField(Fields.CharField field)
{
if (_fields.ContainsKey(field.Tag))
field.Obj = ((Fields.CharField)_fields[field.Tag]).Obj;
else
throw new FieldNotFoundException(field.Tag);
}
public void getField(Fields.IntField field)
{
if (_fields.ContainsKey(field.Tag))
field.Obj = ((Fields.IntField)_fields[field.Tag]).Obj;
else
throw new FieldNotFoundException(field.Tag);
}
public void getField(Fields.DecimalField field)
{
if (_fields.ContainsKey(field.Tag))
field.Obj = ((Fields.DecimalField)_fields[field.Tag]).Obj;
else
throw new FieldNotFoundException(field.Tag);
}
public void getField(Fields.DateTimeField field)
{
if (_fields.ContainsKey(field.Tag))
field.Obj = ((Fields.DateTimeField)_fields[field.Tag]).Obj;
else
throw new FieldNotFoundException(field.Tag);
}
/// <summary>
/// Check to see if field is set
/// </summary>
/// <param name="field">Field Object</param>
/// <returns>true if set</returns>
public bool isSetField(Fields.IField field)
{
return isSetField(field.Tag);
}
/// <summary>
/// Check to see if field is set
/// </summary>
/// <param name="tag">Tag Number</param>
/// <returns>true if set</returns>
public bool isSetField(int tag)
{
return _fields.ContainsKey(tag);
}
public void AddGroup(Group group)
{
if (!_groups.ContainsKey(group.Field))
_groups.Add(group.Field, new List<Group>());
_groups[group.Field].Add(group);
}
/// <summary>
/// Gets specific group instance
/// </summary>
/// <param name="num">num of group (starting at 1)</param>
/// <param name="field">tag of group</param>
/// <returns>Group object</returns>
/// <exception cref="FieldNotFoundException" />
public Group GetGroup(int num, int field)
{
if (!_groups.ContainsKey(field))
throw new FieldNotFoundException(field);
if (num <= 0)
throw new FieldNotFoundException(field);
if (_groups[field].Count < num)
throw new FieldNotFoundException(field);
return _groups[field][num - 1];
}
/// <summary>
/// Removes specific group instance
/// </summary>
/// <param name="num">num of group (starting at 1)</param>
/// <param name="field">tag of group</param>
/// <exception cref="FieldNotFoundException" />
public void RemoveGroup(int num, int field)
{
if (!_groups.ContainsKey(field))
throw new FieldNotFoundException(field);
if (num <= 0)
throw new FieldNotFoundException(field);
if (_groups[field].Count < num)
throw new FieldNotFoundException(field);
if (_groups[field].Count.Equals(1))
_groups.Remove(field);
else
_groups[field].RemoveAt(num - 1);
}
/// <summary>
/// Replaces specific group instance
/// </summary>
/// <param name="num">num of group (starting at 1)</param>
/// <param name="field">tag of group</param>
/// <returns>Group object</returns>
/// <exception cref="FieldNotFoundException" />
public Group ReplaceGroup(int num, int field, Group group)
{
if (!_groups.ContainsKey(field))
throw new FieldNotFoundException(field);
if (num <= 0)
throw new FieldNotFoundException(field);
if (_groups[field].Count < num)
throw new FieldNotFoundException(field);
return _groups[field][num - 1] = group;
}
/// <summary>
/// getField without a type defaults to returning a string
/// </summary>
/// <param name="tag">fix tag</param>
public string GetField(int tag)
{
if (_fields.ContainsKey(tag))
return _fields[tag].ToString();
else
throw new FieldNotFoundException(tag);
}
/// <summary>
/// Removes fields and groups in message
/// </summary>
public virtual void Clear()
{
_fields.Clear();
_groups.Clear();
}
/// <summary>
/// Checks emptiness of message
/// </summary>
/// <returns>true if no fields or groups have been set</returns>
public bool IsEmpty()
{
return ((_fields.Count == 0) && (_groups.Count == 0));
}
public int CalculateTotal()
{
int total = 0;
foreach (Fields.IField field in _fields.Values)
{
if (field.Tag != Fields.Tags.CheckSum)
total += field.getTotal();
}
// TODO not sure if repeated CheckSum should be included int the total
foreach (Fields.IField field in this.RepeatedTags)
{
if (field.Tag != Fields.Tags.CheckSum)
total += field.getTotal();
}
foreach (List<Group> groupList in _groups.Values)
{
foreach (Group group in groupList)
total += group.CalculateTotal();
}
return total;
}
public int CalculateLength()
{
int total = 0;
foreach (Fields.IField field in _fields.Values)
{
if (field != null
&& field.Tag != Tags.BeginString
&& field.Tag != Tags.BodyLength
&& field.Tag != Tags.CheckSum)
{
total += field.getLength();
}
}
// TODO not sure if repeated BeginString/BodyLength/CheckSum should be counted
foreach (Fields.IField field in this.RepeatedTags)
{
if (field != null
&& field.Tag != Tags.BeginString
&& field.Tag != Tags.BodyLength
&& field.Tag != Tags.CheckSum)
{
total += field.getLength();
}
}
foreach (List<Group> groupList in _groups.Values)
{
foreach (Group group in groupList)
total += group.CalculateLength();
}
return total;
}
public virtual string CalculateString()
{
return CalculateString(new StringBuilder(), new int[0]);
}
public virtual string CalculateString(StringBuilder sb, int[] preFields)
{
foreach (int preField in preFields)
{
if (isSetField(preField))
sb.Append(preField + "=" + GetField(preField)).Append(Message.SOH);
}
foreach (Fields.IField field in _fields.Values)
{
if (IsOrderedField(field.Tag, preFields))
continue;
sb.Append(field.Tag.ToString() + "=" + field.ToString());
sb.Append(Message.SOH);
}
foreach (List<Group> groupList in _groups.Values)
{
foreach (Group group in groupList)
sb.Append(CalculateString());
}
return sb.ToString();
}
private bool IsOrderedField(int field, int[] fieldOrder)
{
foreach (int f in fieldOrder)
{
if (field == f)
return true;
}
return false;
}
#region Private Members
private SortedDictionary<int, Fields.IField> _fields; /// FIXME sorted dict is a hack to get quasi-correct field order
private Dictionary<int, List<Group>> _groups;
private int[] _fieldOrder;
#endregion
#region Properties
/// <summary>
/// Used for validation. Only set during Message parsing.
/// </summary>
public List<Fields.IField> RepeatedTags { get; private set; }
#endregion
#region IEnumerable<KeyValuePair<int,IField>> Members
public IEnumerator<KeyValuePair<int, IField>> GetEnumerator()
{
return _fields.GetEnumerator();
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return _fields.GetEnumerator();
}
#endregion
}
}