forked from JSONAPIdotNET/JSONAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRelationAggregator.cs
More file actions
68 lines (59 loc) · 2.15 KB
/
Copy pathRelationAggregator.cs
File metadata and controls
68 lines (59 loc) · 2.15 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JSONAPI.Json
{
public class RelationAggregator
{
private Type itemType = null;
private ISet<object> rootItems = null;
internal Dictionary<Type, ISet<object>> Appendices;
public RelationAggregator()
{
this.Appendices = new Dictionary<Type, ISet<object>>();
}
public void AddPrimary(Type type, IEnumerable<object> items)
{
if (itemType == null) itemType = type;
if (rootItems == null)
rootItems = new HashSet<object>();
rootItems.UnionWith(items);
}
public void AddPrimary(Type type, object item)
{
if (itemType == null) itemType = type;
if (rootItems == null)
rootItems = new HashSet<object>();
rootItems.Add(item);
}
public void Add(Type type, IEnumerable<object> items)
{
// Exclude items that are already included in the root!
items = items.Except(this.rootItems);
if (items.Count() <= 0) return;
if (!this.Appendices.ContainsKey(type))
//TODO: Can we make a strongly-typed collection here somehow, since we know the type?
this.Appendices[type] = new HashSet<object>();
this.Appendices[type].UnionWith(items);
/* Assuming the above is faster than this...
foreach (object item in items)
{
currentPayload.Appendices[prop.GetType()].Add(item);
}
*/
}
public void Add(Type type, object item)
{
// Exclude items that are already included in the root!
if (item.GetType() == itemType)
{
if (this.rootItems.Contains(item)) return;
}
if (!this.Appendices.ContainsKey(type))
this.Appendices[type] = new HashSet<object>();
this.Appendices[type].Add(item);
}
}
}