ProductCollection.cs
2.19 KB
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
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;
}
}
}