| 1 | // 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. |
| 2 | |
| 3 | namespace WixToolset.Core.WindowsInstaller |
| 4 | { |
| 5 | using System; |
| 6 | using System.Collections.Generic; |
| 7 | using WixToolset.Data.WindowsInstaller; |
| 8 | |
| 9 | /// <summary> |
| 10 | /// A dictionary of rows. Unlike the RowIndexedList this |
| 11 | /// will throw when multiple rows with the same key are added. |
| 12 | /// </summary> |
| 13 | internal sealed class RowDictionary<T> : Dictionary<string, T> where T : Row |
| 14 | { |
| 15 | /// <summary> |
| 16 | /// Creates an empty <see cref="RowDictionary{T}"/>. |
| 17 | /// </summary> |
| 18 | public RowDictionary() |
| 19 | : base(StringComparer.InvariantCulture) |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | /// <summary> |
| 24 | /// Creates and populates a <see cref="RowDictionary{T}"/> with the rows from the given <see cref="Table"/>. |
| 25 | /// </summary> |
| 26 | /// <param name="table">The table to index.</param> |
| 27 | /// <remarks> |
| 28 | /// Rows added to the index are not automatically added to the given <paramref name="table"/>. |
| 29 | /// </remarks> |
| 30 | public RowDictionary(Table table) |
| 31 | : this() |
| 32 | { |
| 33 | if (null != table) |
| 34 | { |
| 35 | foreach (T row in table.Rows) |
| 36 | { |
| 37 | this.Add(row); |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /// <summary> |
| 43 | /// Adds a row to the dictionary using the row key. |
| 44 | /// </summary> |
| 45 | /// <param name="row">Row to add to the dictionary.</param> |
| 46 | public void Add(T row) |
| 47 | { |
| 48 | this.Add(row.GetKey(), row); |
| 49 | } |
| 50 | |
| 51 | /// <summary> |
| 52 | /// Gets the row by integer key. |
| 53 | /// </summary> |
| 54 | /// <param name="key">Integer key to look up.</param> |
| 55 | /// <returns>Row or null if key is not found.</returns> |
| 56 | public T Get(int key) |
| 57 | { |
| 58 | return this.Get(key.ToString()); |
| 59 | } |
| 60 | |
| 61 | /// <summary> |
| 62 | /// Gets the row by string key. |
| 63 | /// </summary> |
| 64 | /// <param name="key">String key to look up.</param> |
| 65 | /// <returns>Row or null if key is not found.</returns> |
| 66 | public T Get(string key) |
| 67 | { |
| 68 | return this.TryGetValue(key, out var result) ? result : null; |
| 69 | } |
| 70 | } |
| 71 | } |