< Summary

Information
Class: AmbientServices.AmbientSettingsLayers
Assembly: AmbientServices
File(s): /home/runner/work/AmbientServices/AmbientServices/AmbientServices/AlternateImplementations/AmbientSettingsLayers.cs
Tag: 332_35464845198
Line coverage
100%
Covered lines: 26
Uncovered lines: 0
Coverable lines: 26
Total lines: 105
Line coverage: 100%
Branch coverage
100%
Covered branches: 14
Total branches: 14
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
.ctor(...)100%44100%
.ctor(...)100%11100%
get_SetName()100%22100%
get_SettingsAreMutable()100%11100%
ChangeSetting(...)100%11100%
GetRawValue(...)100%44100%
GetTypedValue(...)100%44100%

File(s)

/home/runner/work/AmbientServices/AmbientServices/AmbientServices/AlternateImplementations/AmbientSettingsLayers.cs

#LineLine coverage
 1using AmbientServices.Extensions;
 2using System;
 3using System.Collections.Generic;
 4using System.Linq;
 5
 6namespace AmbientServices;
 7
 8/// <summary>
 9/// An implementation of <see cref="IAmbientSettingsSet"/> that treats multiple settings sets as a single set.
 10/// </summary>
 11/// <remarks>
 12/// <pitch>Composes several settings sets into one with override semantics — later (higher-priority) sets hide same-name
 13/// <pledge><see cref="IAmbientSettingsSet"/></pledge>
 14/// <pledge>
 15/// Reads probe the layers from highest priority to lowest and return the first value found.  Writes go only to the high
 16/// </pledge>
 17/// <plan>An ordered <see cref="List{T}"/> of the composed sets, fixed at construction (which is what makes unsynchroniz
 18/// </remarks>
 19public class AmbientSettingsLayers : IAmbientSettingsSet
 20{
 321    private readonly List<IAmbientSettingsSet> _setsInLowPriorityOrder = new();
 22
 23    /// <summary>
 24    /// Create a settings layer with just an in-memory settings set.
 25    /// </summary>
 226    public AmbientSettingsLayers()
 27    {
 28        // add a mutable set at the top level
 229        _setsInLowPriorityOrder.Add(new BasicAmbientSettingsSet());
 230    }
 31    /// <summary>
 32    /// Create a settings layer with a specified set of settings sets.
 33    /// If the last (highest priority) set is not mutable, a mutable set will be added so that all settings can be mutat
 34    /// </summary>
 35    /// <param name="sets">An enumeration of settings sets to add, with the last one being the highest priority, hiding 
 336    public AmbientSettingsLayers(IEnumerable<IAmbientSettingsSet?> sets)
 37    {
 338        if (sets == null) throw new ArgumentNullException(nameof(sets));
 339        _setsInLowPriorityOrder.AddRange(sets.WhereNotNull());
 40        // if the last set is not mutable, add a mutable set at the top level
 341        if (!_setsInLowPriorityOrder[_setsInLowPriorityOrder.Count-1].SettingsAreMutable)
 42        {
 243            _setsInLowPriorityOrder.Add(new BasicAmbientSettingsSet());
 44        }
 345    }
 46    /// <summary>
 47    /// Create a settings layer with a specified set of settings sets.
 48    /// If the last (highest priority) set is not mutable, a mutable set will be added so that all settings can be mutat
 49    /// </summary>
 50    /// <param name="sets">An enumeration of settings sets to add, with the last one being the highest priority, hiding 
 351    public AmbientSettingsLayers(params IAmbientSettingsSet?[] sets) : this((IEnumerable<IAmbientSettingsSet?>)sets)
 52    {
 353    }
 54
 55    /// <summary>
 56    /// Gets the name of the set of settings so that a settings consumer can know where a changed setting value came fro
 57    /// </summary>
 258    public string SetName => $"Layers[{string.Join(",", _setsInLowPriorityOrder.Select(s => s.SetName))}]";
 59    /// <summary>
 60    /// Gets whether or not the settings set is mutable.
 61    /// </summary>
 262    public bool SettingsAreMutable => true;
 63    /// <summary>
 64    /// Changes the specified setting.
 65    /// For many ambient settings services, the value will only be reflected in memory until the process shuts down, but
 66    /// </summary>
 67    /// <param name="key">A string that uniquely identifies the setting.</param>
 68    /// <param name="value">The new string value for the setting, or null if the setting should be removed.</param>
 69    /// <returns>Whether or not the setting actually changed.</returns>
 70    public bool ChangeSetting(string key, string? value)
 71    {
 272        IAmbientSettingsSet mutableSet = (IAmbientSettingsSet)_setsInLowPriorityOrder[_setsInLowPriorityOrder.Count-1];
 273        return mutableSet.ChangeSetting(key, value);
 74    }
 75    /// <summary>
 76    /// Gets the current raw value for the setting with the specified key from the first settings set in which it is fou
 77    /// </summary>
 78    /// <param name="key">A key identifying the setting whose value is to be retrieved.</param>
 79    /// <returns>The setting value, or null if the setting is not set in any of the sets.</returns>
 80    public string? GetRawValue(string key)
 81    {
 282        for (int offset = _setsInLowPriorityOrder.Count - 1; offset >= 0; --offset)
 83        {
 284            IAmbientSettingsSet set = _setsInLowPriorityOrder[offset];
 285            string? rawValue = set.GetRawValue(key);
 286            if (rawValue != null) return rawValue;
 87        }
 288        return null;
 89    }
 90    /// <summary>
 91    /// Gets the current typed value for the setting with the specified key from the first settings set in which it is f
 92    /// </summary>
 93    /// <param name="key">A key identifying the setting whose value is to be retrieved.</param>
 94    /// <returns>The setting value, or null if the setting is not set in any of the sets.</returns>
 95    public object? GetTypedValue(string key)
 96    {
 397        for (int offset = _setsInLowPriorityOrder.Count - 1; offset >= 0; --offset)
 98        {
 399            IAmbientSettingsSet set = _setsInLowPriorityOrder[offset];
 3100            object? typedValue = set.GetTypedValue(key);
 3101            if (typedValue != null) return typedValue;
 102        }
 2103        return null;
 104    }
 105}