< Summary

Information
Class: AmbientServices.AmbientImmutableSettingsSet
Assembly: AmbientServices
File(s): /home/runner/work/AmbientServices/AmbientServices/AmbientServices/AlternateImplementations/AmbientImmutableSettingsSet.cs
Tag: 332_35464845198
Line coverage
100%
Covered lines: 21
Uncovered lines: 0
Coverable lines: 21
Total lines: 105
Line coverage: 100%
Branch coverage
90%
Covered branches: 9
Total branches: 10
Branch coverage: 90%
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%11100%
.ctor(...)83.33%66100%
GetRawValue(...)100%22100%
GetTypedValue(...)100%22100%
get_SettingsAreMutable()100%11100%
ChangeSetting(...)100%11100%
ToString()100%11100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3
 4namespace AmbientServices;
 5
 6/// <summary>
 7/// A settings set implementation that contains settings that are assigned at construction and cannot be changed.
 8/// </summary>
 9/// <remarks>
 10/// <pitch>A frozen snapshot of settings fixed at construction — the cheapest possible reads and complete predictability
 11/// <pledge><see cref="IAmbientSettingsSet"/></pledge>
 12/// <pledge>Values never change after construction; the set reports itself immutable and any attempt to change a setting
 13/// <plan>Plain (non-concurrent) dictionaries hold the raw and typed values, safe for unsynchronized concurrent reads pr
 14/// </remarks>
 15public class AmbientImmutableSettingsSet : IAmbientSettingsSet
 16{
 17    /// <summary>
 18    /// The set name of the default settings set.
 19    /// </summary>
 20    public const string DefaultSetName = "Immutable";
 21    private readonly Dictionary<string, string> _rawValues;
 22    private readonly Dictionary<string, object> _typedValues;
 23
 24    /// <summary>
 25    /// Constructs the default ambient settings set.
 26    /// </summary>
 27    internal AmbientImmutableSettingsSet()
 228        : this(DefaultSetName)
 29    {
 230    }
 31    /// <summary>
 32    /// Constructs a new immutable ambient settings set with the specified values.
 33    /// </summary>
 34    /// <param name="name">The name of the set.</param>
 235    public AmbientImmutableSettingsSet(string name)
 36    {
 237        SetName = name;
 238        _rawValues = new Dictionary<string, string>();
 239        _typedValues = new Dictionary<string, object>();
 240    }
 41    /// <summary>
 42    /// Constructs a new immutable ambient settings set with the specified values.
 43    /// </summary>
 44    /// <param name="name">The name of the set.</param>
 45    /// <param name="values">A set of name-value pairs to use for the initial values for the set.</param>
 246    public AmbientImmutableSettingsSet(string name, IDictionary<string, string> values)
 47    {
 248        SetName = name;
 249        _rawValues = new Dictionary<string, string>(values);
 250        _typedValues = new Dictionary<string, object>();
 251        if (values != null)
 52        {
 253            foreach (string key in values.Keys)
 54            {
 255                IAmbientSettingInfo? ps = SettingsRegistry.DefaultRegistry.TryGetSetting(key);
 256                _typedValues[key] = (ps != null) ? ps.Convert(this, values[key]) : values[key];
 57            }
 58        }
 259    }
 60
 61    /// <summary>
 62    /// Gets the name of the settings set so that a settings consumer can know where a changed setting value came from.
 63    /// </summary>
 64    public string SetName { get; }
 65    /// <summary>
 66    /// Gets the current raw (string) value for the specified key, or null if the setting is not set.
 67    /// </summary>
 68    /// <param name="key">A key identifying the setting whose value is to be retrieved.</param>
 69    /// <returns>The setting value, or null if the setting is not set.</returns>
 70
 71    public string? GetRawValue(string key)
 72    {
 273        return _rawValues.TryGetValue(key, out string? value) ? value : null;
 74    }
 75    /// <summary>
 76    /// Gets the current typed value for the setting with the specified key, or null if the setting is not set.
 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.</returns>
 80    public object? GetTypedValue(string key)
 81    {
 282        return _typedValues.TryGetValue(key, out object? value) ? value : null;
 83    }
 84    /// <summary>
 85    /// Gets whether or not the settings set is mutable.
 86    /// </summary>
 287    public bool SettingsAreMutable => false;
 88    /// <summary>
 89    /// Changes the specified setting, if possible.
 90    /// For many ambient settings services, the value will only be reflected in memory until the process shuts down, but
 91    /// </summary>
 92    /// <param name="key">A string that uniquely identifies the setting.</param>
 93    /// <param name="value">The new string value for the setting, or null if the setting should be removed.</param>
 94    /// <returns>Whether or not the setting actually changed (it may have had already the same value).</returns>
 295    public bool ChangeSetting(string key, string? value) => throw new InvalidOperationException($"{nameof(AmbientImmutab
 96
 97    /// <summary>
 98    /// Gets a string representing the settings instance.
 99    /// </summary>
 100    /// <returns></returns>
 101    public override string ToString()
 102    {
 2103        return "ImmutableSettings: " + SetName;
 104    }
 105}