Game/Library/PackageCache/com.unity.purchasing@2.1.1/Runtime/managed/Purchasing/ProductCollection.cs
2021-01-16 18:29:12 +01:00

72 lines
2.2 KiB
C#

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