< Summary - pva.SuperV

Information
Class: pva.SuperV.Engine.Field<T>
Assembly: pva.SuperV.Engine
File(s): /home/runner/work/pva.SuperV/pva.SuperV/pva.SuperV.Engine/Field.cs
Tag: dotnet-ubuntu_22190969454
Line coverage
95%
Covered lines: 63
Uncovered lines: 3
Coverable lines: 66
Total lines: 186
Line coverage: 95.4%
Branch coverage
73%
Covered branches: 19
Total branches: 26
Branch coverage: 73%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Type()100%11100%
get_Instance()100%11100%
get_FieldDefinition()100%11100%
get_Value()100%11100%
set_Value(...)100%11100%
get_Timestamp()50%22100%
get_Quality()50%22100%
.ctor(...)100%44100%
SetValue(...)100%11100%
SetValue(...)100%66100%
SetValueInternal(...)100%11100%
ProcessNewValue(...)100%4489.28%
ToString()37.5%88100%

File(s)

/home/runner/work/pva.SuperV/pva.SuperV/pva.SuperV.Engine/Field.cs

#LineLine coverage
 1using pva.SuperV.Common;
 2using pva.SuperV.Engine.Processing;
 3using System.Text.Json.Serialization;
 4using System.Threading.Channels;
 5
 6namespace pva.SuperV.Engine
 7{
 8    /// <summary>
 9    /// Field of an <see cref="Instance"/>
 10    /// </summary>
 11    /// <typeparam name="T">Type of field</typeparam>
 12    /// <seealso cref="pva.SuperV.Engine.IField" />
 13    public class Field<T> : IField
 14    {
 15        /// <summary>
 16        /// Gets the type of the field.
 17        /// </summary>
 18        /// <value>
 19        /// The type of the field.
 20        /// </value>
 21        [JsonIgnore]
 63422        public Type Type => typeof(T);
 23
 24        /// <summary>
 25        /// Gets or sets the instance to which the field belongs.
 26        /// </summary>
 27        /// <value>
 28        /// The instance.
 29        /// </value>
 30        [JsonIgnore]
 67631        public Instance? Instance { get; set; }
 32
 33        /// <summary>
 34        /// Gets or sets the field definition associated with field.
 35        /// </summary>
 36        /// <value>
 37        /// The field definition.
 38        /// </value>
 253839        public IFieldDefinition? FieldDefinition { get; set; }
 40
 41        private T _value;
 42
 43        /// <summary>
 44        /// Gets or sets the value of the field.
 45        /// </summary>
 46        /// <value>
 47        /// The value.
 48        /// </value>
 49        [JsonIgnore]
 50        [System.Diagnostics.CodeAnalysis.SuppressMessage("Critical Bug", "S4275:Getters and setters should access the ex
 51        public virtual T Value
 52        {
 146453            get => _value;
 454            set => SetValue(value);
 55        }
 56
 57        private DateTime? _timestamp;
 58        /// <summary>
 59        /// Gets or sets the timestamp of value.
 60        /// </summary>
 61        /// <value>
 62        /// The timestamp.
 63        /// </value>
 64        public DateTime? Timestamp
 65        {
 66            get
 71267            {
 71268                _timestamp ??= DateTime.Now;
 71269                return _timestamp;
 71270            }
 71        }
 72
 73        private QualityLevel? _quality;
 74        /// <summary>
 75        /// Gets or sets the quality level of the value.
 76        /// </summary>
 77        /// <value>
 78        /// The quality <see cref="QualityLevel" />.
 79        /// </value>
 80        public QualityLevel? Quality
 81        {
 82            get
 71083            {
 71084                _quality ??= QualityLevel.Good;
 71085                return _quality;
 71086            }
 87        }
 88
 89        /// <summary>
 90        /// Initializes a new instance of the <see cref="Field{T}"/> class.
 91        /// </summary>
 92        /// <param name="value">The initial value of field.</param>
 68193        public Field(T value)
 68194        {
 68195            _value = value;
 68196            _timestamp ??= DateTime.Now;
 68197            _quality ??= QualityLevel.Good;
 68198        }
 99
 100        /// <summary>
 101        /// Sets the value with <see cref="DateTime.UtcNow"/> timestamp and a <see cref="QualityLevel.Good"/>.
 102        /// </summary>
 103        /// <param name="newValue">The value to be set.</param>
 104        public void SetValue(T newValue)
 53105        {
 53106            SetValue(newValue, DateTime.UtcNow, QualityLevel.Good);
 53107        }
 108
 109        /// <summary>
 110        /// Sets the value and associated timestamp with a <see cref="QualityLevel.Good"/>.
 111        /// </summary>
 112        /// <param name="newValue">The value to be set.</param>
 113        /// <param name="timestamp">The timestamp of value.</param>
 114        /// <param name="quality">The quality of value.</param>
 115        public void SetValue(T newValue, DateTime? timestamp, QualityLevel? quality)
 252116        {
 252117            T previousValue = _value;
 252118            SetValueInternal(newValue, timestamp ?? DateTime.Now, quality ?? QualityLevel.Good);
 252119            bool valueChanged = (EqualityComparer<T?>.Default.Equals(previousValue, default) && (previousValue as object
 252120                !previousValue!.Equals(newValue);
 252121            ProcessNewValue(valueChanged, newValue, previousValue!);
 252122        }
 123
 124        /// <summary>
 125        /// Sets the value internally without triggering the processings.
 126        /// </summary>
 127        /// <param name="newValue">The new value.</param>
 128        /// <param name="timestamp">The timestamp.</param>
 129        /// <param name="quality">The quality.</param>
 130        public void SetValueInternal(T newValue, DateTime? timestamp, QualityLevel? quality)
 272131        {
 272132            _timestamp = timestamp;
 272133            _quality = quality;
 272134            _value = newValue;
 272135        }
 136
 137        /// <summary>
 138        /// Processes the new value. Triggers the post change processings and value changed event scripts.
 139        /// </summary>
 140        /// <param name="hasValueChanged">if set to <c>true</c> [has value changed].</param>
 141        /// <param name="newValue">The new value.</param>
 142        /// <param name="previousValue">The previous value.</param>
 143        private void ProcessNewValue(bool hasValueChanged, T newValue, T previousValue)
 252144        {
 252145            if (FieldDefinition is null)
 1146            {
 1147                return;
 148            }
 251149            FieldDefinition.ValuePostChangeProcessings.ForEach(
 251150                processing =>
 114151                {
 114152                    FieldValueProcessing<T>? fieldValueProcessing = processing as FieldValueProcessing<T>;
 114153                    fieldValueProcessing!.ProcessValue(Instance!, this, hasValueChanged, previousValue, newValue);
 365154                });
 251155            if (FieldDefinition.FieldValueChangedEventChannel is not null)
 14156            {
 14157                Task.Run(async () =>
 14158                {
 14159                    try
 14160                    {
 14161                        FieldValueChangedEvent fieldValueChangedEvent = new(FieldDefinition.TopicName!, this, previousVa
 14162                        await FieldDefinition!.FieldValueChangedEventChannel.Writer
 14163                                            .WriteAsync(fieldValueChangedEvent).AsTask();
 14164                    }
 0165                    catch (ChannelClosedException)
 0166                    {
 14167                        // Channel is closed, ignore
 0168                    }
 14169                })
 14170                .Wait();
 14171            }
 252172        }
 173
 174        /// <summary>
 175        /// Converts field's value to string. If a field formatter is defined in field definition, it's used.
 176        /// </summary>
 177        /// <returns>
 178        /// A string that represents the current object.
 179        /// </returns>
 180        public override string? ToString()
 1181        {
 1182            return FieldDefinition?.Formatter?.ConvertToString(Value) ??
 1183                Value?.ToString();
 1184        }
 185    }
 186}