-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadOnlyChildren.cs
111 lines (88 loc) · 2.25 KB
/
ReadOnlyChildren.cs
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Grammophone.GenericContentModel
{
/// <summary>
/// An implementation of <see cref="IReadOnlyChildren{P, C}"/>,
/// an immmutable collection of 'child' objects belonging to a 'parent'.
/// </summary>
/// <typeparam name="P">The type of the parent.</typeparam>
/// <typeparam name="C">The type of the children.</typeparam>
[Serializable]
public class ReadOnlyChildren<P, C> : ReadOnlyBag<C>, IReadOnlyChildren<P, C>, IOwnedObject<P>
where C : class, IChild<P>
where P : class
{
#region Protected fields
/// <summary>
/// The parent owning this collection.
/// </summary>
protected P owner;
#endregion
#region Construction
/// <summary>
/// Create an empty collection with no owner.
/// </summary>
public ReadOnlyChildren()
: this(null)
{
}
/// <summary>
/// Create.
/// </summary>
/// <param name="owner">Optional owner of this collection, else null.</param>
/// <param name="children">The optional set of children to be contained, else empty set if null.</param>
public ReadOnlyChildren(P owner, IEnumerable<C> children = null)
: base(children)
{
this.owner = owner;
foreach (var child in this)
{
child.Parent = owner;
}
}
#endregion
#region IReadOnlyChildren<P,C> Members
/// <summary>
/// The owner of the collection of children.
/// </summary>
public P Owner
{
get { return this.owner; }
}
#endregion
#region IOwnedObject<P> Members
P IOwnedObject<P>.Owner
{
get
{
return this.owner;
}
set
{
this.owner = value;
foreach (var item in this.collection)
{
item.Parent = value;
}
}
}
#endregion
#region Internal methods
/// <summary>
/// Called when an item is added to the collection.
/// </summary>
/// <param name="element">The added element.</param>
/// <returns>Returns true if the element was not already in the collection and added successfully, else false.</returns>
internal protected override bool AddItem(C element)
{
if (element == null) throw new ArgumentNullException("element");
if (!base.AddItem(element)) return false;
element.Parent = this.Owner;
return true;
}
#endregion
}
}