PageRenderTime 28ms CodeModel.GetById 17ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/Fudge/Field.cs

https://github.com/FudgeMsg/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 */
 16using System;
 17using System.Collections.Generic;
 18using System.Linq;
 19using System.Text;
 20using System.Collections;
 21
 22namespace Fudge
 23{
 24    /// <summary>
 25    /// <c>Field</c> is a convenience class to allow functional construction of messages.
 26    /// </summary>
 27    /// <remarks>
 28    /// <c>Field</c> merely holds the data until they are added to a <see cref="FudgeMsg"/>, and
 29    /// in particular, the field type is not determined by <c>Field</c>.
 30    /// </remarks>
 31    /// <example>
 32    /// The following example shows constructing a message containing two sub-messages:
 33    /// <code>
 34    /// var inputMsg = new FudgeMsg(   
 35    ///                     new Field("sub1",
 36    ///                         new Field("bibble", "fibble"),
 37    ///                         new Field(827, "Blibble")),
 38    ///                     new Field("sub2", 
 39    ///                         new Field("bibble9", 9837438),
 40    ///                         new Field(828, 82.77f)));
 41    /// </code>
 42    /// </example>
 43    public class Field : IFudgeField
 44    {
 45        private readonly object value;
 46        private readonly short? ordinal;
 47        private readonly string name;
 48
 49        /// <summary>
 50        /// Constructs a field with a name and value.
 51        /// </summary>
 52        /// <param name="name"></param>
 53        /// <param name="value"></param>
 54        public Field(string name, object value) : this(name, null, value)
 55        {
 56        }
 57
 58        /// <summary>
 59        /// Constructs a field with an ordinal and value.
 60        /// </summary>
 61        /// <param name="ordinal"></param>
 62        /// <param name="value"></param>
 63        public Field(int ordinal, object value) : this(null, ordinal, value)
 64        {
 65        }
 66
 67        /// <summary>
 68        /// Constructs a named field that contains a sub-message of other fields.
 69        /// </summary>
 70        /// <param name="name"></param>
 71        /// <param name="subFields"></param>
 72        /// <example>
 73        /// The following example shows a hierarchical message being created using <see cref="Field"/>.
 74        /// <code>
 75        /// FudgeMsg inputMsg = context.NewMessage(
 76        ///            new Field("sub1",
 77        ///                new Field("bibble", "fibble"),
 78        ///                new Field(827, "Blibble")),
 79        ///            new Field("sub2",
 80        ///                new Field("bibble9", 9837438),
 81        ///                new Field(828, 82.77f)));
 82        /// 
 83        /// </code>
 84        /// </example>
 85        public Field(string name, params IFudgeField[] subFields) : this(name, null, new FieldContainer(subFields))
 86        {
 87        }
 88
 89        /// <summary>
 90        /// Constructs a field with both a name and an ordinal plus value.
 91        /// </summary>
 92        /// <param name="name"></param>
 93        /// <param name="ordinal"></param>
 94        /// <param name="value"></param>
 95        public Field(string name, int? ordinal, object value)
 96        {
 97            if (ordinal.HasValue && ordinal < short.MinValue || ordinal > short.MaxValue)
 98            {
 99                throw new ArgumentOutOfRangeException("ordinal", "Ordinal must be within signed 16-bit range");
100            }
101            this.name = name;
102            this.ordinal = (short?)ordinal;
103            this.value = value;
104        }
105
106        #region IFudgeField Members
107
108        /// <summary>
109        /// Returns <c>null</c>.
110        /// </summary>
111        /// <remarks>The field type is not calculated until the field is added to a <see cref="FudgeMsg"/>.</remarks>
112        public FudgeFieldType Type
113        {
114            get { return null; }
115        }
116
117        /// <inheritdoc/>
118        public object Value
119        {
120            get { return value; }
121        }
122
123        /// <inheritdoc/>
124        public short? Ordinal
125        {
126            get { return ordinal; }
127        }
128
129        /// <inheritdoc/>
130        public string Name
131        {
132            get { return name ; }
133        }
134
135        #endregion
136
137        /// <summary>
138        /// Implementation of <see cref="IFudgeFieldContainer"/> purely to hold sub-fields
139        /// </summary>
140        private class FieldContainer : IFudgeFieldContainer
141        {
142            private readonly IFudgeField[] fields;
143
144            public FieldContainer(IFudgeField[] fields)
145            {
146                this.fields = fields;
147            }
148
149            #region IFudgeFieldContainer Members
150
151            public int GetNumFields()
152            {
153                return fields.Length;
154            }
155
156            public IList<IFudgeField> GetAllFields()
157            {
158                return fields;
159            }
160
161            public IList<string> GetAllFieldNames()
162            {
163                throw new NotSupportedException();
164            }
165
166            public IFudgeField GetByIndex(int index)
167            {
168                throw new NotSupportedException();
169            }
170
171            public IList<IFudgeField> GetAllByOrdinal(int ordinal)
172            {
173                throw new NotSupportedException();
174            }
175
176            public IFudgeField GetByOrdinal(int ordinal)
177            {
178                throw new NotSupportedException();
179            }
180
181            public IList<IFudgeField> GetAllByName(string name)
182            {
183                throw new NotSupportedException();
184            }
185
186            public IFudgeField GetByName(string name)
187            {
188                throw new NotSupportedException();
189            }
190
191            public object GetValue(string name)
192            {
193                throw new NotSupportedException();
194            }
195
196            public T GetValue<T>(string name)
197            {
198                throw new NotSupportedException();
199            }
200
201            public object GetValue(string name, Type type)
202            {
203                throw new NotSupportedException();
204            }
205
206            public object GetValue(int ordinal)
207            {
208                throw new NotSupportedException();
209            }
210
211            public T GetValue<T>(int ordinal)
212            {
213                throw new NotSupportedException();
214            }
215
216            public object GetValue(int ordinal, Type type)
217            {
218                throw new NotSupportedException();
219            }
220
221            public object GetValue(string name, int? ordinal)
222            {
223                throw new NotSupportedException();
224            }
225
226            public T GetValue<T>(string name, int? ordinal)
227            {
228                throw new NotSupportedException();
229            }
230
231            public object GetValue(string name, int? ordinal, Type type)
232            {
233                throw new NotSupportedException();
234            }
235
236            public double? GetDouble(string fieldName)
237            {
238                throw new NotSupportedException();
239            }
240
241            public double? GetDouble(int ordinal)
242            {
243                throw new NotSupportedException();
244            }
245
246            public float? GetFloat(string fieldName)
247            {
248                throw new NotSupportedException();
249            }
250
251            public float? GetFloat(int ordinal)
252            {
253                throw new NotSupportedException();
254            }
255
256            public long? GetLong(string fieldName)
257            {
258                throw new NotSupportedException();
259            }
260
261            public long? GetLong(int ordinal)
262            {
263                throw new NotSupportedException();
264            }
265
266            public int? GetInt(string fieldName)
267            {
268                throw new NotSupportedException();
269            }
270
271            public int? GetInt(int ordinal)
272            {
273                throw new NotSupportedException();
274            }
275
276            public short? GetShort(string fieldName)
277            {
278                throw new NotSupportedException();
279            }
280
281            public short? GetShort(int ordinal)
282            {
283                throw new NotSupportedException();
284            }
285
286            public sbyte? GetSByte(string fieldName)
287            {
288                throw new NotSupportedException();
289            }
290
291            public sbyte? GetSByte(int ordinal)
292            {
293                throw new NotSupportedException();
294            }
295
296            public bool? GetBoolean(string fieldName)
297            {
298                throw new NotSupportedException();
299            }
300
301            public bool? GetBoolean(int ordinal)
302            {
303                throw new NotSupportedException();
304            }
305
306            public string GetString(string fieldName)
307            {
308                throw new NotSupportedException();
309            }
310
311            public string GetString(int ordinal)
312            {
313                throw new NotSupportedException();
314            }
315
316            public IFudgeFieldContainer GetMessage(string fieldName)
317            {
318                throw new NotSupportedException();
319            }
320
321            public IFudgeFieldContainer GetMessage(int ordinal)
322            {
323                throw new NotSupportedException();
324            }
325
326            #endregion
327
328            #region IEnumerable<IFudgeField> Members
329
330            public IEnumerator<IFudgeField> GetEnumerator()
331            {
332                return ((IList<IFudgeField>)fields).GetEnumerator();
333            }
334
335            #endregion
336
337            #region IEnumerable Members
338
339            IEnumerator IEnumerable.GetEnumerator()
340            {
341                return fields.GetEnumerator();
342            }
343
344            #endregion
345        }
346
347    }
348}