// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
namespace Microsoft.CodeAnalysis.Options
{
///
/// An option that can be specified once per language.
///
///
public class PerLanguageOption : IOption2, IOption
{
///
/// Save per language defaults
///
private readonly IImmutableDictionary _perLanguageDefaults;
///
/// Feature this option is associated with.
///
public string Feature { get; }
///
/// The name of the option.
///
public string Name { get; }
///
/// The type of the option value.
///
public Type Type
{
get { return typeof(T); }
}
///
/// The default option value.
///
public T DefaultValue { get; }
///
/// The default option value of specific language
///
internal T GetDefaultValue(string language)
{
if (_perLanguageDefaults.Count == 0)
{
return DefaultValue;
}
T languageSpecificDefault;
if (!_perLanguageDefaults.TryGetValue(language, out languageSpecificDefault))
{
return DefaultValue;
}
return languageSpecificDefault;
}
public PerLanguageOption(string feature, string name, T defaultValue) :
this(feature, name, defaultValue, ImmutableDictionary.Empty)
{
}
internal PerLanguageOption(
string feature, string name, T defaultValue, IDictionary perLanguageDefaults)
{
if (string.IsNullOrWhiteSpace(feature))
{
throw new ArgumentNullException(nameof(feature));
}
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException(nameof(name));
}
if (perLanguageDefaults == null)
{
throw new ArgumentNullException(nameof(perLanguageDefaults));
}
this.Feature = feature;
this.Name = name;
this.DefaultValue = defaultValue;
this._perLanguageDefaults =
(perLanguageDefaults as IImmutableDictionary) ??
ImmutableDictionary.CreateRange(perLanguageDefaults);
}
Type IOption.Type
{
get { return typeof(T); }
}
object IOption.DefaultValue
{
get { return this.DefaultValue; }
}
bool IOption.IsPerLanguage
{
get { return true; }
}
object IOption2.GetDefaultValue(string language)
{
return this.GetDefaultValue(language);
}
public override string ToString()
{
return string.Format("{0} - {1}", this.Feature, this.Name);
}
}
}