| 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.WindowsInstaller |
| 4 | { |
| 5 | using System.Collections.Generic; |
| 6 | using System.Linq; |
| 7 | |
| 8 | /// <summary> |
| 9 | /// Collection for tables. |
| 10 | /// </summary> |
| 11 | public sealed class TableIndexedCollection : ICollection<Table> |
| 12 | { |
| 13 | private Dictionary<string, Table> collection; |
| 14 | |
| 15 | /// <summary> |
| 16 | /// Instantiate a new empty collection. |
| 17 | /// </summary> |
| 18 | public TableIndexedCollection() => this.collection = new Dictionary<string, Table>(); |
| 19 | |
| 20 | /// <summary> |
| 21 | /// Instantiate a new collection populated with a set of tables. |
| 22 | /// </summary> |
| 23 | /// <param name="tables">Set of tables.</param> |
| 24 | public TableIndexedCollection(IEnumerable<Table> tables) => this.collection = tables.ToDictionary(t => t.Name); |
| 25 | |
| 26 | /// <summary> |
| 27 | /// Gets the number of items in the collection. |
| 28 | /// </summary> |
| 29 | /// <value>Number of items in collection.</value> |
| 30 | public int Count => this.collection.Count; |
| 31 | |
| 32 | /// <summary> |
| 33 | /// Table indexed collection is never read only. |
| 34 | /// </summary> |
| 35 | public bool IsReadOnly => false; |
| 36 | |
| 37 | /// <summary> |
| 38 | /// Adds a table to the collection. |
| 39 | /// </summary> |
| 40 | /// <param name="table">Table to add to the collection.</param> |
| 41 | /// <remarks>Indexes the table by name.</remarks> |
| 42 | public void Add(Table table) => this.collection.Add(table.Name, table); |
| 43 | |
| 44 | /// <summary> |
| 45 | /// Clear the tables from the collection. |
| 46 | /// </summary> |
| 47 | public void Clear() => this.collection.Clear(); |
| 48 | |
| 49 | /// <summary> |
| 50 | /// Determines if a table is in the collection. |
| 51 | /// </summary> |
| 52 | /// <param name="table">Table to check if it is in the collection.</param> |
| 53 | /// <returns>True if the table name is in the collection, otherwise false.</returns> |
| 54 | public bool Contains(Table table) => this.collection.ContainsKey(table.Name); |
| 55 | |
| 56 | /// <summary> |
| 57 | /// Copies the collection into an array. |
| 58 | /// </summary> |
| 59 | /// <param name="array">Array to copy the collection into.</param> |
| 60 | /// <param name="arrayIndex">Index to start copying from.</param> |
| 61 | public void CopyTo(Table[] array, int arrayIndex) => this.collection.Values.CopyTo(array, arrayIndex); |
| 62 | |
| 63 | /// <summary> |
| 64 | /// Remove a table from the collection by name. |
| 65 | /// </summary> |
| 66 | /// <param name="tableName">Table name to remove from the collection.</param> |
| 67 | public void Remove(string tableName) => _ = this.collection.Remove(tableName); |
| 68 | |
| 69 | /// <summary> |
| 70 | /// Remove a table from the collection. |
| 71 | /// </summary> |
| 72 | /// <param name="table">Table with matching name to remove from the collection.</param> |
| 73 | public bool Remove(Table table) => this.collection.Remove(table.Name); |
| 74 | |
| 75 | /// <summary> |
| 76 | /// Gets an enumerator over the whole collection. |
| 77 | /// </summary> |
| 78 | /// <returns>Collection enumerator.</returns> |
| 79 | public IEnumerator<Table> GetEnumerator() => this.collection.Values.GetEnumerator(); |
| 80 | |
| 81 | /// <summary> |
| 82 | /// Gets an untyped enumerator over the whole collection. |
| 83 | /// </summary> |
| 84 | /// <returns>Untyped collection enumerator.</returns> |
| 85 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => this.collection.Values.GetEnumerator(); |
| 86 | |
| 87 | /// <summary> |
| 88 | /// Gets a table by name. |
| 89 | /// </summary> |
| 90 | /// <param name="tableName">Name of table to locate.</param> |
| 91 | public Table this[string tableName] |
| 92 | { |
| 93 | get => this.collection.TryGetValue(tableName, out var table) ? table : null; |
| 94 | set => this.collection[tableName] = value; |
| 95 | } |
| 96 | |
| 97 | /// <summary> |
| 98 | /// Tries to find a table by name. |
| 99 | /// </summary> |
| 100 | /// <param name="tableName">Table name to locate.</param> |
| 101 | /// <param name="table">Found table.</param> |
| 102 | /// <returns>True if table with table name was found, otherwise false.</returns> |
| 103 | public bool TryGetTable(string tableName, out Table table) => this.collection.TryGetValue(tableName, out table); |
| 104 | } |
| 105 | } |