diff --git a/src/Common.Test/Common.Test.csproj b/src/Common.Test/Common.Test.csproj index 23ac7340bbb1..691e09dcd01e 100644 --- a/src/Common.Test/Common.Test.csproj +++ b/src/Common.Test/Common.Test.csproj @@ -74,6 +74,8 @@ + + @@ -98,6 +100,7 @@ + diff --git a/src/Common.Test/Internals/LazyDictionaryTest.cs b/src/Common.Test/Internals/LazyDictionaryTest.cs new file mode 100644 index 000000000000..f6cdf928039a --- /dev/null +++ b/src/Common.Test/Internals/LazyDictionaryTest.cs @@ -0,0 +1,64 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +using Microsoft.WindowsAzure.Common.Internals; +using Xunit; + +namespace Microsoft.WindowsAzure.Common.Test.Internals +{ + public class LazyDictionaryTest + { + [Fact] + public void LazyByDefaultTest() + { + // Arrange + var lazyDictionary = new LazyDictionary(); + + // Act + var initialized = lazyDictionary.IsInitialized; + + // Assert + Assert.False(initialized); + } + + [Fact] + public void LazyAddTest() + { + // Arrange + var lazyDictionary = new LazyDictionary(); + + // Act + lazyDictionary.Add("key", "value"); + var initialized = lazyDictionary.IsInitialized; + + // Assert + Assert.True(initialized); + } + + [Fact] + public void LazyKeyAddTest() + { + // Arrange + var lazyDictionary = new LazyDictionary(); + + // Act + lazyDictionary["key"] = "value"; + var initialized = lazyDictionary.IsInitialized; + + // Assert + Assert.True(initialized); + } + } +} diff --git a/src/Common.Test/Internals/LazyListTest.cs b/src/Common.Test/Internals/LazyListTest.cs new file mode 100644 index 000000000000..4e8fd9f40b35 --- /dev/null +++ b/src/Common.Test/Internals/LazyListTest.cs @@ -0,0 +1,50 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +using Microsoft.WindowsAzure.Common.Internals; +using Xunit; + +namespace Microsoft.WindowsAzure.Common.Test.Internals +{ + public class LazyListTest + { + [Fact] + public void LazyByDefaultTest() + { + // Arrange + var lazyList = new LazyList(); + + // Act + var initialized = lazyList.IsInitialized; + + // Assert + Assert.False(initialized); + } + + [Fact] + public void LazyAddTest() + { + // Arrange + var lazyList = new LazyList(); + + // Act + lazyList.Add("item"); + var initialized = lazyList.IsInitialized; + + // Assert + Assert.True(initialized); + } + } +} diff --git a/src/Common/Common.csproj b/src/Common/Common.csproj index 32d94a7ac0f5..4f4e8f36af68 100644 --- a/src/Common/Common.csproj +++ b/src/Common/Common.csproj @@ -46,6 +46,9 @@ + + + diff --git a/src/Common/Internals/ILazyCollection.cs b/src/Common/Internals/ILazyCollection.cs new file mode 100644 index 000000000000..71d007ffb1ab --- /dev/null +++ b/src/Common/Internals/ILazyCollection.cs @@ -0,0 +1,22 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +namespace Microsoft.WindowsAzure.Common.Internals +{ + public interface ILazyCollection + { + bool IsInitialized { get; } + } +} diff --git a/src/Common/Internals/LazyDictionary.cs b/src/Common/Internals/LazyDictionary.cs new file mode 100644 index 000000000000..1f0d7c7d8961 --- /dev/null +++ b/src/Common/Internals/LazyDictionary.cs @@ -0,0 +1,158 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +using System.Collections; +using System.Collections.Generic; + +namespace Microsoft.WindowsAzure.Common.Internals +{ + public class LazyDictionary : IDictionary, ILazyCollection + { + private IDictionary _internalDictionary; + private IDictionary InternalDictionary + { + get + { + if (_internalDictionary == null) + { + _internalDictionary = new Dictionary(); + } + + return _internalDictionary; + } + + set + { + _internalDictionary = value; + } + } + + public bool IsInitialized + { + get { return _internalDictionary != null; } + } + + public LazyDictionary() + { + // Default constructor is lazy so it doesn't initialize the dictionary + } + + public LazyDictionary(IDictionary dictionary) + { + InternalDictionary = new Dictionary(dictionary); + } + + public LazyDictionary(IDictionary dictionary, IEqualityComparer comparer) + { + InternalDictionary = new Dictionary(dictionary, comparer); + } + + public LazyDictionary(IEqualityComparer comparer) + { + InternalDictionary = new Dictionary(comparer); + } + + public LazyDictionary(int capacity) + { + InternalDictionary = new Dictionary(capacity); + } + + public LazyDictionary(int capacity, IEqualityComparer comparer) + { + InternalDictionary = new Dictionary(capacity, comparer); + } + + public void Add(TKey key, TValue value) + { + InternalDictionary.Add(key, value); + } + + public bool ContainsKey(TKey key) + { + return InternalDictionary.ContainsKey(key); + } + + public ICollection Keys + { + get { return InternalDictionary.Keys; } + } + + public bool Remove(TKey key) + { + return InternalDictionary.Remove(key); + } + + public bool TryGetValue(TKey key, out TValue value) + { + return InternalDictionary.TryGetValue(key, out value); + } + + public ICollection Values + { + get { return InternalDictionary.Values; } + } + + public TValue this[TKey key] + { + get { return InternalDictionary[key]; } + set { InternalDictionary[key] = value; } + } + + public void Add(KeyValuePair item) + { + InternalDictionary.Add(item); + } + + public void Clear() + { + InternalDictionary.Clear(); + } + + public bool Contains(KeyValuePair item) + { + return InternalDictionary.Contains(item); + } + + public void CopyTo(KeyValuePair[] array, int arrayIndex) + { + InternalDictionary.CopyTo(array, arrayIndex); + } + + public int Count + { + get { return InternalDictionary.Count; } + } + + public bool IsReadOnly + { + get { return InternalDictionary.IsReadOnly; } + } + + public bool Remove(KeyValuePair item) + { + return InternalDictionary.Remove(item); + } + + public IEnumerator> GetEnumerator() + { + return InternalDictionary.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return InternalDictionary.GetEnumerator(); + } + } +} diff --git a/src/Common/Internals/LazyList.cs b/src/Common/Internals/LazyList.cs new file mode 100644 index 000000000000..c413e0c06053 --- /dev/null +++ b/src/Common/Internals/LazyList.cs @@ -0,0 +1,128 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +using System.Collections; +using System.Collections.Generic; + +namespace Microsoft.WindowsAzure.Common.Internals +{ + public class LazyList : IList, ILazyCollection + { + private IList _internalList; + private IList InternalList + { + get + { + if (_internalList == null) + { + _internalList = new List(); + } + + return _internalList; + } + + set + { + _internalList = value; + } + } + + public bool IsInitialized + { + get { return _internalList != null; } + } + + public LazyList() + { + // Default constructor is lazy so it doesn't initialize the list + } + + public LazyList(IEnumerable collection) + { + InternalList = new List(collection); + } + + public LazyList(int capacity) + { + InternalList = new List(capacity); + } + + public int IndexOf(T item) + { + return InternalList.IndexOf(item); + } + + public void Insert(int index, T item) + { + InternalList.Insert(index, item); + } + + public void RemoveAt(int index) + { + InternalList.RemoveAt(index); + } + + public T this[int index] + { + get { return InternalList[index]; } + set { InternalList[index] = value; } + } + + public void Add(T item) + { + InternalList.Add(item); + } + + public void Clear() + { + InternalList.Clear(); + } + + public bool Contains(T item) + { + return InternalList.Contains(item); + } + + public void CopyTo(T[] array, int arrayIndex) + { + InternalList.CopyTo(array, arrayIndex); + } + + public int Count + { + get { return InternalList.Count; } + } + + public bool IsReadOnly + { + get { return InternalList.IsReadOnly; } + } + + public bool Remove(T item) + { + return InternalList.Remove(item); + } + + public IEnumerator GetEnumerator() + { + return InternalList.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return InternalList.GetEnumerator(); + } + } +}