forked from ical-org/ical.net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroupedCollectionProxy.cs
More file actions
92 lines (69 loc) · 2.84 KB
/
GroupedCollectionProxy.cs
File metadata and controls
92 lines (69 loc) · 2.84 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Ical.Net.Collections.Proxies
{
/// <summary>
/// A proxy for a keyed list.
/// </summary>
public class GroupedCollectionProxy<TGroup, TOriginal, TNew> :
IGroupedCollection<TGroup, TNew>
where TOriginal : class, IGroupedObject<TGroup>
where TNew : class, TOriginal
{
readonly Func<TNew, bool> _predicate;
public GroupedCollectionProxy(IGroupedCollection<TGroup, TOriginal> realObject, Func<TNew, bool> predicate = null)
{
_predicate = predicate ?? (o => true);
SetProxiedObject(realObject);
}
public event EventHandler<ObjectEventArgs<TNew, int>> ItemAdded;
public event EventHandler<ObjectEventArgs<TNew, int>> ItemRemoved;
protected void OnItemAdded(TNew item, int index) => ItemAdded?.Invoke(this, new ObjectEventArgs<TNew, int>(item, index));
protected void OnItemRemoved(TNew item, int index) => ItemRemoved?.Invoke(this, new ObjectEventArgs<TNew, int>(item, index));
public bool Remove(TGroup group) => RealObject.Remove(group);
public void Clear(TGroup group) => RealObject.Clear(group);
public bool ContainsKey(TGroup group) => RealObject.ContainsKey(group);
public int CountOf(TGroup group) => RealObject.OfType<TGroup>().Count();
public IEnumerable<TNew> AllOf(TGroup group) => RealObject
.AllOf(group)
.OfType<TNew>()
.Where(_predicate);
public void Add(TNew item) => RealObject.Add(item);
public void Clear()
{
// Only clear items of this type
// that match the predicate.
var items = RealObject
.OfType<TNew>()
.ToArray();
foreach (var item in items)
{
RealObject.Remove(item);
}
}
public bool Contains(TNew item) => RealObject.Contains(item);
public void CopyTo(TNew[] array, int arrayIndex)
{
var i = 0;
foreach (var item in this)
{
array[arrayIndex + (i++)] = item;
}
}
public int Count => RealObject
.OfType<TNew>()
.Count();
public bool IsReadOnly => false;
public bool Remove(TNew item) => RealObject.Remove(item);
public IEnumerator<TNew> GetEnumerator() => RealObject
.OfType<TNew>()
.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => RealObject
.OfType<TNew>()
.GetEnumerator();
public IGroupedCollection<TGroup, TOriginal> RealObject { get; private set; }
public void SetProxiedObject(IGroupedCollection<TGroup, TOriginal> realObject) => RealObject = realObject;
}
}