< 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_18869653307
Line coverage
97%
Covered lines: 45
Uncovered lines: 1
Coverable lines: 46
Total lines: 147
Line coverage: 97.8%
Branch coverage
62%
Covered branches: 15
Total branches: 24
Branch coverage: 62.5%
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%210%
get_Timestamp()50%22100%
get_Quality()50%22100%
.ctor(...)100%44100%
SetValue(...)100%11100%
SetValue(...)66.66%66100%
SetValueInternal(...)100%11100%
ProcessNewValue(...)100%22100%
ToString()37.5%88100%

File(s)

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

#LineLine coverage
 1using pva.SuperV.Engine.Processing;
 2using System.Text.Json.Serialization;
 3
 4namespace 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]
 54020        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]
 57729        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>
 176737        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        {
 125151            get => _value;
 052            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
 56965            {
 56966                _timestamp ??= DateTime.Now;
 56967                return _timestamp;
 56968            }
 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
 56781            {
 56782                _quality ??= QualityLevel.Good;
 56783                return _quality;
 56784            }
 85        }
 86
 62187        public Field(T value)
 62188        {
 62189            _value = value;
 62190            _timestamp ??= DateTime.Now;
 62191            _quality ??= QualityLevel.Good;
 62192        }
 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)
 4099        {
 40100            SetValue(newValue, DateTime.UtcNow, QualityLevel.Good);
 40101        }
 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)
 210110        {
 210111            T previousValue = _value;
 210112            SetValueInternal(newValue, timestamp ?? DateTime.Now, quality ?? QualityLevel.Good);
 210113            bool valueChanged = (previousValue == null && (previousValue as object) != (newValue as object)) ||
 210114                !previousValue!.Equals(newValue);
 210115            ProcessNewValue(valueChanged, newValue, previousValue!);
 210116        }
 117
 118        public void SetValueInternal(T newValue, DateTime? timestamp, QualityLevel? quality)
 230119        {
 230120            _timestamp = timestamp;
 230121            _quality = quality;
 230122            _value = newValue;
 230123        }
 124
 125        private void ProcessNewValue(bool hasValueChanged, T newValue, T previousValue)
 210126        {
 210127            FieldDefinition?.ValuePostChangeProcessings.ForEach(
 210128                processing =>
 42129                {
 42130                    FieldValueProcessing<T>? fieldValueProcessing = processing as FieldValueProcessing<T>;
 42131                    fieldValueProcessing!.ProcessValue(Instance!, this, hasValueChanged, previousValue, newValue);
 252132                });
 210133        }
 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()
 1142        {
 1143            return FieldDefinition?.Formatter?.ConvertToString(Value) ??
 1144                Value?.ToString();
 1145        }
 146    }
 147}