// 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.WindowsInstaller
{
using System.Collections.Generic;
using System.Linq;
///
/// Collection for tables.
///
public sealed class TableIndexedCollection : ICollection
{
private Dictionary collection;
///
/// Instantiate a new empty collection.
///
public TableIndexedCollection() => this.collection = new Dictionary();
///
/// Instantiate a new collection populated with a set of tables.
///
/// Set of tables.
public TableIndexedCollection(IEnumerable tables) => this.collection = tables.ToDictionary(t => t.Name);
///
/// Gets the number of items in the collection.
///
/// Number of items in collection.
public int Count => this.collection.Count;
///
/// Table indexed collection is never read only.
///
public bool IsReadOnly => false;
///
/// Adds a table to the collection.
///
/// Table to add to the collection.
/// Indexes the table by name.
public void Add(Table table) => this.collection.Add(table.Name, table);
///
/// Clear the tables from the collection.
///
public void Clear() => this.collection.Clear();
///
/// Determines if a table is in the collection.
///
/// Table to check if it is in the collection.
/// True if the table name is in the collection, otherwise false.
public bool Contains(Table table) => this.collection.ContainsKey(table.Name);
///
/// Copies the collection into an array.
///
/// Array to copy the collection into.
/// Index to start copying from.
public void CopyTo(Table[] array, int arrayIndex) => this.collection.Values.CopyTo(array, arrayIndex);
///
/// Remove a table from the collection by name.
///
/// Table name to remove from the collection.
public void Remove(string tableName) => _ = this.collection.Remove(tableName);
///
/// Remove a table from the collection.
///
/// Table with matching name to remove from the collection.
public bool Remove(Table table) => this.collection.Remove(table.Name);
///
/// Gets an enumerator over the whole collection.
///
/// Collection enumerator.
public IEnumerator GetEnumerator() => this.collection.Values.GetEnumerator();
///
/// Gets an untyped enumerator over the whole collection.
///
/// Untyped collection enumerator.
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => this.collection.Values.GetEnumerator();
///
/// Gets a table by name.
///
/// Name of table to locate.
public Table this[string tableName]
{
get => this.collection.TryGetValue(tableName, out var table) ? table : null;
set => this.collection[tableName] = value;
}
///
/// Tries to find a table by name.
///
/// Table name to locate.
/// Found table.
/// True if table with table name was found, otherwise false.
public bool TryGetTable(string tableName, out Table table) => this.collection.TryGetValue(tableName, out table);
}
}