wasSharp

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 55  →  ?path2? @ 54
/Collections/Specialized/ConcurrentQueueChangedEventHandler.cs
@@ -0,0 +1,27 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ObservableConcurrentQueue.cs" company="BledSoft">
// This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
// To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/.
// </copyright>
// <Author>
// Cheikh Younes
// </Author>
// --------------------------------------------------------------------------------------------------------------------
namespace wasSharp.Collections.Specialized
{
/// <summary>
/// Observable Concurrent queue changed event handler
/// </summary>
/// <typeparam name="T">
/// The concurrent queue elements type
/// </typeparam>
/// <param name="sender">
/// The sender.
/// </param>
/// <param name="args">
/// The <see cref="NotifyConcurrentQueueChangedEventArgs{T}"/> instance containing the event data.
/// </param>
public delegate void ConcurrentQueueChangedEventHandler<T>(
object sender,
NotifyConcurrentQueueChangedEventArgs<T> args);
}
/Collections/Specialized/NotifyConcurrentQueueChangedAction.cs
@@ -0,0 +1,37 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ObservableConcurrentQueue.cs" company="BledSoft">
// This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
// To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/.
// </copyright>
// <Author>
// Cheikh Younes
// </Author>
// --------------------------------------------------------------------------------------------------------------------
namespace wasSharp.Collections.Specialized
{
/// <summary>
/// The notify concurrent queue changed action.
/// </summary>
public enum NotifyConcurrentQueueChangedAction
{
/// <summary>
/// The enqueue
/// </summary>
Enqueue,
 
/// <summary>
/// The de-queue
/// </summary>
Dequeue,
 
/// <summary>
/// The peek
/// </summary>
Peek,
 
/// <summary>
/// The empty
/// </summary>
Empty
}
}
/Collections/Specialized/NotifyConcurrentQueueChangedEventArgs.cs
@@ -0,0 +1,72 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ObservableConcurrentQueue.cs" company="BledSoft">
// This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
// To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/.
// </copyright>
// <Author>
// Cheikh Younes
// </Author>
// --------------------------------------------------------------------------------------------------------------------
using System;
 
namespace wasSharp.Collections.Specialized
{
/// <summary>
/// The notify concurrent queue changed event args.
/// </summary>
/// <typeparam name="T">
/// The item type
/// </typeparam>
public class NotifyConcurrentQueueChangedEventArgs<T> : EventArgs
{
#region Constructors and Destructors
 
/// <summary>
/// Initializes a new instance of the <see cref="NotifyConcurrentQueueChangedEventArgs{T}"/> class.
/// </summary>
/// <param name="action">
/// The action.
/// </param>
/// <param name="changedItem">
/// The changed item.
/// </param>
public NotifyConcurrentQueueChangedEventArgs(NotifyConcurrentQueueChangedAction action, T changedItem)
{
this.Action = action;
this.ChangedItem = changedItem;
}
 
/// <summary>
/// Initializes a new instance of the <see cref="NotifyConcurrentQueueChangedEventArgs{T}"/> class.
/// </summary>
/// <param name="action">
/// The action.
/// </param>
public NotifyConcurrentQueueChangedEventArgs(NotifyConcurrentQueueChangedAction action)
{
this.Action = action;
}
 
#endregion
 
#region Public Properties
 
/// <summary>
/// Gets the action.
/// </summary>
/// <value>
/// The action.
/// </value>
public NotifyConcurrentQueueChangedAction Action { get; private set; }
 
/// <summary>
/// Gets the changed item.
/// </summary>
/// <value>
/// The changed item.
/// </value>
public T ChangedItem { get; private set; }
 
#endregion
}
}
/Collections/Specialized/ObservableConcurrentQueue.cs
@@ -1,12 +1,14 @@
using System;
using System.Threading;
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ObservableConcurrentQueue.cs" company="BledSoft">
// This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
// To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/.
// </copyright>
// <Author>
// Cheikh Younes
// </Author>
// --------------------------------------------------------------------------------------------------------------------
using System.Collections.Concurrent;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Collections.Concurrent;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
 
namespace wasSharp.Collections.Specialized
{
@@ -16,71 +18,114 @@
/// <typeparam name="T">
/// The content type
/// </typeparam>
public sealed class ObservableConcurrentQueue<T> : ConcurrentQueue<T>, INotifyCollectionChanged
public sealed class ObservableConcurrentQueue<T> : ConcurrentQueue<T>
{
public event NotifyCollectionChangedEventHandler CollectionChanged;
#region Public Events
 
private new void Enqueue(T item)
/// <summary>
/// Occurs when concurrent queue elements [changed].
/// </summary>
public event ConcurrentQueueChangedEventHandler<T> ContentChanged;
 
#endregion
 
#region Public Methods and Operators
 
/// <summary>
/// Adds an object to the end of the <see cref="T:System.Collections.Concurrent.ConcurrentQueue`1"/>.
/// </summary>
/// <param name="item">
/// The object to add to the end of the <see cref="T:System.Collections.Concurrent.ConcurrentQueue`1"/>
/// . The value can be a null reference (Nothing in Visual Basic) for reference types.
/// </param>
public new async Task Enqueue(T item)
{
EnqueueAsync(item).RunSynchronously();
}
 
public async Task EnqueueAsync(T item) => await Task.Run(() =>
await new Task(() =>
{
base.Enqueue(item);
 
OnCollectionChanged(
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
// Raise event added event
this.OnContentChanged(
new NotifyConcurrentQueueChangedEventArgs<T>(NotifyConcurrentQueueChangedAction.Enqueue, item));
});
}
 
private new bool TryDequeue(out T result)
/// <summary>
/// Attempts to remove and return the object at the beginning of the
/// <see cref="T:System.Collections.Concurrent.ConcurrentQueue`1"/>.
/// </summary>
/// <param name="result">
/// When this method returns, if the operation was successful, <paramref name="result"/> contains the
/// object removed. If no object was available to be removed, the value is unspecified.
/// </param>
/// <returns>
/// true if an element was removed and returned from the beginning of the
/// <see cref="T:System.Collections.Concurrent.ConcurrentQueue`1"/> successfully; otherwise, false.
/// </returns>
public new bool TryDequeue(out T result)
{
result = DequeueAsync().Result;
if (result.Equals(default(T)))
if (!base.TryDequeue(out result))
{
return false;
}
 
return true;
}
// Raise item dequeued event
this.OnContentChanged(
new NotifyConcurrentQueueChangedEventArgs<T>(NotifyConcurrentQueueChangedAction.Dequeue, result));
 
public async Task<T> DequeueAsync() => await Task.Run(() =>
if (this.IsEmpty)
{
if (!base.TryDequeue(out T item))
return default(T);
// Raise Queue empty event
this.OnContentChanged(
new NotifyConcurrentQueueChangedEventArgs<T>(NotifyConcurrentQueueChangedAction.Empty));
}
 
OnCollectionChanged(
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item));
return true;
}
 
return item;
});
 
