| | | 1 | | using pva.SuperV.Engine.Processing; |
| | | 2 | | using System.Text.Json.Serialization; |
| | | 3 | | |
| | | 4 | | namespace pva.SuperV.Engine |
| | | 5 | | { |
| | | 6 | | /// <summary> |
| | | 7 | | /// Field of an <see cref="Instance"/> |
| | | 8 | | /// </summary> |
| | | 9 | | /// <typeparam name="T"></typeparam> |
| | | 10 | | /// <seealso cref="pva.SuperV.Engine.IField" /> |
| | | 11 | | public class Field<T> : IField |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Gets the type of the field. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <value> |
| | | 17 | | /// The type of the field. |
| | | 18 | | /// </value> |
| | | 19 | | [JsonIgnore] |
| | 540 | 20 | | public Type Type => typeof(T); |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets or sets the instance to which the field belongs. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <value> |
| | | 26 | | /// The instance. |
| | | 27 | | /// </value> |
| | | 28 | | [JsonIgnore] |
| | 577 | 29 | | public Instance? Instance { get; set; } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Gets or sets the field definition associated with field. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <value> |
| | | 35 | | /// The field definition. |
| | | 36 | | /// </value> |
| | 1767 | 37 | | public IFieldDefinition? FieldDefinition { get; set; } |
| | | 38 | | |
| | | 39 | | private T _value; |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Gets or sets the value of the field. |
| | | 43 | | /// </summary> |
| | | 44 | | /// <value> |
| | | 45 | | /// The value. |
| | | 46 | | /// </value> |
| | | 47 | | [JsonIgnore] |
| | | 48 | | [System.Diagnostics.CodeAnalysis.SuppressMessage("Critical Bug", "S4275:Getters and setters should access the ex |
| | | 49 | | public virtual T Value |
| | | 50 | | { |
| | 1251 | 51 | | get => _value; |
| | 0 | 52 | | set => SetValue(value); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | private DateTime? _timestamp; |
| | | 56 | | /// <summary> |
| | | 57 | | /// Gets or sets the timestamp of value. |
| | | 58 | | /// </summary> |
| | | 59 | | /// <value> |
| | | 60 | | /// The timestamp. |
| | | 61 | | /// </value> |
| | | 62 | | public DateTime? Timestamp |
| | | 63 | | { |
| | | 64 | | get |
| | 569 | 65 | | { |
| | 569 | 66 | | _timestamp ??= DateTime.Now; |
| | 569 | 67 | | return _timestamp; |
| | 569 | 68 | | } |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | private QualityLevel? _quality; |
| | | 72 | | /// <summary> |
| | | 73 | | /// Gets or sets the quality level of the value. |
| | | 74 | | /// </summary> |
| | | 75 | | /// <value> |
| | | 76 | | /// The quality <see cref="QualityLevel" />. |
| | | 77 | | /// </value> |
| | | 78 | | public QualityLevel? Quality |
| | | 79 | | { |
| | | 80 | | get |
| | 567 | 81 | | { |
| | 567 | 82 | | _quality ??= QualityLevel.Good; |
| | 567 | 83 | | return _quality; |
| | 567 | 84 | | } |
| | | 85 | | } |
| | | 86 | | |
| | 621 | 87 | | public Field(T value) |
| | 621 | 88 | | { |
| | 621 | 89 | | _value = value; |
| | 621 | 90 | | _timestamp ??= DateTime.Now; |
| | 621 | 91 | | _quality ??= QualityLevel.Good; |
| | 621 | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Sets the value with <see cref="DateTime.UtcNow"/> timestamp and a <see cref="QualityLevel.Good"/>. |
| | | 96 | | /// </summary> |
| | | 97 | | /// <param name="newValue">The value to be set.</param> |
| | | 98 | | public void SetValue(T newValue) |
| | 40 | 99 | | { |
| | 40 | 100 | | SetValue(newValue, DateTime.UtcNow, QualityLevel.Good); |
| | 40 | 101 | | } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Sets the value and associated timestamp with a <see cref="QualityLevel.Good"/>. |
| | | 105 | | /// </summary> |
| | | 106 | | /// <param name="newValue">The value to be set.</param> |
| | | 107 | | /// <param name="timestamp">The timestamp of value.</param> |
| | | 108 | | /// <param name="quality">The quality of value.</param> |
| | | 109 | | public void SetValue(T newValue, DateTime? timestamp, QualityLevel? quality) |
| | 210 | 110 | | { |
| | 210 | 111 | | T previousValue = _value; |
| | 210 | 112 | | SetValueInternal(newValue, timestamp ?? DateTime.Now, quality ?? QualityLevel.Good); |
| | 210 | 113 | | bool valueChanged = (previousValue == null && (previousValue as object) != (newValue as object)) || |
| | 210 | 114 | | !previousValue!.Equals(newValue); |
| | 210 | 115 | | ProcessNewValue(valueChanged, newValue, previousValue!); |
| | 210 | 116 | | } |
| | | 117 | | |
| | | 118 | | public void SetValueInternal(T newValue, DateTime? timestamp, QualityLevel? quality) |
| | 230 | 119 | | { |
| | 230 | 120 | | _timestamp = timestamp; |
| | 230 | 121 | | _quality = quality; |
| | 230 | 122 | | _value = newValue; |
| | 230 | 123 | | } |
| | | 124 | | |
| | | 125 | | private void ProcessNewValue(bool hasValueChanged, T newValue, T previousValue) |
| | 210 | 126 | | { |
| | 210 | 127 | | FieldDefinition?.ValuePostChangeProcessings.ForEach( |
| | 210 | 128 | | processing => |
| | 42 | 129 | | { |
| | 42 | 130 | | FieldValueProcessing<T>? fieldValueProcessing = processing as FieldValueProcessing<T>; |
| | 42 | 131 | | fieldValueProcessing!.ProcessValue(Instance!, this, hasValueChanged, previousValue, newValue); |
| | 252 | 132 | | }); |
| | 210 | 133 | | } |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// Converts field's value to string. If a field formatter is defined in field definition, it's used. |
| | | 137 | | /// </summary> |
| | | 138 | | /// <returns> |
| | | 139 | | /// A string that represents the current object. |
| | | 140 | | /// </returns> |
| | | 141 | | public override string? ToString() |
| | 1 | 142 | | { |
| | 1 | 143 | | return FieldDefinition?.Formatter?.ConvertToString(Value) ?? |
| | 1 | 144 | | Value?.ToString(); |
| | 1 | 145 | | } |
| | | 146 | | } |
| | | 147 | | } |