diff options
Diffstat (limited to 'src/WixToolset.Data.WindowsInstaller/RowDictionary.cs')
| -rw-r--r-- | src/WixToolset.Data.WindowsInstaller/RowDictionary.cs | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/src/WixToolset.Data.WindowsInstaller/RowDictionary.cs b/src/WixToolset.Data.WindowsInstaller/RowDictionary.cs new file mode 100644 index 00000000..a0cc5302 --- /dev/null +++ b/src/WixToolset.Data.WindowsInstaller/RowDictionary.cs | |||
| @@ -0,0 +1,84 @@ | |||
| 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.Data | ||
| 4 | { | ||
| 5 | using System; | ||
| 6 | using System.Collections.Generic; | ||
| 7 | |||
| 8 | /// <summary> | ||
| 9 | /// A dictionary of rows. Unlike the <see cref="RowIndexedCollection"/> this | ||
| 10 | /// will throw when multiple rows with the same key are added. | ||
| 11 | /// </summary> | ||
| 12 | public sealed class RowDictionary<T> : Dictionary<string, T> where T : Row | ||
| 13 | { | ||
| 14 | /// <summary> | ||
| 15 | /// Creates an empty <see cref="RowDictionary"/>. | ||
| 16 | /// </summary> | ||
| 17 | public RowDictionary() | ||
| 18 | : base(StringComparer.InvariantCulture) | ||
| 19 | { | ||
| 20 | } | ||
| 21 | |||
| 22 | /// <summary> | ||
| 23 | /// Creates and populates a <see cref="RowDictionary"/> with the rows from the given enumerator. | ||
| 24 | /// </summary> | ||
| 25 | /// <param name="Rows">Rows to add.</param> | ||
| 26 | public RowDictionary(IEnumerable<T> rows) | ||
| 27 | : this() | ||
| 28 | { | ||
| 29 | foreach (T row in rows) | ||
| 30 | { | ||
| 31 | this.Add(row); | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | /// <summary> | ||
| 36 | /// Creates and populates a <see cref="RowDictionary"/> with the rows from the given <see cref="Table"/>. | ||
| 37 | /// </summary> | ||
| 38 | /// <param name="table">The table to index.</param> | ||
| 39 | /// <remarks> | ||
| 40 | /// Rows added to the index are not automatically added to the given <paramref name="table"/>. | ||
| 41 | /// </remarks> | ||
| 42 | public RowDictionary(Table table) | ||
| 43 | : this() | ||
| 44 | { | ||
| 45 | if (null != table) | ||
| 46 | { | ||
| 47 | foreach (T row in table.Rows) | ||
| 48 | { | ||
| 49 | this.Add(row); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | /// <summary> | ||
| 55 | /// Adds a row to the dictionary using the row key. | ||
| 56 | /// </summary> | ||
| 57 | /// <param name="row">Row to add to the dictionary.</param> | ||
| 58 | public void Add(T row) | ||
| 59 | { | ||
| 60 | this.Add(row.GetKey(), row); | ||
| 61 | } | ||
| 62 | |||
| 63 | /// <summary> | ||
| 64 | /// Gets the row by integer key. | ||
| 65 | /// </summary> | ||
| 66 | /// <param name="key">Integer key to look up.</param> | ||
| 67 | /// <returns>Row or null if key is not found.</returns> | ||
| 68 | public T Get(int key) | ||
| 69 | { | ||
| 70 | return this.Get(key.ToString()); | ||
| 71 | } | ||
| 72 | |||
| 73 | /// <summary> | ||
| 74 | /// Gets the row by string key. | ||
| 75 | /// </summary> | ||
| 76 | /// <param name="key">String key to look up.</param> | ||
| 77 | /// <returns>Row or null if key is not found.</returns> | ||
| 78 | public T Get(string key) | ||
| 79 | { | ||
| 80 | T result; | ||
| 81 | return this.TryGetValue(key, out result) ? result : null; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | } | ||
