using System;
using System.Collections.Generic;
using System.Linq;
namespace UnityEngine.Purchasing
{
///
/// Provides helper methods to retrieve products by
/// store independent/store specific id.
///
public class ProductCollection
{
private Dictionary m_IdToProduct;
private Dictionary m_StoreSpecificIdToProduct;
private Product[] m_Products;
private HashSet m_ProductSet = new HashSet();
internal ProductCollection(Product[] products)
{
AddProducts(products);
}
internal void AddProducts(IEnumerable products)
{
m_ProductSet.UnionWith(products);
m_Products = m_ProductSet.ToArray();
m_IdToProduct = m_Products.ToDictionary(x => x.definition.id);
m_StoreSpecificIdToProduct = m_Products.ToDictionary(x => x.definition.storeSpecificId);
}
///
/// The hash set of all products
///
public HashSet set
{
get { return m_ProductSet; }
}
///
/// The array of all products
///
public Product[] all
{
get { return m_Products; }
}
///
/// Gets a product matching an id
///
/// The id of the desired product
/// The product matching the id, or null if not found
public Product WithID(string id)
{
Product result = null;
m_IdToProduct.TryGetValue(id, out result);
return result;
}
///
/// Gets a product matching a store-specific id
///
/// The store-specific id of the desired product
/// The product matching the id, or null if not found
public Product WithStoreSpecificID(string id)
{
Product result = null;
if (id != null)
{
m_StoreSpecificIdToProduct.TryGetValue(id, out result);
}
return result;
}
}
}