aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core/ProvidesDependencyCollection.cs
blob: a777afb031d6903c519b4642d6a2a199b8a563c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

namespace WixToolset
{
    using System;
    using System.Collections.ObjectModel;

    /// <summary>
    /// A case-insensitive collection of unique <see cref="ProvidesDependency"/> objects.
    /// </summary>
    internal sealed class ProvidesDependencyCollection : KeyedCollection<string, ProvidesDependency>
    {
        /// <summary>
        /// Creates a case-insensitive collection of unique <see cref="ProvidesDependency"/> objects.
        /// </summary>
        internal ProvidesDependencyCollection()
            : base(StringComparer.InvariantCultureIgnoreCase)
        {
        }

        /// <summary>
        /// Adds the <see cref="ProvidesDependency"/> to the collection if it doesn't already exist.
        /// </summary>
        /// <param name="dependency">The <see cref="ProvidesDependency"/> to add to the collection.</param>
        /// <returns>True if the <see cref="ProvidesDependency"/> was added to the collection; otherwise, false.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="dependency"/> parameter is null.</exception>
        internal bool Merge(ProvidesDependency dependency)
        {
            if (null == dependency)
            {
                throw new ArgumentNullException("dependency");
            }

            // If the dependency key is already in the collection, verify equality for a subset of properties.
            if (this.Contains(dependency.Key))
            {
                ProvidesDependency current = this[dependency.Key];
                if (!current.Equals(dependency))
                {
                    return false;
                }
            }

            base.Add(dependency);
            return true;
        }

        /// <summary>
        /// Gets the <see cref="ProvidesDependency.Key"/> for the <paramref name="dependency"/>.
        /// </summary>
        /// <param name="dependency">The dependency to index.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="dependency"/> parameter is null.</exception>
        /// <returns>The <see cref="ProvidesDependency.Key"/> for the <paramref name="dependency"/>.</returns>
        protected override string GetKeyForItem(ProvidesDependency dependency)
        {
            if (null == dependency)
            {
                throw new ArgumentNullException("dependency");
            }

            return dependency.Key;
        }
    }
}