forked from ical-org/ical.net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroupedValueList.cs
More file actions
47 lines (42 loc) · 1.48 KB
/
GroupedValueList.cs
File metadata and controls
47 lines (42 loc) · 1.48 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
using System;
using System.Collections.Generic;
using System.Linq;
using Ical.Net.Collections.Interfaces;
using Ical.Net.Collections.Proxies;
namespace Ical.Net.Collections
{
public class GroupedValueList<TGroup, TInterface, TItem, TValueType> :
GroupedList<TGroup, TInterface>
where TInterface : class, IGroupedObject<TGroup>, IValueObject<TValueType>
where TItem : new()
{
public void Set(TGroup group, TValueType value) => Set(group, new[] { value });
public void Set(TGroup group, IEnumerable<TValueType> values)
{
if (ContainsKey(group))
{
AllOf(group)?.FirstOrDefault()?.SetValue(values);
return;
}
// No matching item was found, add a new item to the list
var obj = Activator.CreateInstance(typeof(TItem)) as TInterface;
obj.Group = group;
Add(obj);
obj.SetValue(values);
}
public TType Get<TType>(TGroup group)
{
var firstItem = AllOf(group).FirstOrDefault();
if (firstItem?.Values != null)
{
return firstItem
.Values
.OfType<TType>()
.FirstOrDefault();
}
return default;
}
public IList<TType> GetMany<TType>(TGroup group)
=> new GroupedValueListProxy<TGroup, TInterface, TItem, TValueType, TType>(this, group);
}
}