-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added convenience methods for conversion of arrays with abstractions to array with implementations and vice versa. Added InstanceEqualityComparer. Added AbstractionEventHandlerLookup for mapping eventhandler using abstractions to eventhandler using implementations.
- Loading branch information
Showing
18 changed files
with
781 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
**/.vs/** | ||
**/bin/** | ||
**/obj/** | ||
**/packages/** | ||
*.xproj.user | ||
project.lock.json | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/Thinktecture.Abstractions/AbstractionEventHandlerContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
|
||
namespace Thinktecture | ||
{ | ||
/// <summary> | ||
/// Event handler context of an abstraction. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
public class AbstractionEventHandlerContext<T> | ||
{ | ||
/// <summary> | ||
/// Event handler. | ||
/// </summary> | ||
public EventHandler<T> Handler { get; } | ||
|
||
/// <summary> | ||
/// Indication how many times the handler has been attached. | ||
/// </summary> | ||
public int Count { get; set; } | ||
|
||
/// <summary> | ||
/// Initializes new istance of <see cref="AbstractionEventHandlerContext{T}"/>. | ||
/// </summary> | ||
/// <param name="handler">Event handler</param> | ||
public AbstractionEventHandlerContext(EventHandler<T> handler) | ||
{ | ||
if (handler == null) | ||
throw new ArgumentNullException(nameof(handler)); | ||
|
||
Handler = handler; | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/Thinktecture.Abstractions/AbstractionEventHandlerLookup.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Thinktecture.Collections.Generic; | ||
|
||
namespace Thinktecture | ||
{ | ||
/// <summary> | ||
/// Allow mapping of event handlers. | ||
/// </summary> | ||
public class AbstractionEventHandlerLookup<TAbstraction, TImplementation> | ||
where TAbstraction : IAbstraction | ||
{ | ||
private readonly Dictionary<EventHandler<TAbstraction>, AbstractionEventHandlerContext<TImplementation>> _lookup; | ||
|
||
/// <summary> | ||
/// Initializes new instance of <see cref="AbstractionEventHandlerLookup{TAbstraction,TImplementation}"/>. | ||
/// </summary> | ||
public AbstractionEventHandlerLookup() | ||
{ | ||
var comparer = new InstanceEqualityComparer<EventHandler<TAbstraction>>(); | ||
_lookup = new Dictionary<EventHandler<TAbstraction>, AbstractionEventHandlerContext<TImplementation>>(comparer); | ||
} | ||
|
||
/// <summary> | ||
/// Maps handler for attaching to an event. | ||
/// </summary> | ||
/// <param name="handler">Handler to map.</param> | ||
/// <param name="toInterface">Function to convert an implementation to an abstraction.</param> | ||
/// <returns>Mapped handler</returns> | ||
public EventHandler<TImplementation> MapForAttachment(EventHandler<TAbstraction> handler, Func<TImplementation, TAbstraction> toInterface) | ||
{ | ||
if (handler == null) | ||
return null; | ||
|
||
if (toInterface == null) | ||
throw new ArgumentNullException(nameof(toInterface)); | ||
|
||
AbstractionEventHandlerContext<TImplementation> ctx; | ||
if (!_lookup.TryGetValue(handler, out ctx)) | ||
{ | ||
ctx = new AbstractionEventHandlerContext<TImplementation>((sender, args) => handler(sender, toInterface(args))); | ||
_lookup.Add(handler, ctx); | ||
} | ||
|
||
ctx.Count++; | ||
|
||
return ctx.Handler; | ||
} | ||
|
||
/// <summary> | ||
/// Maps handler for detachment. | ||
/// </summary> | ||
/// <param name="handler">Handler to map.</param> | ||
/// <returns>Mapped handler</returns> | ||
public EventHandler<TImplementation> TryMapForDetachment(EventHandler<TAbstraction> handler) | ||
{ | ||
AbstractionEventHandlerContext<TImplementation> ctx; | ||
|
||
if (handler != null && _lookup.TryGetValue(handler, out ctx)) | ||
{ | ||
ctx.Count--; | ||
|
||
if (ctx.Count <= 0) | ||
_lookup.Remove(handler); | ||
|
||
return ctx.Handler; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Thinktecture.Abstractions/Collections/Generic/InstanceEqualityComparer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Thinktecture.Collections.Generic | ||
{ | ||
/// <summary> | ||
/// Use <see cref="Object.ReferenceEquals"/> for comparison. | ||
/// </summary> | ||
/// <typeparam name="T">Type of item to compare.</typeparam> | ||
public class InstanceEqualityComparer<T> : IEqualityComparer<T> | ||
{ | ||
/// <inheritdoc /> | ||
public bool Equals(T x, T y) | ||
{ | ||
return ReferenceEquals(x, y); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public int GetHashCode(T obj) | ||
{ | ||
return obj?.GetHashCode() ?? 0; | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Thinktecture.Abstractions/Extensions/ArrayExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
namespace Thinktecture.Extensions | ||
{ | ||
/// <summary> | ||
/// Extensions for arrays | ||
/// </summary> | ||
public static class ArrayExtensions | ||
{ | ||
/// <summary> | ||
/// Copies and converts abstractions to implementations. | ||
/// </summary> | ||
/// <typeparam name="TAbstraction">Type of the abstraction.</typeparam> | ||
/// <typeparam name="TImplementation">Type of the implementation.</typeparam> | ||
/// <param name="abstractions">Array with abstractions.</param> | ||
/// <returns></returns> | ||
public static TImplementation[] ToImplementation<TAbstraction, TImplementation>(this TAbstraction[] abstractions) | ||
where TAbstraction : IAbstraction | ||
{ | ||
if (abstractions == null) | ||
return null; | ||
|
||
var implementations = new TImplementation[abstractions.Length]; | ||
|
||
for (var i = 0; i < abstractions.Length; i++) | ||
{ | ||
implementations[i] = (TImplementation) abstractions[i]?.UnsafeConvert(); | ||
} | ||
|
||
return implementations; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/Thinktecture.IO.FileSystem.Abstractions/Extensions/ArrayExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System.IO; | ||
using Thinktecture.IO; | ||
|
||
namespace Thinktecture | ||
{ | ||
/// <summary> | ||
/// Extensions for arrays. | ||
/// </summary> | ||
public static class ArrayExtensions | ||
{ | ||
/// <summary> | ||
/// Converts an array of <see cref="DirectoryInfo"/> to an array of <see cref="IDirectoryInfo"/>. | ||
/// </summary> | ||
/// <param name="infos">Array to convert.</param> | ||
/// <returns>An array of <see cref="IDirectoryInfo"/>.</returns> | ||
public static IDirectoryInfo[] ToInterface(this DirectoryInfo[] infos) | ||
{ | ||
if (infos == null) | ||
return null; | ||
|
||
var abstractions = new IDirectoryInfo[infos.Length]; | ||
|
||
for (var i = 0; i < infos.Length; i++) | ||
{ | ||
abstractions[i] = infos[i].ToInterface(); | ||
} | ||
|
||
return abstractions; | ||
} | ||
|
||
/// <summary> | ||
/// Converts an array of <see cref="FileInfo"/> to an array of <see cref="IFileInfo"/>. | ||
/// </summary> | ||
/// <param name="infos">Array to convert.</param> | ||
/// <returns>An array of <see cref="IFileInfo"/>.</returns> | ||
public static IFileInfo[] ToInterface(this FileInfo[] infos) | ||
{ | ||
if (infos == null) | ||
return null; | ||
|
||
var abstractions = new IFileInfo[infos.Length]; | ||
|
||
for (var i = 0; i < infos.Length; i++) | ||
{ | ||
abstractions[i] = infos[i].ToInterface(); | ||
} | ||
|
||
return abstractions; | ||
} | ||
|
||
/// <summary> | ||
/// Converts an array of <see cref="FileSystemInfo"/> to an array of <see cref="IFileSystemInfo"/>. | ||
/// </summary> | ||
/// <param name="infos">Array to convert.</param> | ||
/// <returns>An array of <see cref="IFileSystemInfo"/>.</returns> | ||
public static IFileSystemInfo[] ToInterface(this FileSystemInfo[] infos) | ||
{ | ||
if (infos == null) | ||
return null; | ||
|
||
var abstractions = new IFileSystemInfo[infos.Length]; | ||
|
||
for (var i = 0; i < infos.Length; i++) | ||
{ | ||
abstractions[i] = infos[i].ToInterface(); | ||
} | ||
|
||
return abstractions; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.