public async Task<T> PeekAsync() => await Task.Run(() =>
/// <summary>
/// Attempts to return an object from the beginning of the
/// <see cref="T:System.Collections.Concurrent.ConcurrentQueue`1"/> without removing it.
/// </summary>
/// <param name="result">
/// When this method returns, <paramref name="result"/> contains an object from the beginning of the
/// <see cref="T:System.Collections.Concurrent.ConcurrentQueue`1"/> or an unspecified value if the operation failed.
/// </param>
/// <returns>
/// true if and object was returned successfully; otherwise, false.
/// </returns>
public new bool TryPeek(out T result)
{
if (!base.TryPeek(out T item))
return default(T);
var retValue = base.TryPeek(out result);
if (retValue)
{
// Raise item dequeued event
this.OnContentChanged(
new NotifyConcurrentQueueChangedEventArgs<T>(NotifyConcurrentQueueChangedAction.Peek, result));
}
 
return item;
});
return retValue;
}
 
private new bool TryPeek(out T result)
{
result = PeekAsync().Result;
#endregion
 
if (result.Equals(default(T)))
return false;
#region Methods
 
return true;
}
 
private void OnCollectionChanged(NotifyCollectionChangedEventArgs args)
/// <summary>
/// Raises the <see cref="E:Changed"/> event.
/// </summary>
/// <param name="args">
/// The <see cref="NotifyConcurrentQueueChangedEventArgs{T}"/> instance containing the event data.
/// </param>
private void OnContentChanged(NotifyConcurrentQueueChangedEventArgs<T> args)
{
CollectionChanged?.Invoke(this, args);
var handler = this.ContentChanged;
if (handler != null)
{
handler(this, args);
}
}
 
 
public async Task Clear()
{
while (!base.IsEmpty)
await DequeueAsync();
}
#endregion
}
}
/Languages/XML.cs
@@ -13,7 +13,7 @@
using System.Threading.Tasks;
using System.Xml.Linq;
 
namespace wasSharp.Languages
namespace wasSharp
{
public static class XML
{
/Languages/CSV.cs
@@ -8,7 +8,7 @@
using System.Linq;
using System.Text;
 
namespace wasSharp.Languages
namespace wasSharp
{
public static class CSV
{
/Languages/KeyValue.cs
@@ -8,7 +8,7 @@
using System.Collections.Generic;
using System.Linq;
 
namespace wasSharp.Languages
namespace wasSharp
{
public static class KeyValue
{
/Web/wasHTTPClient.cs
@@ -12,7 +12,6 @@
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using wasSharp.Languages;
 
namespace wasSharp.Web
{
/wasSharp.csproj
@@ -73,6 +73,9 @@
<Compile Include="Cryptography\Extensions.cs" />
<Compile Include="Sets\Extensions.cs" />
<Compile Include="Collections\Specialized\ObservableConcurrentQueue.cs" />
<Compile Include="Collections\Specialized\NotifyConcurrentQueueChangedEventArgs.cs" />
<Compile Include="Collections\Specialized\NotifyConcurrentQueueChangedAction.cs" />
<Compile Include="Collections\Specialized\ConcurrentQueueChangedEventHandler.cs" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
/Lambda/Extensions.cs
@@ -207,22 +207,6 @@
/// <param name="pass">function with no parameters and return type T in case condition passes</param>
/// <param name="fail">function with no parameters and return type T in case condition fails</param>
/// <returns>the result of pass in case condition holds or the result of fail otherwise</returns>
public static V IfElse<T, V>(this T arg, Func<T, bool> condition, Func<T, V> pass, Func<T, V> fail)
{
return condition.Invoke(arg) ? pass.Invoke(arg) : fail.Invoke(arg);
}
 
///////////////////////////////////////////////////////////////////////////
// Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
/// <summary>
/// Invokes pass if and only if condition holds or fail otherwise.
/// </summary>
/// <typeparam name="T">the return type of the pass and fail functions</typeparam>
/// <param name="condition">the branch condition</param>
/// <param name="pass">function with no parameters and return type T in case condition passes</param>
/// <param name="fail">function with no parameters and return type T in case condition fails</param>
/// <returns>the result of pass in case condition holds or the result of fail otherwise</returns>
public static T IfElse<T>(this bool condition, Func<T> pass, Func<T> fail)
{
return condition ? pass.Invoke() : fail.Invoke();