forked from 51Degrees/dotNET-Device-Detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Profile.cs
518 lines (477 loc) · 18.1 KB
/
Profile.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/* *********************************************************************
* This Source Code Form is copyright of 51Degrees Mobile Experts Limited.
* Copyright © 2014 51Degrees Mobile Experts Limited, 5 Charlotte Close,
* Caversham, Reading, Berkshire, United Kingdom RG4 7BY
*
* This Source Code Form is the subject of the following patent
* applications, owned by 51Degrees Mobile Experts Limited of 5 Charlotte
* Close, Caversham, Reading, Berkshire, United Kingdom RG4 7BY:
* European Patent Application No. 13192291.6; and
* United States Patent Application Nos. 14/085,223 and 14/085,301.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0.
*
* If a copy of the MPL was not distributed with this file, You can obtain
* one at http://mozilla.org/MPL/2.0/.
*
* This Source Code Form is “Incompatible With Secondary Licenses”, as
* defined by the Mozilla Public License, v. 2.0.
* ********************************************************************* */
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Diagnostics;
using System.Collections.Concurrent;
namespace FiftyOne.Foundation.Mobile.Detection.Entities
{
/// <summary>
/// Represents a collection of properties and values relating to a profile
/// which in turn relates to a component.
/// </summary>
/// <para>
/// Each <see cref="Signature"/> relates to one profile for each component.
/// </para>
public abstract class Profile : BaseEntity, IComparable<Profile>
{
#region Fields
/// <summary>
/// Unique Id of the profile. Does not change between different data sets.
/// </summary>
public readonly int ProfileId;
#endregion
#region Internal Properties
/// <summary>
/// The release date of the profile if it's a hardware profile.
/// </summary>
internal DateTime ReleaseDate
{
get
{
if (_releaseDateChecked == false)
{
lock (this)
{
if (_releaseDateChecked == false)
{
var month = Values.FirstOrDefault(i =>
i.Property.Name == "ReleaseMonth");
var year = Values.FirstOrDefault(i =>
i.Property.Name == "ReleaseYear");
if (month != null && year != null)
{
int monthValue = GetMonthAsInt(month.Name);
int yearValue;
if (monthValue >= 1 && monthValue <= 12 &&
int.TryParse(year.Name, out yearValue))
{
_releaseDate = new DateTime(yearValue, monthValue, 1);
}
}
_releaseDateChecked = true;
}
}
}
return _releaseDate;
}
}
private bool _releaseDateChecked;
private DateTime _releaseDate;
#endregion
#region Abstract Properties
/// <summary>
/// A array of the indexes of the values associated with the profile.
/// </summary>
protected internal abstract int[] ValueIndexes { get; }
/// <summary>
/// An array of the signature indexes associated with the profile.
/// </summary>
protected internal abstract int[] SignatureIndexes { get; }
#endregion
#region Public Properties
/// <summary>
/// Gets the values associated with the property.
/// </summary>
/// <param name="property">The property whose values are required</param>
/// <returns>
/// Array of the values associated with the property, or null if the property does not exist
/// </returns>
/// <para>
/// The <see cref="Values"/> type is used to return values so that
/// helper methods like ToBool can be used to convert the response to
/// a boolean.
/// </para>
public Values this[Property property]
{
get
{
Values values = null;
if (PropertyIndexToValues.TryGetValue(property.Index, out values) == false)
{
lock(this)
{
if (PropertyIndexToValues.TryGetValue(property.Index, out values) == false)
{
values = new Entities.Values(
property,
GetPropertyValuesEnumerable(property));
PropertyIndexToValues.Add(property.Index, values);
}
}
}
return values;
}
}
/// <summary>
/// A dictionary relating the index of a property to the
/// values returned by the profile. Used to speed up
/// subsequent data processing.
/// </summary>
private IDictionary<int, Values> PropertyIndexToValues
{
get
{
if (_propertyIndexToValues == null)
{
lock(this)
{
if (_propertyIndexToValues == null)
{
_propertyIndexToValues = new Dictionary<int, Values>();
}
}
}
return _propertyIndexToValues;
}
}
private IDictionary<int, Values> _propertyIndexToValues;
/// <summary>
/// Gets the values associated with the property name.
/// </summary>
/// <param name="propertyName">Name of the property whose values are required</param>
/// <returns>
/// Array of the values associated with the property, or null if the property does not exist
/// </returns>
/// <para>
/// The <see cref="Values"/> type is used to return values so that
/// helper methods like ToBool can be used to convert the response to
/// a boolean.
/// </para>
public Values this[string propertyName]
{
get
{
Values values = null;
if (_nameToValues == null ||
_nameToValues.TryGetValue(propertyName, out values) == false)
{
lock (this)
{
if (_nameToValues == null ||
_nameToValues.TryGetValue(propertyName, out values) == false)
{
var property = DataSet.Properties[propertyName];
if (property != null)
{
values = this[property];
}
if (_nameToValues == null)
{
_nameToValues = new SortedList<string, Values>();
}
_nameToValues.Add(propertyName, values);
}
}
}
return values;
}
}
private SortedList<string, Values> _nameToValues;
/// <summary>
/// Array of signatures associated with the profile.
/// </summary>
public Signature[] Signatures
{
get
{
if (_signatures == null)
{
lock (this)
{
if (_signatures == null)
{
_signatures = GetSignatures();
}
}
}
return _signatures;
}
}
private Signature[] _signatures;
/// <summary>
/// The component the profile belongs to.
/// </summary>
public Component Component
{
get
{
if (_component == null)
{
lock (this)
{
if (_component == null)
{
_component = DataSet.Components[_componentIndex];
}
}
}
return _component;
}
}
private Component _component;
private readonly int _componentIndex;
/// <summary>
/// An array of values associated with the profile.
/// </summary>
public Value[] Values
{
get
{
if (_values == null)
{
lock (this)
{
if (_values == null)
{
_values = GetValues().ToArray();
}
}
}
return _values;
}
}
private Value[] _values;
/// <summary>
/// An array of properties associated with the profile.
/// </summary>
public Property[] Properties
{
get
{
if (_properties == null)
{
lock (this)
{
if (_properties == null)
{
_properties = GetProperties();
}
}
}
return _properties;
}
}
private Property[] _properties = null;
#endregion
#region Constructor
/// <summary>
/// Constructs a new instance of the <see cref="Profile"/>
/// </summary>
/// <param name="dataSet">
/// The data set whose profile list the profile will be contained within
/// </param>
/// <param name="offset">
/// The offset position in the data structure to the profile</param>
/// <param name="reader">
/// Reader connected to the source data structure and positioned to start reading
/// </param>
internal Profile(
DataSet dataSet,
int offset,
BinaryReader reader)
: base(dataSet, offset)
{
_componentIndex = reader.ReadByte();
ProfileId = reader.ReadInt32();
}
#endregion
#region Methods
/// <summary>
/// Called after the entire data set has been loaded to ensure
/// any further initialisation steps that require other items in
/// the data set can be completed.
/// </summary>
internal virtual void Init()
{
if (_properties == null)
_properties = GetProperties();
if (_values == null)
{
_values = GetValues().ToArray();
}
if (_signatures == null)
{
_signatures = GetSignatures();
}
if (_component == null)
_component = DataSet.Components[_componentIndex];
}
private int GetValuesIndex(int valueIndex, int lower = 0)
{
var upper = ValueIndexes.Length - 1;
int middle = 0;
while (lower <= upper)
{
middle = lower + (upper - lower) / 2;
var comparisonResult = ValueIndexes[middle].CompareTo(valueIndex);
if (comparisonResult == 0)
return middle;
else if (comparisonResult > 0)
upper = middle - 1;
else
lower = middle + 1;
}
return ~middle;
}
/// <summary>
/// Gets the values associated with the property for this profile.
/// </summary>
/// <param name="property"></param>
/// <returns>Iterator providing access to the values for the property for this profile</returns>
private IEnumerable<Value> GetPropertyValuesEnumerable(Property property)
{
var start = GetValuesIndex(property.FirstValueIndex);
if (start < 0) start = ~start;
var end = GetValuesIndex(property.LastValueIndex, start);
if (end < 0) end = ~end;
Debug.Assert(start == 0 ||
DataSet.Values[ValueIndexes[start - 1]].Property.Index != property.Index);
Debug.Assert(end == ValueIndexes.Length - 1 ||
DataSet.Values[ValueIndexes[end + 1]].Property.Index != property.Index);
for (int i = start; i <= end; i++)
{
var value = DataSet.Values[ValueIndexes[i]];
if (value.Property.Index == property.Index)
{
yield return DataSet.Values[ValueIndexes[i]];
}
}
}
/// <summary>
/// Gets the signatures related to the profile.
/// </summary>
/// <returns>Array of signatures related to the profile.</returns>
private Signature[] GetSignatures()
{
var signatures = new Signature[SignatureIndexes.Length];
for (int i = 0; i < SignatureIndexes.Length; i++)
{
signatures[i] = DataSet.Signatures[SignatureIndexes[i]];
}
return signatures;
}
/// <summary>
/// Returns an array of properties the profile relates to.
/// </summary>
/// <returns></returns>
private Property[] GetProperties()
{
var properties = new List<Property>();
foreach (var value in Values)
{
var index = properties.BinarySearch(value.Property);
if (index < 0)
properties.Insert(~index, value.Property);
}
return properties.ToArray();
}
/// <summary>
/// Returns an iterator of values the profile relates to.
/// </summary>
/// <returns></returns>
private IEnumerable<Value> GetValues()
{
return ValueIndexes.Select(i =>
DataSet.Values[i]);
}
/// <summary>
/// Returns the month name as an integer.
/// </summary>
/// <param name="month">Name of the month, i.e. January</param>
/// <returns>The integer representation of the month</returns>
private static int GetMonthAsInt(string month)
{
switch (month)
{
case "January": return 1;
case "February": return 2;
case "March": return 3;
case "April": return 4;
case "May": return 5;
case "June": return 6;
case "July": return 7;
case "August": return 8;
case "September": return 9;
case "October": return 10;
case "November": return 11;
case "December": return 12;
default: return 0;
}
}
#endregion
#region Public Methods
/// <summary>
/// Compares this profile to another using the numeric
/// ProfileId field.
/// </summary>
/// <param name="other">The profile to be compared against</param>
/// <returns>Indication of relative value based on ProfileId field</returns>
public int CompareTo(Profile other)
{
return ProfileId.CompareTo(other.ProfileId);
}
/// <summary>
/// A string representation of the profiles display values or if none
/// are available a concatentation of the display properties.
/// </summary>
/// <returns>A string that represents the profile</returns>
/// <para>
/// If there are properties with the DisplayOrder properties set to
/// values greater than 1 their values are concatenated in ascending
/// display order and returned as a string.
/// If no DisplayOrder properties are available (Lite data) then the
/// string version of the ProfileId is returned.
/// </para>
/// <para>
/// For more information see https://51degrees.com/Support/Documentation/Net
/// </para>
public override string ToString()
{
if (_stringValue == null)
{
lock (this)
{
if (_stringValue == null)
{
_stringValue =
String.Join(
"/",
Values.Where(i =>
i.Property.DisplayOrder > 0 &&
i.Name != "Unknown").OrderByDescending(i =>
i.Property.DisplayOrder).Select(i =>
i.Name).Distinct().ToArray()).Trim();
if (String.IsNullOrEmpty(_stringValue))
{
_stringValue = ProfileId.ToString();
}
}
}
}
return _stringValue;
}
private string _stringValue = null;
#endregion
}
}