< Summary

Class:Azure.Core.Http.Multipart.ObjectCollection`1
Assembly:Azure.Storage.Blobs.Batch
File(s):C:\Git\azure-sdk-for-net\sdk\storage\Azure.Storage.Blobs.Batch\src\Shared\ObjectCollection.cs
Covered lines:0
Uncovered lines:27
Coverable lines:27
Total lines:86
Line coverage:0% (0 of 27)
Covered branches:0
Total branches:8
Branch coverage:0% (0 of 8)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.cctor()-0%100%
CreateInnerList(...)-0%0%
.ctor()-0%100%
.ctor(...)-0%100%
.ctor(...)-0%0%
get_IsReadOnly()-0%100%
InsertItem(...)-0%100%
SetItem(...)-0%100%
CheckNotNull(...)-0%0%

File(s)

C:\Git\azure-sdk-for-net\sdk\storage\Azure.Storage.Blobs.Batch\src\Shared\ObjectCollection.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4// Copied from https://github.com/aspnet/AspNetCore/tree/master/src/Http/Headers/src
 5
 6using System;
 7using System.Collections.Generic;
 8using System.Collections.ObjectModel;
 9
 10#pragma warning disable IDE0008 // Use explicit type
 11#pragma warning disable IDE1006 // Missing s_ prefix
 12
 13namespace Azure.Core.Http.Multipart
 14{
 15    // List<T> allows 'null' values to be added. This is not what we want so we use a custom Collection<T> derived
 16    // type to throw if 'null' gets added. Collection<T> internally uses List<T> which comes at some cost. In addition
 17    // Collection<T>.Add() calls List<T>.InsertItem() which is an O(n) operation (compared to O(1) for List<T>.Add()).
 18    // This type is only used for very small collections (1-2 items) to keep the impact of using Collection<T> small.
 19    internal sealed class ObjectCollection<T> : Collection<T>
 20    {
 021        internal static readonly Action<T> DefaultValidator = CheckNotNull;
 022        internal static readonly ObjectCollection<T> EmptyReadOnlyCollection
 023            = new ObjectCollection<T>(DefaultValidator, isReadOnly: true);
 24
 25        private readonly Action<T> _validator;
 26
 27        // We need to create a 'read-only' inner list for Collection<T> to do the right
 28        // thing.
 29        private static IList<T> CreateInnerList(bool isReadOnly, IEnumerable<T> other = null)
 30        {
 031            var list = other == null ? new List<T>() : new List<T>(other);
 032            if (isReadOnly)
 33            {
 034                return new ReadOnlyCollection<T>(list);
 35            }
 36            else
 37            {
 038                return list;
 39            }
 40        }
 41
 42        public ObjectCollection()
 043            : this(DefaultValidator)
 44        {
 045        }
 46
 47        public ObjectCollection(Action<T> validator, bool isReadOnly = false)
 048            : base(CreateInnerList(isReadOnly))
 49        {
 050            _validator = validator;
 051        }
 52
 53        public ObjectCollection(IEnumerable<T> other, bool isReadOnly = false)
 054            : base(CreateInnerList(isReadOnly, other))
 55        {
 056            _validator = DefaultValidator;
 057            foreach (T item in Items)
 58            {
 059                _validator(item);
 60            }
 061        }
 62
 063        public bool IsReadOnly => ((ICollection<T>)this).IsReadOnly;
 64
 65        protected override void InsertItem(int index, T item)
 66        {
 067            _validator(item);
 068            base.InsertItem(index, item);
 069        }
 70
 71        protected override void SetItem(int index, T item)
 72        {
 073            _validator(item);
 074            base.SetItem(index, item);
 075        }
 76
 77        private static void CheckNotNull(T item)
 78        {
 79            // null values cannot be added to the collection.
 080            if (item == null)
 81            {
 082                throw new ArgumentNullException(nameof(item));
 83            }
 084        }
 85    }
 86}