OptionKey.cs 2.0 KB
Newer Older
1
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
P
Pilchie 已提交
2 3 4 5 6 7 8 9

using System;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Options
{
    public struct OptionKey : IEquatable<OptionKey>
    {
10 11
        public IOption Option { get; }
        public string Language { get; }
P
Pilchie 已提交
12

13 14
        internal object DefaultValue => ((IOption2)Option).GetDefaultValue(Language);

P
Pilchie 已提交
15 16 17 18
        public OptionKey(IOption option, string language = null)
        {
            if (option == null)
            {
19
                throw new ArgumentNullException(nameof(option));
P
Pilchie 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
            }

            if (language != null && !option.IsPerLanguage)
            {
                throw new ArgumentException(WorkspacesResources.InvalidLanguageNameOption);
            }
            else if (language == null && option.IsPerLanguage)
            {
                throw new ArgumentNullException(WorkspacesResources.InvalidLanguageNameOption2);
            }

            this.Option = option;
            this.Language = language;
        }

        public override bool Equals(object obj)
        {
            if (obj is OptionKey)
            {
                return Equals((OptionKey)obj);
            }

            return false;
        }

        public bool Equals(OptionKey other)
        {
            return Option == other.Option && Language == other.Language;
        }

        public override int GetHashCode()
        {
            var hash = Option.GetHashCode();

            if (Language != null)
            {
                hash = Hash.Combine(Language.GetHashCode(), hash);
            }

            return hash;
        }

        public static bool operator ==(OptionKey left, OptionKey right)
        {
            return left.Equals(right);
        }

        public static bool operator !=(OptionKey left, OptionKey right)
        {
            return !left.Equals(right);
        }
    }
}