diff options
author | csharptest <roger@csharptest.net> | 2011-09-16 12:21:00 -0500 |
---|---|---|
committer | rogerk <devnull@localhost> | 2011-09-16 12:21:00 -0500 |
commit | 4ecebd8b4a34c1f902176395024185f1724cb2eb (patch) | |
tree | b66dbd1ef1b8fc8f4b4813f4ac7522fc25d637dc /src/ProtocolBuffers/Collections/PopsicleList.cs | |
parent | 8f0dcf3df1548a1eff0bed54a9b992f55b8f72d5 (diff) | |
download | protobuf-4ecebd8b4a34c1f902176395024185f1724cb2eb.tar.gz protobuf-4ecebd8b4a34c1f902176395024185f1724cb2eb.tar.bz2 protobuf-4ecebd8b4a34c1f902176395024185f1724cb2eb.zip |
Tests and fixes for double-enumeration on AddRange and adding of null to PopsicleList
Diffstat (limited to 'src/ProtocolBuffers/Collections/PopsicleList.cs')
-rw-r--r-- | src/ProtocolBuffers/Collections/PopsicleList.cs | 38 |
1 files changed, 33 insertions, 5 deletions
diff --git a/src/ProtocolBuffers/Collections/PopsicleList.cs b/src/ProtocolBuffers/Collections/PopsicleList.cs index dc5c5836..3de97f89 100644 --- a/src/ProtocolBuffers/Collections/PopsicleList.cs +++ b/src/ProtocolBuffers/Collections/PopsicleList.cs @@ -42,6 +42,7 @@ namespace Google.ProtocolBuffers.Collections /// </summary>
public sealed class PopsicleList<T> : IPopsicleList<T>, ICastArray
{
+ private static readonly bool CheckForNull = default(T) == null;
private static readonly T[] EmptySet = new T[0];
private List<T> items;
@@ -65,6 +66,10 @@ namespace Google.ProtocolBuffers.Collections public void Insert(int index, T item)
{
ValidateModification();
+ if (CheckForNull)
+ {
+ ThrowHelper.ThrowIfNull(item);
+ }
items.Insert(index, item);
}
@@ -87,6 +92,10 @@ namespace Google.ProtocolBuffers.Collections set
{
ValidateModification();
+ if (CheckForNull)
+ {
+ ThrowHelper.ThrowIfNull(value);
+ }
items[index] = value;
}
}
@@ -94,6 +103,10 @@ namespace Google.ProtocolBuffers.Collections public void Add(T item)
{
ValidateModification();
+ if (CheckForNull)
+ {
+ ThrowHelper.ThrowIfNull(item);
+ }
items.Add(item);
}
@@ -145,15 +158,30 @@ namespace Google.ProtocolBuffers.Collections public void Add(IEnumerable<T> collection)
{
- if (readOnly)
+ ValidateModification();
+ ThrowHelper.ThrowIfNull(collection);
+
+ if (!CheckForNull || collection is PopsicleList<T>)
{
- throw new NotSupportedException("List is read-only");
+ items.AddRange(collection);
}
- if (items == null)
+ else
{
- items = new List<T>();
+ // Assumption, it's ok to enumerate collections more than once.
+ if (collection is ICollection<T>)
+ {
+ ThrowHelper.ThrowIfAnyNull(collection);
+ items.AddRange(collection);
+ }
+ else
+ {
+ foreach (T item in collection)
+ {
+ ThrowHelper.ThrowIfNull(item);
+ items.Add(item);
+ }
+ }
}
- items.AddRange(collection);
}
private void ValidateModification()
|