aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Data/WindowsInstaller/TableIndexedCollection.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/WixToolset.Data/WindowsInstaller/TableIndexedCollection.cs')
-rw-r--r--src/WixToolset.Data/WindowsInstaller/TableIndexedCollection.cs143
1 files changed, 143 insertions, 0 deletions
diff --git a/src/WixToolset.Data/WindowsInstaller/TableIndexedCollection.cs b/src/WixToolset.Data/WindowsInstaller/TableIndexedCollection.cs
new file mode 100644
index 00000000..1b7de72b
--- /dev/null
+++ b/src/WixToolset.Data/WindowsInstaller/TableIndexedCollection.cs
@@ -0,0 +1,143 @@
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
3namespace 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()
19 {
20 this.collection = new Dictionary<string,Table>();
21 }
22
23 /// <summary>
24 /// Instantiate a new collection populated with a set of tables.
25 /// </summary>
26 /// <param name="tables">Set of tables.</param>
27 public TableIndexedCollection(IEnumerable<Table> tables)
28 {
29 this.collection = tables.ToDictionary(t => t.Name);
30 }
31
32 /// <summary>
33 /// Gets the number of items in the collection.
34 /// </summary>
35 /// <value>Number of items in collection.</value>
36 public int Count => this.collection.Count;
37
38 /// <summary>
39 /// Table indexed collection is never read only.
40 /// </summary>
41 public bool IsReadOnly => false;
42
43 /// <summary>
44 /// Adds a table to the collection.
45 /// </summary>
46 /// <param name="table">Table to add to the collection.</param>
47 /// <remarks>Indexes the table by name.</remarks>
48 public void Add(Table table)
49 {
50 this.collection.Add(table.Name, table);
51 }
52
53 /// <summary>
54 /// Clear the tables from the collection.
55 /// </summary>
56 public void Clear()
57 {
58 this.collection.Clear();
59 }
60
61 /// <summary>
62 /// Determines if a table is in the collection.
63 /// </summary>
64 /// <param name="table">Table to check if it is in the collection.</param>
65 /// <returns>True if the table name is in the collection, otherwise false.</returns>
66 public bool Contains(Table table) => this.collection.ContainsKey(table.Name);
67
68 /// <summary>
69 /// Copies the collection into an array.
70 /// </summary>
71 /// <param name="array">Array to copy the collection into.</param>
72 /// <param name="arrayIndex">Index to start copying from.</param>
73 public void CopyTo(Table[] array, int arrayIndex)
74 {
75 this.collection.Values.CopyTo(array, arrayIndex);
76 }
77
78 /// <summary>
79 /// Remove a table from the collection by name.
80 /// </summary>
81 /// <param name="tableName">Table name to remove from the collection.</param>
82 public void Remove(string tableName)
83 {
84 this.collection.Remove(tableName);
85 }
86
87 /// <summary>
88 /// Remove a table from the collection.
89 /// </summary>
90 /// <param name="table">Table with matching name to remove from the collection.</param>
91 public bool Remove(Table table)
92 {
93 return this.collection.Remove(table.Name);
94 }
95
96 /// <summary>
97 /// Gets an enumerator over the whole collection.
98 /// </summary>
99 /// <returns>Collection enumerator.</returns>
100 public IEnumerator<Table> GetEnumerator()
101 {
102 return this.collection.Values.GetEnumerator();
103 }
104
105 /// <summary>
106 /// Gets an untyped enumerator over the whole collection.
107 /// </summary>
108 /// <returns>Untyped collection enumerator.</returns>
109 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
110 {
111 return this.collection.Values.GetEnumerator();
112 }
113
114 /// <summary>
115 /// Gets a table by name.
116 /// </summary>
117 /// <param name="tableName">Name of table to locate.</param>
118 public Table this[string tableName]
119 {
120 get
121 {
122 Table table;
123 return this.collection.TryGetValue(tableName, out table) ? table : null;
124 }
125
126 set
127 {
128 this.collection[tableName] = value;
129 }
130 }
131
132 /// <summary>
133 /// Tries to find a table by name.
134 /// </summary>
135 /// <param name="tableName">Table name to locate.</param>
136 /// <param name="table">Found table.</param>
137 /// <returns>True if table with table name was found, otherwise false.</returns>
138 public bool TryGetTable(string tableName, out Table table)
139 {
140 return this.collection.TryGetValue(tableName, out table);
141 }
142 }
143}