Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/NHibernate.Test/CollectionTest/PersistentCollectionsFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Collections.Generic;
using System.Linq;
using NHibernate.Collection.Generic;
using NUnit.Framework;

namespace NHibernate.Test.CollectionTest
{
[TestFixture]
public class PersistentCollectionsFixture
{
[Test]
public void SelectManyWorksCorrectly()
{
var bags = new IEnumerable<string>[]
{
new List<string> {"A"},
new PersistentGenericBag<string>(null, new[] {"B"}),
new PersistentIdentifierBag<string>(null, new[] {"C"}),
new PersistentGenericList<string>(null, new[] {"D"}),
new PersistentGenericSet<string>(null, new HashSet<string> {"E"})
};

var items = bags.SelectMany(b => b).ToArray();

Assert.That(items, Is.EqualTo(new[] {"A", "B", "C", "D", "E"}));
}

[Test]
public void AddRangeWorksCorrectly()
{
var items = new List<string> {"A"};
items.AddRange(new PersistentGenericBag<string>(null, new[] {"B"}));
items.AddRange(new PersistentIdentifierBag<string>(null, new[] {"C"}));
items.AddRange(new PersistentGenericList<string>(null, new[] {"D"}));
items.AddRange(new PersistentGenericSet<string>(null, new HashSet<string> {"E"}));

Assert.That(items, Is.EqualTo(new[] {"A", "B", "C", "D", "E"}));
}
}
}
46 changes: 46 additions & 0 deletions src/NHibernate.Test/CollectionTest/PersistentGenericMapFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Collections.Generic;
using System.Linq;
using NHibernate.Collection.Generic;
using NUnit.Framework;

namespace NHibernate.Test.CollectionTest
{
[TestFixture]
public class PersistentGenericMapFixture
{
[Test]
public void SelectManyWorksCorrectly()
{
var bags = new[]
{
new PersistentGenericMap<string, string>(
null,
new Dictionary<string, string> {{"A", "B"}}),
new PersistentGenericMap<string, string>(
null,
new Dictionary<string, string> {{"C", "D"}})
};

var items = bags.SelectMany(b => b).ToArray();

Assert.That(
items,
Is.EquivalentTo(new[] {new KeyValuePair<string, string>("A", "B"), new KeyValuePair<string, string>("C", "D")}));
}

[Test]
public void AddRangeWorksCorrectly()
{
var items = new List<KeyValuePair<string, string>> {new KeyValuePair<string, string>("A", "B")};

items.AddRange(
new PersistentGenericMap<string, string>(
null,
new Dictionary<string, string> {{"C", "D"}}));

Assert.That(
items,
Is.EquivalentTo(new[] {new KeyValuePair<string, string>("A", "B"), new KeyValuePair<string, string>("C", "D")}));
}
}
}
21 changes: 12 additions & 9 deletions src/NHibernate/Collection/Generic/PersistentGenericBag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,17 @@ bool ICollection.IsSynchronized
get { return false; }
}

void ICollection.CopyTo(Array array, int index)
void ICollection.CopyTo(Array array, int arrayIndex)
{
for (var i = index; i < Count; i++)
Read();
if (_gbag is ICollection collection)
{
collection.CopyTo(array, arrayIndex);
}
else
{
array.SetValue(this[i], i);
foreach (var item in _gbag)
array.SetValue(item, arrayIndex++);
}
}

Expand Down Expand Up @@ -197,16 +203,13 @@ public void Clear()

public bool Contains(T item)
{
var exists = ReadElementExistence(item);
return !exists.HasValue ? _gbag.Contains(item) : exists.Value;
return ReadElementExistence(item) ?? _gbag.Contains(item);
}

public void CopyTo(T[] array, int arrayIndex)
{
for (var i = arrayIndex; i < Count; i++)
{
array.SetValue(this[i], i);
}
Read();
_gbag.CopyTo(array, arrayIndex);
}

public bool Remove(T item)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,11 +390,17 @@ public int Count
get { return ReadSize() ? CachedSize : _values.Count; }
}

void ICollection.CopyTo(Array array, int index)
void ICollection.CopyTo(Array array, int arrayIndex)
{
for (int i = index; i < Count; i++)
Read();
if (_values is ICollection collection)
{
collection.CopyTo(array, arrayIndex);
}
else
{
array.SetValue(this[i], i);
foreach (var item in _values)
array.SetValue(item, arrayIndex++);
}
}

Expand Down Expand Up @@ -449,10 +455,8 @@ public bool Contains(T item)

public void CopyTo(T[] array, int arrayIndex)
{
for (int i = arrayIndex; i < Count; i++)
{
array.SetValue(this[i], i);
}
Read();
_values.CopyTo(array, arrayIndex);
}

public bool Remove(T item)
Expand Down
21 changes: 12 additions & 9 deletions src/NHibernate/Collection/Generic/PersistentGenericList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -407,11 +407,17 @@ public T this[int index]

#region ICollection Members

void ICollection.CopyTo(Array array, int index)
void ICollection.CopyTo(Array array, int arrayIndex)
{
for (int i = index; i < Count; i++)
Read();
if (WrappedList is ICollection collection)
{
collection.CopyTo(array, arrayIndex);
}
else
{
array.SetValue(this[i], i);
foreach (var item in WrappedList)
array.SetValue(item, arrayIndex++);
}
}

Expand Down Expand Up @@ -450,16 +456,13 @@ public void Add(T item)

public bool Contains(T item)
{
bool? exists = ReadElementExistence(item);
return !exists.HasValue ? WrappedList.Contains(item) : exists.Value;
return ReadElementExistence(item) ?? WrappedList.Contains(item);
}

public void CopyTo(T[] array, int arrayIndex)
{
for (int i = arrayIndex; i < Count; i++)
{
array.SetValue(this[i], i);
}
Read();
WrappedList.CopyTo(array, arrayIndex);
}

bool ICollection<T>.IsReadOnly {
Expand Down
33 changes: 13 additions & 20 deletions src/NHibernate/Collection/Generic/PersistentGenericMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -408,24 +408,8 @@ public bool Contains(KeyValuePair<TKey, TValue> item)

public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
int c = Count;
var keys = new TKey[c];
var values = new TValue[c];
if (Keys != null)
{
Keys.CopyTo(keys, arrayIndex);
}
if (Values != null)
{
Values.CopyTo(values, arrayIndex);
}
for (int i = arrayIndex; i < c; i++)
{
if (keys[i] != null || values[i] != null)
{
array.SetValue(new KeyValuePair<TKey, TValue>(keys[i], values[i]), i);
}
}
Read();
WrappedMap.CopyTo(array, arrayIndex);
}

public bool Remove(KeyValuePair<TKey, TValue> item)
Expand Down Expand Up @@ -453,9 +437,18 @@ public bool IsReadOnly

#region ICollection Members

public void CopyTo(Array array, int index)
public void CopyTo(Array array, int arrayIndex)
{
CopyTo((KeyValuePair<TKey, TValue>[]) array, index);
Read();
if (WrappedMap is ICollection collection)
{
collection.CopyTo(array, arrayIndex);
}
else
{
foreach (var item in WrappedMap)
array.SetValue(item, arrayIndex++);
}
}

public object SyncRoot
Expand Down
1 change: 0 additions & 1 deletion src/NHibernate/Collection/Generic/PersistentGenericSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,6 @@ public void Clear()

public void CopyTo(T[] array, int arrayIndex)
{
// NH : we really need to initialize the set ?
Read();
WrappedSet.CopyTo(array, arrayIndex);
}
Expand Down