PageRenderTime 64ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Fudge/Field.cs

https://github.com/OpenGamma/Fudge-CSharp
C# | 348 lines | 213 code | 57 blank | 78 comment | 3 complexity | 59f2ad6dcf9cdef069f2e4b3492c0842 MD5 | raw file
  1. /* <!--
  2. * Copyright (C) 2009 - 2009 by OpenGamma Inc. and other contributors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using System.Text;
  20. using System.Collections;
  21. namespace Fudge
  22. {
  23. /// <summary>
  24. /// <c>Field</c> is a convenience class to allow functional construction of messages.
  25. /// </summary>
  26. /// <remarks>
  27. /// <c>Field</c> merely holds the data until they are added to a <see cref="FudgeMsg"/>, and
  28. /// in particular, the field type is not determined by <c>Field</c>.
  29. /// </remarks>
  30. /// <example>
  31. /// The following example shows constructing a message containing two sub-messages:
  32. /// <code>
  33. /// var inputMsg = new FudgeMsg(
  34. /// new Field("sub1",
  35. /// new Field("bibble", "fibble"),
  36. /// new Field(827, "Blibble")),
  37. /// new Field("sub2",
  38. /// new Field("bibble9", 9837438),
  39. /// new Field(828, 82.77f)));
  40. /// </code>
  41. /// </example>
  42. public class Field : IFudgeField
  43. {
  44. private readonly object value;
  45. private readonly short? ordinal;
  46. private readonly string name;
  47. /// <summary>
  48. /// Constructs a field with a name and value.
  49. /// </summary>
  50. /// <param name="name"></param>
  51. /// <param name="value"></param>
  52. public Field(string name, object value) : this(name, null, value)
  53. {
  54. }
  55. /// <summary>
  56. /// Constructs a field with an ordinal and value.
  57. /// </summary>
  58. /// <param name="ordinal"></param>
  59. /// <param name="value"></param>
  60. public Field(int ordinal, object value) : this(null, ordinal, value)
  61. {
  62. }
  63. /// <summary>
  64. /// Constructs a named field that contains a sub-message of other fields.
  65. /// </summary>
  66. /// <param name="name"></param>
  67. /// <param name="subFields"></param>
  68. /// <example>
  69. /// The following example shows a hierarchical message being created using <see cref="Field"/>.
  70. /// <code>
  71. /// FudgeMsg inputMsg = context.NewMessage(
  72. /// new Field("sub1",
  73. /// new Field("bibble", "fibble"),
  74. /// new Field(827, "Blibble")),
  75. /// new Field("sub2",
  76. /// new Field("bibble9", 9837438),
  77. /// new Field(828, 82.77f)));
  78. ///
  79. /// </code>
  80. /// </example>
  81. public Field(string name, params IFudgeField[] subFields) : this(name, null, new FieldContainer(subFields))
  82. {
  83. }
  84. /// <summary>
  85. /// Constructs a field with both a name and an ordinal plus value.
  86. /// </summary>
  87. /// <param name="name"></param>
  88. /// <param name="ordinal"></param>
  89. /// <param name="value"></param>
  90. public Field(string name, int? ordinal, object value)
  91. {
  92. if (ordinal.HasValue && ordinal < short.MinValue || ordinal > short.MaxValue)
  93. {
  94. throw new ArgumentOutOfRangeException("ordinal", "Ordinal must be within signed 16-bit range");
  95. }
  96. this.name = name;
  97. this.ordinal = (short?)ordinal;
  98. this.value = value;
  99. }
  100. #region IFudgeField Members
  101. /// <summary>
  102. /// Returns <c>null</c>.
  103. /// </summary>
  104. /// <remarks>The field type is not calculated until the field is added to a <see cref="FudgeMsg"/>.</remarks>
  105. public FudgeFieldType Type
  106. {
  107. get { return null; }
  108. }
  109. /// <inheritdoc/>
  110. public object Value
  111. {
  112. get { return value; }
  113. }
  114. /// <inheritdoc/>
  115. public short? Ordinal
  116. {
  117. get { return ordinal; }
  118. }
  119. /// <inheritdoc/>
  120. public string Name
  121. {
  122. get { return name ; }
  123. }
  124. #endregion
  125. /// <summary>
  126. /// Implementation of <see cref="IFudgeFieldContainer"/> purely to hold sub-fields
  127. /// </summary>
  128. private class FieldContainer : IFudgeFieldContainer
  129. {
  130. private readonly IFudgeField[] fields;
  131. public FieldContainer(IFudgeField[] fields)
  132. {
  133. this.fields = fields;
  134. }
  135. #region IFudgeFieldContainer Members
  136. public int GetNumFields()
  137. {
  138. return fields.Length;
  139. }
  140. public IList<IFudgeField> GetAllFields()
  141. {
  142. return fields;
  143. }
  144. public IList<string> GetAllFieldNames()
  145. {
  146. throw new NotSupportedException();
  147. }
  148. public IFudgeField GetByIndex(int index)
  149. {
  150. throw new NotSupportedException();
  151. }
  152. public IList<IFudgeField> GetAllByOrdinal(int ordinal)
  153. {
  154. throw new NotSupportedException();
  155. }
  156. public IFudgeField GetByOrdinal(int ordinal)
  157. {
  158. throw new NotSupportedException();
  159. }
  160. public IList<IFudgeField> GetAllByName(string name)
  161. {
  162. throw new NotSupportedException();
  163. }
  164. public IFudgeField GetByName(string name)
  165. {
  166. throw new NotSupportedException();
  167. }
  168. public object GetValue(string name)
  169. {
  170. throw new NotSupportedException();
  171. }
  172. public T GetValue<T>(string name)
  173. {
  174. throw new NotSupportedException();
  175. }
  176. public object GetValue(string name, Type type)
  177. {
  178. throw new NotSupportedException();
  179. }
  180. public object GetValue(int ordinal)
  181. {
  182. throw new NotSupportedException();
  183. }
  184. public T GetValue<T>(int ordinal)
  185. {
  186. throw new NotSupportedException();
  187. }
  188. public object GetValue(int ordinal, Type type)
  189. {
  190. throw new NotSupportedException();
  191. }
  192. public object GetValue(string name, int? ordinal)
  193. {
  194. throw new NotSupportedException();
  195. }
  196. public T GetValue<T>(string name, int? ordinal)
  197. {
  198. throw new NotSupportedException();
  199. }
  200. public object GetValue(string name, int? ordinal, Type type)
  201. {
  202. throw new NotSupportedException();
  203. }
  204. public double? GetDouble(string fieldName)
  205. {
  206. throw new NotSupportedException();
  207. }
  208. public double? GetDouble(int ordinal)
  209. {
  210. throw new NotSupportedException();
  211. }
  212. public float? GetFloat(string fieldName)
  213. {
  214. throw new NotSupportedException();
  215. }
  216. public float? GetFloat(int ordinal)
  217. {
  218. throw new NotSupportedException();
  219. }
  220. public long? GetLong(string fieldName)
  221. {
  222. throw new NotSupportedException();
  223. }
  224. public long? GetLong(int ordinal)
  225. {
  226. throw new NotSupportedException();
  227. }
  228. public int? GetInt(string fieldName)
  229. {
  230. throw new NotSupportedException();
  231. }
  232. public int? GetInt(int ordinal)
  233. {
  234. throw new NotSupportedException();
  235. }
  236. public short? GetShort(string fieldName)
  237. {
  238. throw new NotSupportedException();
  239. }
  240. public short? GetShort(int ordinal)
  241. {
  242. throw new NotSupportedException();
  243. }
  244. public sbyte? GetSByte(string fieldName)
  245. {
  246. throw new NotSupportedException();
  247. }
  248. public sbyte? GetSByte(int ordinal)
  249. {
  250. throw new NotSupportedException();
  251. }
  252. public bool? GetBoolean(string fieldName)
  253. {
  254. throw new NotSupportedException();
  255. }
  256. public bool? GetBoolean(int ordinal)
  257. {
  258. throw new NotSupportedException();
  259. }
  260. public string GetString(string fieldName)
  261. {
  262. throw new NotSupportedException();
  263. }
  264. public string GetString(int ordinal)
  265. {
  266. throw new NotSupportedException();
  267. }
  268. public IFudgeFieldContainer GetMessage(string fieldName)
  269. {
  270. throw new NotSupportedException();
  271. }
  272. public IFudgeFieldContainer GetMessage(int ordinal)
  273. {
  274. throw new NotSupportedException();
  275. }
  276. #endregion
  277. #region IEnumerable<IFudgeField> Members
  278. public IEnumerator<IFudgeField> GetEnumerator()
  279. {
  280. return ((IList<IFudgeField>)fields).GetEnumerator();
  281. }
  282. #endregion
  283. #region IEnumerable Members
  284. IEnumerator IEnumerable.GetEnumerator()
  285. {
  286. return fields.GetEnumerator();
  287. }
  288. #endregion
  289. }
  290. }
  291. }