aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core.WindowsInstaller/RowDictionary.cs
blob: 101ebefd600bf54410aee9bd69acf40e049b8f86 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// 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.Core.WindowsInstaller
{
    using System;
    using System.Collections.Generic;
    using WixToolset.Data.WindowsInstaller;

    /// <summary>
    /// A dictionary of rows. Unlike the <see cref="RowIndexedCollection"/> this
    /// will throw when multiple rows with the same key are added.
    /// </summary>
    public sealed class RowDictionary<T> : Dictionary<string, T> where T : Row
    {
        /// <summary>
        /// Creates an empty <see cref="RowDictionary"/>.
        /// </summary>
        public RowDictionary()
            : base(StringComparer.InvariantCulture)
        {
        }

        /// <summary>
        /// Creates and populates a <see cref="RowDictionary"/> with the rows from the given enumerator.
        /// </summary>
        /// <param name="Rows">Rows to add.</param>
        public RowDictionary(IEnumerable<T> rows)
            : this()
        {
            foreach (T row in rows)
            {
                this.Add(row);
            }
        }

        /// <summary>
        /// Creates and populates a <see cref="RowDictionary"/> with the rows from the given <see cref="Table"/>.
        /// </summary>
        /// <param name="table">The table to index.</param>
        /// <remarks>
        /// Rows added to the index are not automatically added to the given <paramref name="table"/>.
        /// </remarks>
        public RowDictionary(Table table)
            : this()
        {
            if (null != table)
            {
                foreach (T row in table.Rows)
                {
                    this.Add(row);
                }
            }
        }

        /// <summary>
        /// Adds a row to the dictionary using the row key.
        /// </summary>
        /// <param name="row">Row to add to the dictionary.</param>
        public void Add(T row)
        {
            this.Add(row.GetKey(), row);
        }

        /// <summary>
        /// Gets the row by integer key.
        /// </summary>
        /// <param name="key">Integer key to look up.</param>
        /// <returns>Row or null if key is not found.</returns>
        public T Get(int key)
        {
            return this.Get(key.ToString());
        }

        /// <summary>
        /// Gets the row by string key.
        /// </summary>
        /// <param name="key">String key to look up.</param>
        /// <returns>Row or null if key is not found.</returns>
        public T Get(string key)
        {
            return this.TryGetValue(key, out var result) ? result : null;
        }
    }
}