/// <summary> /// Maps the array of <typeparamref name="TSource"/> into a list of /// <typeparamref name="TDestination"/>. /// </summary> /// <typeparam name="TSource">The type of the source objects.</typeparam> /// <typeparam name="TDestination">The type of the destination objects.</typeparam> /// <param name="mapper">The mapper.</param> /// <param name="source">The source objects.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>A list of <typeparamref name="TDestination"/>.</returns> /// <exception cref="ArgumentNullException">The <paramref name="mapper"/> or <paramref name="source"/> is /// <c>null</c>.</exception> public static async Task <List <TDestination> > MapListAsync <TSource, TDestination>( this IAsyncMapper <TSource, TDestination> mapper, TSource[] source, CancellationToken cancellationToken = default) where TDestination : new() { if (mapper is null) { throw new ArgumentNullException(nameof(mapper)); } if (source is null) { throw new ArgumentNullException(nameof(source)); } var sourceCount = source.Length; var tasks = new Task[sourceCount]; var destination = new List <TDestination>(sourceCount); for (var i = 0; i < sourceCount; ++i) { var sourceItem = source[i]; var destinationItem = Factory <TDestination> .CreateInstance(); destination.Insert(i, destinationItem); tasks[i] = mapper.MapAsync(sourceItem, destinationItem, cancellationToken); } await Task.WhenAll(tasks).ConfigureAwait(false); return(destination); }
/// <summary> /// Maps the enumerable of <typeparamref name="TSource"/> into an observable collection of /// <typeparamref name="TDestination"/>. /// </summary> /// <typeparam name="TSource">The type of the source objects.</typeparam> /// <typeparam name="TDestination">The type of the destination objects.</typeparam> /// <param name="mapper">The mapper.</param> /// <param name="source">The source objects.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>An observable collection of <typeparamref name="TDestination"/>.</returns> /// <exception cref="ArgumentNullException">The <paramref name="mapper"/> or <paramref name="source"/> is /// <c>null</c>.</exception> public static async Task <ObservableCollection <TDestination> > MapObservableCollectionAsync <TSource, TDestination>( this IAsyncMapper <TSource, TDestination> mapper, IEnumerable <TSource> source, CancellationToken cancellationToken = default) where TDestination : new() { if (mapper is null) { throw new ArgumentNullException(nameof(mapper)); } if (source is null) { throw new ArgumentNullException(nameof(source)); } var sourceCount = source.Count(); var tasks = new Task[sourceCount]; var destination = new ObservableCollection <TDestination>(); var i = 0; foreach (var sourceItem in source) { var destinationItem = Factory <TDestination> .CreateInstance(); destination.Insert(i, destinationItem); tasks[i] = mapper.MapAsync(sourceItem, destinationItem, cancellationToken); ++i; } await Task.WhenAll(tasks).ConfigureAwait(false); return(destination); }
/// <summary> /// Maps the collection of <typeparamref name="TSource"/> into an observable collection of /// <typeparamref name="TDestination"/>. /// </summary> /// <typeparam name="TSource">The type of the source objects.</typeparam> /// <typeparam name="TDestination">The type of the destination objects.</typeparam> /// <param name="mapper">The mapper.</param> /// <param name="source">The source objects.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>An observable collection of <typeparamref name="TDestination"/>.</returns> /// <exception cref="ArgumentNullException">The <paramref name="mapper"/> or <paramref name="source"/> is /// <c>null</c>.</exception> public static async UniTask <ObservableCollection <TDestination> > MapObservableCollectionAsync <TSource, TDestination>( this IAsyncMapper <TSource, TDestination> mapper, Collection <TSource> source, CancellationToken cancellationToken = default) where TDestination : new() { if (mapper == null) { throw new ArgumentNullException(nameof(mapper)); } if (source == null) { throw new ArgumentNullException(nameof(source)); } var sourceCount = source.Count; var tasks = new UniTask[sourceCount]; var destination = new ObservableCollection <TDestination>(); for (var i = 0; i < sourceCount; ++i) { var sourceItem = source[i]; var destinationItem = new TDestination(); destination.Insert(i, destinationItem); tasks[i] = mapper.MapAsync(sourceItem, destinationItem, cancellationToken); } await UniTask.WhenAll(tasks); return(destination); }
/// <summary> /// Maps the <see cref="IAsyncEnumerable{TSource}"/> into <see cref="IAsyncEnumerable{TDestination}"/>. /// </summary> /// <typeparam name="TSource">The type of the source objects.</typeparam> /// <typeparam name="TDestination">The type of the destination objects.</typeparam> /// <param name="mapper">The mapper.</param> /// <param name="source">The source asynchronous enumerable.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>An <see cref="IAsyncEnumerable{TDestination}"/> collection.</returns> /// <exception cref="ArgumentNullException">The <paramref name="mapper"/> or <paramref name="source"/> is /// <c>null</c>.</exception> public static async IAsyncEnumerable <TDestination> MapEnumerableAsync <TSource, TDestination>( this IAsyncMapper <TSource, TDestination> mapper, IAsyncEnumerable <TSource> source, [EnumeratorCancellation] CancellationToken cancellationToken = default) where TDestination : new() { if (mapper is null) { throw new ArgumentNullException(nameof(mapper)); } if (source is null) { throw new ArgumentNullException(nameof(source)); } await foreach (var sourceItem in source.ConfigureAwait(false).WithCancellation(cancellationToken)) { var destinationItem = Factory <TDestination> .CreateInstance(); await mapper.MapAsync(sourceItem, destinationItem, cancellationToken).ConfigureAwait(false); yield return(destinationItem); } }
/// <summary> /// Maps the collection of <typeparamref name="TSource"/> into a collection of /// <typeparamref name="TDestination"/>. /// </summary> /// <typeparam name="TSource">The type of the source objects.</typeparam> /// <typeparam name="TDestination">The type of the destination objects.</typeparam> /// <param name="mapper">The mapper.</param> /// <param name="source">The source objects.</param> /// <returns>A collection of <typeparamref name="TDestination"/>.</returns> /// <exception cref="ArgumentNullException">The <paramref name="mapper"/> or <paramref name="source"/> is /// <c>null</c>.</exception> public static async Task <Collection <TDestination> > MapCollectionAsync <TSource, TDestination>( this IAsyncMapper <TSource, TDestination> mapper, Collection <TSource> source) where TDestination : new() { if (mapper is null) { throw new ArgumentNullException(nameof(mapper)); } if (source is null) { throw new ArgumentNullException(nameof(source)); } var sourceCount = source.Count; var tasks = new Task[sourceCount]; var destination = new Collection <TDestination>(); for (var i = 0; i < sourceCount; ++i) { var sourceItem = source[i]; var destinationItem = Factory <TDestination> .CreateInstance(); destination.Insert(i, destinationItem); tasks[i] = mapper.MapAsync(sourceItem, destinationItem); } await Task.WhenAll(tasks).ConfigureAwait(false); return(destination); }
/// <summary> /// Maps the array of <typeparamref name="TSource"/> into an observable collection of /// <typeparamref name="TDestination"/>. /// </summary> /// <typeparam name="TSource">The type of the source objects.</typeparam> /// <typeparam name="TDestination">The type of the destination objects.</typeparam> /// <param name="translator">The translator.</param> /// <param name="source">The source objects.</param> /// <returns>An observable collection of <typeparamref name="TDestination"/>.</returns> /// <exception cref="ArgumentNullException">The <paramref name="translator"/> or <paramref name="source"/> is /// <c>null</c>.</exception> public static async Task <ObservableCollection <TDestination> > MapObservableCollectionAsync <TSource, TDestination>( this IAsyncMapper <TSource, TDestination> translator, TSource[] source) where TDestination : new() { if (translator == null) { throw new ArgumentNullException(nameof(translator)); } if (source == null) { throw new ArgumentNullException(nameof(source)); } var sourceCount = source.Length; var tasks = new Task[sourceCount]; var destination = new ObservableCollection <TDestination>(); for (var i = 0; i < sourceCount; ++i) { var sourceItem = source[i]; var destinationItem = Factory <TDestination> .CreateInstance(); destination.Insert(i, destinationItem); tasks[i] = translator.MapAsync(sourceItem, destinationItem); } await Task.WhenAll(tasks).ConfigureAwait(false); return(destination); }
/// <summary> /// Maps the enumerable of <typeparamref name="TSource"/> into an array of /// <typeparamref name="TDestination"/>. /// </summary> /// <typeparam name="TSource">The type of the source objects.</typeparam> /// <typeparam name="TDestination">The type of the destination objects.</typeparam> /// <param name="mapper">The mapper.</param> /// <param name="source">The source objects.</param> /// <returns>An array of <typeparamref name="TDestination"/>.</returns> /// <exception cref="ArgumentNullException">The <paramref name="mapper"/> or <paramref name="source"/> is /// <c>null</c>.</exception> public static async Task <TDestination[]> MapArrayAsync <TSource, TDestination>( this IAsyncMapper <TSource, TDestination> mapper, IEnumerable <TSource> source) where TDestination : new() { if (mapper is null) { throw new ArgumentNullException(nameof(mapper)); } if (source is null) { throw new ArgumentNullException(nameof(source)); } var sourceCount = source.Count(); var tasks = new Task[sourceCount]; var destination = new TDestination[sourceCount]; var i = 0; foreach (var sourceItem in source) { var destinationItem = Factory <TDestination> .CreateInstance(); destination[i] = destinationItem; tasks[i] = mapper.MapAsync(sourceItem, destinationItem); ++i; } await Task.WhenAll(tasks).ConfigureAwait(false); return(destination); }
/// <summary> /// Maps the list of <typeparamref name="TSource"/> into an array of /// <typeparamref name="TDestination"/>. /// </summary> /// <typeparam name="TSource">The type of the source objects.</typeparam> /// <typeparam name="TDestination">The type of the destination objects.</typeparam> /// <param name="translator">The translator.</param> /// <param name="source">The source objects.</param> /// <returns>An array of <typeparamref name="TDestination"/>.</returns> /// <exception cref="ArgumentNullException">The <paramref name="translator"/> or <paramref name="source"/> is /// <c>null</c>.</exception> public static async Task <TDestination[]> MapArrayAsync <TSource, TDestination>( this IAsyncMapper <TSource, TDestination> translator, List <TSource> source) where TDestination : new() { if (translator == null) { throw new ArgumentNullException(nameof(translator)); } if (source == null) { throw new ArgumentNullException(nameof(source)); } var sourceCount = source.Count; var tasks = new Task[sourceCount]; var destination = new TDestination[sourceCount]; for (var i = 0; i < sourceCount; ++i) { var sourceItem = source[i]; var destinationItem = Factory <TDestination> .CreateInstance(); destination[i] = destinationItem; tasks[i] = translator.MapAsync(sourceItem, destinationItem); } await Task.WhenAll(tasks).ConfigureAwait(false); return(destination); }
/// <summary> /// Maps the enumerable of <typeparamref name="TSource"/> into a list of /// <typeparamref name="TDestination"/>. /// </summary> /// <typeparam name="TSource">The type of the source objects.</typeparam> /// <typeparam name="TDestination">The type of the destination objects.</typeparam> /// <param name="translator">The translator.</param> /// <param name="source">The source objects.</param> /// <returns>A list of <typeparamref name="TDestination"/>.</returns> /// <exception cref="ArgumentNullException">The <paramref name="translator"/> or <paramref name="source"/> is /// <c>null</c>.</exception> public static async Task <List <TDestination> > MapListAsync <TSource, TDestination>( this IAsyncMapper <TSource, TDestination> translator, IEnumerable <TSource> source) where TDestination : new() { if (translator == null) { throw new ArgumentNullException(nameof(translator)); } if (source == null) { throw new ArgumentNullException(nameof(source)); } var sourceCount = source.Count(); var tasks = new Task[sourceCount]; var destination = new List <TDestination>(sourceCount); var i = 0; foreach (var sourceItem in source) { var destinationItem = Factory <TDestination> .CreateInstance(); destination.Insert(i, destinationItem); tasks[i] = translator.MapAsync(sourceItem, destinationItem); ++i; } await Task.WhenAll(tasks).ConfigureAwait(false); return(destination); }
/// <summary> /// Maps the enumerable of <typeparamref name="TSource"/> into a list of /// <typeparamref name="TDestination"/>. /// </summary> /// <typeparam name="TSource">The type of the source objects.</typeparam> /// <typeparam name="TDestination">The type of the destination objects.</typeparam> /// <param name="mapper">The mapper.</param> /// <param name="source">The source objects.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>A list of <typeparamref name="TDestination"/>.</returns> /// <exception cref="ArgumentNullException">The <paramref name="mapper"/> or <paramref name="source"/> is /// <c>null</c>.</exception> public static async UniTask <List <TDestination> > MapListAsync <TSource, TDestination>( this IAsyncMapper <TSource, TDestination> mapper, IEnumerable <TSource> source, CancellationToken cancellationToken = default) where TDestination : new() { if (mapper == null) { throw new ArgumentNullException(nameof(mapper)); } if (source == null) { throw new ArgumentNullException(nameof(source)); } var sourceCount = source.Count(); var tasks = new UniTask[sourceCount]; var destination = new List <TDestination>(sourceCount); var i = 0; foreach (var sourceItem in source) { var destinationItem = new TDestination(); destination.Insert(i, destinationItem); tasks[i] = mapper.MapAsync(sourceItem, destinationItem, cancellationToken); ++i; } await UniTask.WhenAll(tasks); return(destination); }
/// <summary> /// Maps the array of <typeparamref name="TSource"/> into an array of /// <typeparamref name="TDestination"/>. /// </summary> /// <typeparam name="TSource">The type of the source objects.</typeparam> /// <typeparam name="TDestination">The type of the destination objects.</typeparam> /// <param name="mapper">The mapper.</param> /// <param name="source">The source objects.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>An array of <typeparamref name="TDestination"/>.</returns> /// <exception cref="ArgumentNullException">The <paramref name="mapper"/> or <paramref name="source"/> is /// <c>null</c>.</exception> public static async UniTask <TDestination[]> MapArrayAsync <TSource, TDestination>( this IAsyncMapper <TSource, TDestination> mapper, TSource[] source, CancellationToken cancellationToken = default) where TDestination : new() { if (mapper == null) { throw new ArgumentNullException(nameof(mapper)); } if (source == null) { throw new ArgumentNullException(nameof(source)); } var sourceCount = source.Length; var tasks = new UniTask[sourceCount]; var destination = new TDestination[sourceCount]; for (var i = 0; i < sourceCount; ++i) { var sourceItem = source[i]; var destinationItem = new TDestination(); destination[i] = destinationItem; tasks[i] = mapper.MapAsync(sourceItem, destinationItem, cancellationToken); } await UniTask.WhenAll(tasks); return(destination); }
/// <summary> /// Maps the specified source object to a new object with a type of <typeparamref name="TDestination"/>. /// </summary> /// <typeparam name="TSource">The type of the source object.</typeparam> /// <typeparam name="TDestination">The type of the destination object.</typeparam> /// <param name="mapper">The mapper.</param> /// <param name="source">The source object.</param> /// <returns>The mapped object of type <typeparamref name="TDestination"/>.</returns> /// <exception cref="ArgumentNullException">The <paramref name="mapper" /> or <paramref name="source" /> is /// <c>null</c>.</exception> public static async Task <TDestination> MapAsync <TSource, TDestination>( this IAsyncMapper <TSource, TDestination> mapper, TSource source) where TDestination : new() { if (mapper is null) { throw new ArgumentNullException(nameof(mapper)); } if (source is null) { throw new ArgumentNullException(nameof(source)); } var destination = Factory <TDestination> .CreateInstance(); await mapper.MapAsync(source, destination).ConfigureAwait(false); return(destination); }
/// <summary> /// Maps the specified source object to a new object with a type of <typeparamref name="TDestination"/>. /// </summary> /// <typeparam name="TSource">The type of the source object.</typeparam> /// <typeparam name="TDestination">The type of the destination object.</typeparam> /// <param name="mapper">The mapper.</param> /// <param name="source">The source object.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The mapped object of type <typeparamref name="TDestination"/>.</returns> /// <exception cref="ArgumentNullException">The <paramref name="mapper" /> or <paramref name="source" /> is /// <c>null</c>.</exception> public static async UniTask <TDestination> MapAsync <TSource, TDestination>( this IAsyncMapper <TSource, TDestination> mapper, TSource source, CancellationToken cancellationToken = default) where TDestination : new() { if (mapper == null) { throw new ArgumentNullException(nameof(mapper)); } if (source == null) { throw new ArgumentNullException(nameof(source)); } var destination = new TDestination(); await mapper.MapAsync(source, destination, cancellationToken); return(destination); }
/// <summary> /// Maps the collection of <typeparamref name="TSource"/> into an array of /// <typeparamref name="TDestination"/>. /// </summary> /// <typeparam name="TSourceCollection">The type of the source collection.</typeparam> /// <typeparam name="TSource">The type of the source objects.</typeparam> /// <typeparam name="TDestination">The type of the destination objects.</typeparam> /// <param name="translator">The translator.</param> /// <param name="source">The source collection.</param> /// <param name="destination">The destination collection.</param> /// <param name="sourceCount">The number of items in the source collection.</param> /// <returns>An array of <typeparamref name="TDestination"/>.</returns> /// <exception cref="ArgumentNullException">The <paramref name="translator"/> or <paramref name="source"/> is /// <c>null</c>.</exception> public static async Task <TDestination[]> MapArrayAsync <TSourceCollection, TSource, TDestination>( this IAsyncMapper <TSource, TDestination> translator, TSourceCollection source, TDestination[] destination, int?sourceCount = null) where TSourceCollection : IEnumerable <TSource> where TDestination : new() { if (translator == null) { throw new ArgumentNullException(nameof(translator)); } if (source == null) { throw new ArgumentNullException(nameof(source)); } if (destination == null) { throw new ArgumentNullException(nameof(destination)); } var tasks = new Task[sourceCount ?? source.Count()]; var i = 0; foreach (var sourceItem in source) { var destinationItem = Factory <TDestination> .CreateInstance(); destination[i] = destinationItem; tasks[i] = translator.MapAsync(sourceItem, destinationItem); ++i; } await Task.WhenAll(tasks).ConfigureAwait(false); return(destination); }
/// <summary> /// Maps the collection of <typeparamref name="TSource"/> into an array of /// <typeparamref name="TDestination"/>. /// </summary> /// <typeparam name="TSourceCollection">The type of the source collection.</typeparam> /// <typeparam name="TSource">The type of the source objects.</typeparam> /// <typeparam name="TDestination">The type of the destination objects.</typeparam> /// <param name="mapper">The mapper.</param> /// <param name="source">The source collection.</param> /// <param name="destination">The destination collection.</param> /// <param name="sourceCount">The number of items in the source collection.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>An array of <typeparamref name="TDestination"/>.</returns> /// <exception cref="ArgumentNullException">The <paramref name="mapper"/> or <paramref name="source"/> is /// <c>null</c>.</exception> public static async UniTask <TDestination[]> MapArrayAsync <TSourceCollection, TSource, TDestination>( this IAsyncMapper <TSource, TDestination> mapper, TSourceCollection source, TDestination[] destination, int?sourceCount = null, CancellationToken cancellationToken = default) where TSourceCollection : IEnumerable <TSource> where TDestination : new() { if (mapper == null) { throw new ArgumentNullException(nameof(mapper)); } if (source == null) { throw new ArgumentNullException(nameof(source)); } if (destination == null) { throw new ArgumentNullException(nameof(destination)); } var tasks = new UniTask[sourceCount ?? source.Count()]; var i = 0; foreach (var sourceItem in source) { var destinationItem = new TDestination(); destination[i] = destinationItem; tasks[i] = mapper.MapAsync(sourceItem, destinationItem, cancellationToken); ++i; } await UniTask.WhenAll(tasks); return(destination); }