aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Data.WindowsInstaller/TableIndexedCollection.cs
blob: 9f85efffd186a5f76475ca051b49a197a1bd5298 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// 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.Data
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    /// <summary>
    /// Collection for tables.
    /// </summary>
    public sealed class TableIndexedCollection : ICollection<Table>
    {
        private Dictionary<string, Table> collection;

        /// <summary>
        /// Instantiate a new empty collection.
        /// </summary>
        public TableIndexedCollection()
        {
            this.collection = new Dictionary<string,Table>();
        }

        /// <summary>
        /// Instantiate a new collection populated with a set of tables.
        /// </summary>
        /// <param name="tables">Set of tables.</param>
        public TableIndexedCollection(IEnumerable<Table> tables)
        {
            this.collection = tables.ToDictionary(t => t.Name);
        }

        /// <summary>
        /// Gets the number of items in the collection.
        /// </summary>
        /// <value>Number of items in collection.</value>
        public int Count
        {
            get { return this.collection.Count; }
        }

        /// <summary>
        /// Table indexed collection is never read only.
        /// </summary>
        public bool IsReadOnly
        {
            get { return false; }
        }

        /// <summary>
        /// Adds a table to the collection.
        /// </summary>
        /// <param name="table">Table to add to the collection.</param>
        /// <remarks>Indexes the table by name.</remarks>
        public void Add(Table table)
        {
            this.collection.Add(table.Name, table);
        }

        /// <summary>
        /// Clear the tables from the collection.
        /// </summary>
        public void Clear()
        {
            this.collection.Clear();
        }

        /// <summary>
        /// Determines if a table is in the collection.
        /// </summary>
        /// <param name="table">Table to check if it is in the collection.</param>
        /// <returns>True if the table name is in the collection, otherwise false.</returns>
        public bool Contains(Table table)
        {
            return this.collection.ContainsKey(table.Name);
        }

        /// <summary>
        /// Copies the collection into an array.
        /// </summary>
        /// <param name="array">Array to copy the collection into.</param>
        /// <param name="arrayIndex">Index to start copying from.</param>
        public void CopyTo(Table[] array, int arrayIndex)
        {
            this.collection.Values.CopyTo(array, arrayIndex);
        }

        /// <summary>
        /// Remove a table from the collection by name.
        /// </summary>
        /// <param name="tableName">Table name to remove from the collection.</param>
        public void Remove(string tableName)
        {
            this.collection.Remove(tableName);
        }

        /// <summary>
        /// Remove a table from the collection.
        /// </summary>
        /// <param name="table">Table with matching name to remove from the collection.</param>
        public bool Remove(Table table)
        {
            return this.collection.Remove(table.Name);
        }

        /// <summary>
        /// Gets an enumerator over the whole collection.
        /// </summary>
        /// <returns>Collection enumerator.</returns>
        public IEnumerator<Table> GetEnumerator()
        {
            return this.collection.Values.GetEnumerator();
        }

        /// <summary>
        /// Gets an untyped enumerator over the whole collection.
        /// </summary>
        /// <returns>Untyped collection enumerator.</returns>
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.collection.Values.GetEnumerator();
        }

        /// <summary>
        /// Gets a table by name.
        /// </summary>
        /// <param name="tableName">Name of table to locate.</param>
        public Table this[string tableName]
        {
            get
            {
                Table table;
                return this.collection.TryGetValue(tableName, out table) ? table : null;
            }

            set
            {
                this.collection[tableName] = value;
            }
        }

        /// <summary>
        /// Tries to find a table by name.
        /// </summary>
        /// <param name="tableName">Table name to locate.</param>
        /// <param name="table">Found table.</param>
        /// <returns>True if table with table name was found, otherwise false.</returns>
        public bool TryGetTable(string tableName, out Table table)
        {
            return this.collection.TryGetValue(tableName, out table);
        }
    }
}