Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: cdhowie/Cdh.Toolkit
base: 124e8c08c9
...
head fork: cdhowie/Cdh.Toolkit
compare: 2b392ffdb1
Checking mergeability… Don't worry, you can still create the pull request.
  • 6 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
View
36 Cdh.Toolkit.Collections/Builders/DictionaryBuilder.cs
@@ -0,0 +1,36 @@
+using System;
+using System.Collections.Generic;
+
+namespace Cdh.Toolkit.Collections.Builders
+{
+ public static class DictionaryBuilder
+ {
+ public static DictionaryBuilder<TKey, TValue> Create<TKey, TValue>(TKey key, TValue value)
+ {
+ return new DictionaryBuilder<TKey, TValue>().Add(key, value);
+ }
+ }
+
+ public class DictionaryBuilder<TKey, TValue>
+ {
+ public Dictionary<TKey, TValue> Dictionary { get; private set; }
+
+ public DictionaryBuilder()
+ {
+ Dictionary = new Dictionary<TKey, TValue>();
+ }
+
+ public DictionaryBuilder<TKey, TValue> Add(TKey key, TValue value)
+ {
+ Dictionary.Add(key, value);
+
+ return this;
+ }
+
+ public IDictionary<TKey, TValue> AsReadOnly()
+ {
+ return new ReadOnlyDictionary<TKey, TValue>(Dictionary);
+ }
+ }
+}
+
View
13 Cdh.Toolkit.Collections/Cdh.Toolkit.Collections.csproj
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -18,9 +18,9 @@
<UpgradeBackupLocation />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
- <DebugSymbols>true</DebugSymbols>
+ <DebugSymbols>True</DebugSymbols>
<DebugType>full</DebugType>
- <Optimize>false</Optimize>
+ <Optimize>False</Optimize>
<OutputPath>..\bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
@@ -28,7 +28,7 @@
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
- <Optimize>true</Optimize>
+ <Optimize>True</Optimize>
<OutputPath>..\bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
@@ -71,6 +71,8 @@
<Compile Include="SynchronizedCollection.cs" />
<Compile Include="SynchronizedDictionary.cs" />
<Compile Include="SynchronizedList.cs" />
+ <Compile Include="Builders\DictionaryBuilder.cs" />
+ <Compile Include="ReferenceEqualityComparerBase.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Cdh.Toolkit.Extensions\Cdh.Toolkit.Extensions.csproj">
@@ -86,4 +88,7 @@
<Target Name="AfterBuild">
</Target>
-->
+ <ItemGroup>
+ <Folder Include="Builders\" />
+ </ItemGroup>
</Project>
View
21 Cdh.Toolkit.Collections/CustomEqualityComparer.cs
@@ -47,11 +47,32 @@ public CustomEqualityComparer(Func<TObject, TKey> keySelector)
public bool Equals(TObject x, TObject y)
{
+ if (!typeof(TObject).IsValueType) {
+ // Reference semantics.
+ if (object.ReferenceEquals(x, y)) {
+ return true;
+ }
+
+ if (object.ReferenceEquals(x, default(TObject))) {
+ return object.ReferenceEquals(y, default(TObject));
+ }
+
+ if (object.ReferenceEquals(y, default(TObject))) {
+ return false;
+ }
+ }
+
return EqualityComparer<TKey>.Default.Equals(keySelector(x), keySelector(y));
}
public int GetHashCode(TObject obj)
{
+ if (!typeof(TObject).IsValueType) {
+ if (object.ReferenceEquals(obj, default(TObject))) {
+ throw new ArgumentNullException("obj");
+ }
+ }
+
return EqualityComparer<TKey>.Default.GetHashCode(keySelector(obj));
}
View
80 Cdh.Toolkit.Collections/ReferenceEqualityComparerBase.cs
@@ -0,0 +1,80 @@
+using System;
+using System.Collections.Generic;
+
+namespace Cdh.Toolkit.Collections
+{
+ public abstract class ReferenceEqualityComparerBase<T> : IEqualityComparer<T>
+ where T : class
+ {
+ public ReferenceEqualityComparerBase() { }
+
+ public bool Equals(T x, T y)
+ {
+ if (object.ReferenceEquals(x, y)) {
+ return true;
+ }
+
+ if (x == null) {
+ return y == null;
+ }
+
+ if (y == null) {
+ return false;
+ }
+
+ return EqualityTest(x, y);
+ }
+
+ protected abstract bool EqualityTest(T x, T y);
+
+ public int GetHashCode(T obj)
+ {
+ if (obj == null) {
+ throw new ArgumentNullException("obj");
+ }
+
+ return ComputeHashCode(obj);
+ }
+
+ protected abstract int ComputeHashCode(T obj);
+
+ #region Singleton implementation
+
+ public static ReferenceEqualityComparerBase<T> Create(
+ Func<T, T, bool> equalityFunction,
+ Func<T, int> hashCodeFunction)
+ {
+ return new LambdaReferenceEqualityComparer(equalityFunction, hashCodeFunction);
+ }
+
+ private class LambdaReferenceEqualityComparer : ReferenceEqualityComparerBase<T>
+ {
+ private Func<T, T, bool> equalityFunction;
+ private Func<T, int> hashCodeFunction;
+
+ public LambdaReferenceEqualityComparer(Func<T, T, bool> equalityFunction, Func<T, int> hashCodeFunction)
+ {
+ if (equalityFunction == null)
+ throw new ArgumentNullException("equalityFunction");
+
+ if (hashCodeFunction == null)
+ throw new ArgumentNullException("hashCodeFunction");
+
+ this.equalityFunction = equalityFunction;
+ this.hashCodeFunction = hashCodeFunction;
+ }
+
+ protected override bool EqualityTest(T x, T y)
+ {
+ return equalityFunction(x, y);
+ }
+
+ protected override int ComputeHashCode(T obj)
+ {
+ return hashCodeFunction(obj);
+ }
+ }
+
+ #endregion
+ }
+}

No commit comments for this range

Something went wrong with that request. Please try again.