generated from gemstone/gemtem
-
Notifications
You must be signed in to change notification settings - Fork 4
/
CollectionElement.cs
302 lines (266 loc) · 11.5 KB
/
CollectionElement.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
//******************************************************************************************************
// CollectionElement.cs - Gbtc
//
// Copyright © 2012, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 05/02/2012 - Stephen C. Wills, Grid Protection Alliance
// Generated original version of source code.
// 12/17/2012 - Starlynn Danyelle Gilliam
// Modified Header.
// 06/09/2016 - Stephen Jenks, Grid Protection Alliance
// Added readSize member
//
//******************************************************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Gemstone.PQDIF.Physical
{
/// <summary>
/// Represents an <see cref="Element"/> which is a collection of other
/// elements. Collection elements are part of the physical structure of
/// a PQDIF file. They exist within the body of a <see cref="Record"/>.
/// </summary>
public class CollectionElement : Element
{
#region [ Members ]
// Fields
private readonly IList<Element> m_elements;
private int m_readSize; // In case the actual size differs from the read size.
#endregion
#region [ Constructors ]
/// <summary>
/// Creates a new instance of the <see cref="CollectionElement"/> class.
/// </summary>
public CollectionElement()
{
m_elements = new List<Element>();
}
/// <summary>
/// Creates a new instance of the <see cref="CollectionElement"/> class.
/// </summary>
/// <param name="readSize">The size of the collection as read from the PQDIF file.</param>
public CollectionElement(int readSize)
{
m_elements = new List<Element>(readSize);
ReadSize = readSize;
}
#endregion
#region [ Properties ]
/// <summary>
/// Gets or sets the number of elements in the collection.
/// </summary>
public int Size
{
get
{
return m_elements.Count;
}
}
/// <summary>
/// Gets or sets the size that the file says the collection is.
/// This may differ from the actual size if, upon parsing the
/// file the end of file is reached before the collection becomes
/// as large as the read size.
/// </summary>
public int ReadSize
{
get
{
return m_readSize;
}
set
{
m_readSize = value;
}
}
/// <summary>
/// Gets the type of the element.
/// Returns <see cref="ElementType.Collection"/>.
/// </summary>
public override ElementType TypeOfElement
{
get
{
return ElementType.Collection;
}
}
/// <summary>
/// Gets a list of the element in this collection.
/// </summary>
public IList<Element> Elements
{
get
{
return m_elements;
}
}
#endregion
#region [ Methods ]
/// <summary>
/// Adds the given element to the collection.
/// </summary>
/// <param name="element">The element to be added.</param>
public void AddElement(Element element)
{
m_elements.Add(element);
}
/// <summary>
/// Removes the given element from the collection.
/// </summary>
/// <param name="element">The element to be removed.</param>
public void RemoveElement(Element element)
{
m_elements.Remove(element);
}
/// <summary>
/// Removes all elements identified by the given tag from the collection.
/// </summary>
/// <param name="tag">The tag of the elements to be removed.</param>
public void RemoveElementsByTag(Guid tag)
{
for (int i = m_elements.Count - 1; i >= 0; i--)
{
if (m_elements[i].TagOfElement == tag)
m_elements.RemoveAt(i);
}
}
/// <summary>
/// Gets the elements whose tag matches the one given as a parameter.
/// </summary>
/// <param name="tag">The tag of the elements to be retrieved.</param>
/// <returns>
/// An <see cref="IEnumerable{T}"/> of <see cref="Element"/>s
/// identified by the given <paramref name="tag"/>.
/// </returns>
public IEnumerable<Element> GetElementsByTag(Guid tag)
{
return m_elements.Where(element => element.TagOfElement == tag);
}
/// <summary>
/// Gets the element whose tag matches the one given as a
/// parameter, type cast to <see cref="CollectionElement"/>.
/// </summary>
/// <param name="tag">The tag to search by.</param>
/// <returns>The element whose tag matches the one given, or null if no matching collection element exists.</returns>
public CollectionElement? GetCollectionByTag(Guid tag) =>
m_elements.SingleOrDefault(element => element.TagOfElement == tag) as CollectionElement;
/// <summary>
/// Gets the element whose tag matches the one given as a
/// parameter, type cast to <see cref="ScalarElement"/>.
/// </summary>
/// <param name="tag">The tag to search by.</param>
/// <returns>The element whose tag matches the one given, or null if no matching scalar element exists.</returns>
public ScalarElement? GetScalarByTag(Guid tag) =>
m_elements.SingleOrDefault(element => element.TagOfElement == tag) as ScalarElement;
/// <summary>
/// Gets the element whose tag matches the one given as a
/// parameter, type cast to <see cref="VectorElement"/>.
/// </summary>
/// <param name="tag">The tag to search by.</param>
/// <returns>The element whose tag matches the one given, or null if no matching vector element exists.</returns>
public VectorElement? GetVectorByTag(Guid tag) =>
m_elements.SingleOrDefault(element => element.TagOfElement == tag) as VectorElement;
/// <summary>
/// Gets the scalar element identified by the given tag
/// or adds a new scalar element if one does not already exist.
/// </summary>
/// <param name="tag">The tag which identifies the scalar element to be retrieved.</param>
/// <returns>The scalar element identified by the tag, or a new scalar element if one did not already exist.</returns>
public ScalarElement GetOrAddScalar(Guid tag)
{
ScalarElement? scalarElement = GetScalarByTag(tag);
if (scalarElement is null)
{
scalarElement = new ScalarElement();
scalarElement.TagOfElement = tag;
AddElement(scalarElement);
}
return scalarElement;
}
/// <summary>
/// Gets the vector element identified by the given tag
/// or adds a new vector element if one does not already exist.
/// </summary>
/// <param name="tag">The tag which identifies the vector element to be retrieved.</param>
/// <returns>The vector element identified by the tag, or a new vector element if one did not already exist.</returns>
public VectorElement GetOrAddVector(Guid tag)
{
VectorElement? vectorElement = GetVectorByTag(tag);
if (vectorElement is null)
{
vectorElement = new VectorElement();
vectorElement.TagOfElement = tag;
AddElement(vectorElement);
}
return vectorElement;
}
/// <summary>
/// Updates the value of the scalar element identified by the given tag
/// or adds a new scalar element if one does not already exist.
/// </summary>
/// <param name="tag">The tag which identifies the scalar element to be updated.</param>
/// <param name="type">The physical type of the value contained in the scalar element.</param>
/// <param name="bytes">The value to be entered into the scalar element.</param>
/// <returns>The scalar element which was updated, or a new scalar element if one did not already exist.</returns>
public ScalarElement AddOrUpdateScalar(Guid tag, PhysicalType type, byte[] bytes)
{
ScalarElement scalarElement = GetOrAddScalar(tag);
scalarElement.TypeOfValue = type;
scalarElement.SetValue(bytes, 0);
return scalarElement;
}
/// <summary>
/// Updates the values contained by the vector element identified by the given tag
/// or adds a new vector element if one does not already exist.
/// </summary>
/// <param name="tag">The tag which identifies the vector element to be updated.</param>
/// <param name="type">The physical type of the values contained in the vector element.</param>
/// <param name="bytes">The values to be entered into the vector element.</param>
/// <returns>The vector element which was updated, or a new vector element if one did not already exist.</returns>
public VectorElement AddOrUpdateVector(Guid tag, PhysicalType type, byte[] bytes)
{
VectorElement vectorElement = GetOrAddVector(tag);
vectorElement.TypeOfValue = type;
vectorElement.Size = bytes.Length / type.GetByteSize();
vectorElement.SetValues(bytes, 0);
return vectorElement;
}
/// <summary>
/// Returns a string that represents the collection.
/// </summary>
/// <returns>A string that represents the collection.</returns>
public override string ToString()
{
StringBuilder builder = new();
builder.AppendFormat("Collection -- Size: {0}, Tag: {1}", Size, TagOfElement);
foreach (Element element in m_elements)
{
#pragma warning disable CS8602 // Dereference of a possibly null reference.
string[] lines = element?.ToString().Split(new[] { Environment.NewLine }, StringSplitOptions.None) ?? Array.Empty<string>();
#pragma warning restore CS8602 // Dereference of a possibly null reference.
foreach (string line in lines)
{
builder.AppendLine();
builder.AppendFormat(" {0}", line);
}
}
return builder.ToString();
}
#endregion
}
}