Skip to content

Commit 095d03e

Browse files
committed
Default element equality check uses the ID
1 parent 4060721 commit 095d03e

File tree

4 files changed

+49
-44
lines changed

4 files changed

+49
-44
lines changed

Arrays.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,6 @@ void ICollection.CopyTo(Array array, int index)
136136
CopyTo((T[])array, index);
137137
}
138138

139-
/*object IList.this[int index]
140-
{
141-
get
142-
{
143-
return this[index];
144-
}
145-
set
146-
{
147-
this[index] = (T)value;
148-
}
149-
}
150-
*/
151139
#endregion IList
152140
}
153141

AttributeList.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace Datamodel
1717
/// </summary>
1818
[DebuggerTypeProxy(typeof(DebugView))]
1919
[DebuggerDisplay("Count = {Count}")]
20-
public class AttributeList : IDictionary<string, object>, IDictionary, INotifyCollectionChanged
20+
public class AttributeList : IDictionary<string, object>, IDictionary
2121
{
2222
internal OrderedDictionary PropertyInfos;
2323
internal OrderedDictionary Inner;

Element.cs

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Datamodel
1515
[TypeConverter(typeof(TypeConverters.ElementConverter))]
1616
[DebuggerTypeProxy(typeof(AttributeList.DebugView))]
1717
[DebuggerDisplay("{Name} {ID}", Type = "{ClassName,nq}")]
18-
public class Element : AttributeList, INotifyPropertyChanged, ISupportInitialize
18+
public class Element : AttributeList, IEquatable<Element>
1919
{
2020
#region Constructors and Init
2121

@@ -36,11 +36,11 @@ public Element(Datamodel owner, string name, Guid? id = null, string classNameOv
3636
ClassName = classNameOverride ?? ClassName;
3737

3838
if (id.HasValue)
39-
_ID = id.Value;
39+
ID = id.Value;
4040
else
4141
{
4242
if (!owner.AllowRandomIDs) throw new InvalidOperationException("Random IDs are not allowed in this Datamodel.");
43-
_ID = Guid.NewGuid();
43+
ID = Guid.NewGuid();
4444
}
4545
Owner = owner;
4646
}
@@ -56,7 +56,7 @@ public Element(Datamodel owner, Guid id)
5656
{
5757
ArgumentNullException.ThrowIfNull(owner);
5858

59-
_ID = id;
59+
ID = id;
6060
Stub = true;
6161
Name = "Stub element";
6262
Owner = owner;
@@ -68,7 +68,7 @@ public Element(Datamodel owner, Guid id)
6868
public Element()
6969
: base(null)
7070
{
71-
_ID = Guid.NewGuid();
71+
ID = Guid.NewGuid();
7272

7373
// For subclasses get the actual classname
7474
if (GetType() != typeof(Element))
@@ -84,39 +84,14 @@ public Element()
8484
}
8585
}
8686

87-
bool Initialising = false;
88-
void ISupportInitialize.BeginInit()
89-
{
90-
Initialising = true;
91-
}
92-
93-
void ISupportInitialize.EndInit()
94-
{
95-
if (ID == default)
96-
{
97-
ID = Guid.NewGuid();
98-
}
99-
100-
Initialising = false;
101-
}
102-
10387
#endregion
10488

10589
#region Properties
10690

10791
/// <summary>
10892
/// Gets the ID of this Element. This must be unique within the Element's <see cref="Datamodel"/>.
10993
/// </summary>
110-
public Guid ID
111-
{
112-
get => _ID;
113-
set
114-
{
115-
if (!Initialising && value != _ID) throw new InvalidOperationException("ID can only be changed during initialisation.");
116-
_ID = value;
117-
}
118-
}
119-
Guid _ID;
94+
public Guid ID { get; set; }
12095

12196
/// <summary>
12297
/// Gets or sets the name of this Element.
@@ -402,6 +377,11 @@ public override bool ContainsKey(string key)
402377
if (Stub) throw new InvalidOperationException("Cannot access attributes on a stub element.");
403378
return base.ContainsKey(key);
404379
}
380+
381+
public bool Equals(Element other)
382+
{
383+
return other != null && ID == other.ID;
384+
}
405385
}
406386

407387
namespace TypeConverters

Tests/Tests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,43 @@ protected static void ValidatePopulated(string encoding_name, int encoding_versi
219219
dm.Dispose();
220220
}
221221

222+
[Test]
223+
public static void TypedArrayAddingRemoving()
224+
{
225+
using var dm = MakeDatamodel();
226+
var array = new ElementArray();
227+
228+
var elementA = new Element(dm, "a");
229+
var elementB = new Element();
230+
231+
Assert.False(array.Remove(elementB));
232+
Assert.False(array.Remove(elementA));
233+
234+
dm.Root["a"] = array;
235+
236+
Assert.False(array.Remove(elementB));
237+
Assert.False(array.Remove(elementA));
238+
239+
array.Add(elementB);
240+
Assert.True(array.Remove(elementB));
241+
242+
Assert.False(array.Remove(elementB));
243+
Assert.False(array.Remove(elementA));
244+
245+
((IList)array).Add(elementA);
246+
array.Add(elementB);
247+
248+
array.Add(elementA); // add again?
249+
array.Remove(elementA);
250+
251+
Assert.AreEqual(2, array.Count); // only removes first instance
252+
253+
array.Remove(elementA);
254+
array.Remove(elementB);
255+
256+
Assert.AreEqual(0, array.Count);
257+
}
258+
222259
protected static DM Create(string encoding, int version, bool memory_save = false)
223260
{
224261
var dm = MakeDatamodel();

0 commit comments

Comments
 (0)