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
3 changes: 3 additions & 0 deletions src/Common.Test/Common.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
<Compile Include="Fakes\FakeServiceClient.cs" />
<Compile Include="Fakes\RecordedDelegatingHandler.cs" />
<Compile Include="Internals\ParserHelperTests.cs" />
<Compile Include="Internals\LazyDictionaryTest.cs" />
<Compile Include="Internals\LazyListTest.cs" />
<Compile Include="OData\FilterStringTest.cs" />
<Compile Include="Internals\UriHelperTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand All @@ -98,6 +100,7 @@
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Import Project="..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />
Expand Down
64 changes: 64 additions & 0 deletions src/Common.Test/Internals/LazyDictionaryTest.cs
Original file line number Diff line number Diff line change
@@ -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<string, string>();

// Act
var initialized = lazyDictionary.IsInitialized;

// Assert
Assert.False(initialized);
}

[Fact]
public void LazyAddTest()
{
// Arrange
var lazyDictionary = new LazyDictionary<string, string>();

// Act
lazyDictionary.Add("key", "value");
var initialized = lazyDictionary.IsInitialized;

// Assert
Assert.True(initialized);
}

[Fact]
public void LazyKeyAddTest()
{
// Arrange
var lazyDictionary = new LazyDictionary<string, string>();

// Act
lazyDictionary["key"] = "value";
var initialized = lazyDictionary.IsInitialized;

// Assert
Assert.True(initialized);
}
}
}
50 changes: 50 additions & 0 deletions src/Common.Test/Internals/LazyListTest.cs
Original file line number Diff line number Diff line change
@@ -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<string>();

// Act
var initialized = lazyList.IsInitialized;

// Assert
Assert.False(initialized);
}

[Fact]
public void LazyAddTest()
{
// Arrange
var lazyList = new LazyList<string>();

// Act
lazyList.Add("item");
var initialized = lazyList.IsInitialized;

// Assert
Assert.True(initialized);
}
}
}
3 changes: 3 additions & 0 deletions src/Common/Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
<Compile Include="Internals\ParserHelper.cs" />
<Compile Include="Internals\TypeConversion.cs" />
<Compile Include="Internals\UriHelper.cs" />
<Compile Include="Internals\ILazyCollection.cs" />
<Compile Include="Internals\LazyDictionary.cs" />
<Compile Include="Internals\LazyList.cs" />
<Compile Include="Models\OperationResponse.cs" />
<Compile Include="Models\OperationStatus.cs" />
<Compile Include="Models\OperationStatusResponse.cs" />
Expand Down
22 changes: 22 additions & 0 deletions src/Common/Internals/ILazyCollection.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
158 changes: 158 additions & 0 deletions src/Common/Internals/LazyDictionary.cs
Original file line number Diff line number Diff line change
@@ -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<TKey, TValue> : IDictionary<TKey, TValue>, ILazyCollection
{
private IDictionary<TKey, TValue> _internalDictionary;
private IDictionary<TKey, TValue> InternalDictionary
{
get
{
if (_internalDictionary == null)
{
_internalDictionary = new Dictionary<TKey, TValue>();
}

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<TKey, TValue> dictionary)
{
InternalDictionary = new Dictionary<TKey, TValue>(dictionary);
}

public LazyDictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer)
{
InternalDictionary = new Dictionary<TKey, TValue>(dictionary, comparer);
}

public LazyDictionary(IEqualityComparer<TKey> comparer)
{
InternalDictionary = new Dictionary<TKey, TValue>(comparer);
}

public LazyDictionary(int capacity)
{
InternalDictionary = new Dictionary<TKey, TValue>(capacity);
}

public LazyDictionary(int capacity, IEqualityComparer<TKey> comparer)
{
InternalDictionary = new Dictionary<TKey, TValue>(capacity, comparer);
}

public void Add(TKey key, TValue value)
{
InternalDictionary.Add(key, value);
}

public bool ContainsKey(TKey key)
{
return InternalDictionary.ContainsKey(key);
}

public ICollection<TKey> 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<TValue> Values
{
get { return InternalDictionary.Values; }
}

public TValue this[TKey key]
{
get { return InternalDictionary[key]; }
set { InternalDictionary[key] = value; }
}

public void Add(KeyValuePair<TKey, TValue> item)
{
InternalDictionary.Add(item);
}

public void Clear()
{
InternalDictionary.Clear();
}

public bool Contains(KeyValuePair<TKey, TValue> item)
{
return InternalDictionary.Contains(item);
}

public void CopyTo(KeyValuePair<TKey, TValue>[] 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<TKey, TValue> item)
{
return InternalDictionary.Remove(item);
}

public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return InternalDictionary.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return InternalDictionary.GetEnumerator();
}
}
}
Loading