Skip to content

Commit

Permalink
SwitchCaseDefault
Browse files Browse the repository at this point in the history
Instance has been created as double-checked locking
  • Loading branch information
ArisAgnew committed Feb 16, 2020
1 parent 50ddd79 commit 879f7a3
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions SwitchFunc/SwitchCaseDefault.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Immutable;
using System.Linq;

using System.Threading;
using static System.Threading.LazyThreadSafetyMode;

namespace SwitchFunc
Expand All @@ -16,6 +16,8 @@ namespace SwitchFunc
/// </remarks>
public sealed partial class SwitchCaseDefault<V> : SwitchFactory<V>, ISwitch<V>, ICase<V>, IDefault<V>
{
private static readonly object syncRoot = new object();

private static readonly ImmutableList<V>.Builder argsBuilder = ImmutableList.CreateBuilder<V>();
private static readonly SwitchCaseDefault<V> EMPTY = new SwitchCaseDefault<V>(default);

Expand All @@ -40,8 +42,25 @@ private SwitchCaseDefault() { }

private Action ResetArgumentList => () => argsBuilder?.Clear();

public static SwitchCaseDefault<V> Of(V arg) =>
new Lazy<SwitchCaseDefault<V>>(() => new SwitchCaseDefault<V>(arg), PublicationOnly).Value;
private static SwitchCaseDefault<V> Instance
{
get
{
if (instance == default)
{
Monitor.Enter(syncRoot);
SwitchCaseDefault<V> temp = new SwitchCaseDefault<V>();
Interlocked.Exchange(ref instance, temp);
Monitor.Exit(syncRoot);

return instance;
}

return instance;
}
}

public static SwitchCaseDefault<V> Of(V arg) => Instance;

public static SwitchCaseDefault<V> OfNullable(V arg) => arg != null ? Of(arg) : EMPTY;
public static SwitchCaseDefault<V> OfNullable(Func<V> outputValue) => outputValue != null ? Of(outputValue()) : EMPTY;
Expand Down Expand Up @@ -113,8 +132,8 @@ public ICase<V> Peek(in Action<V> action)
//It goes after default end-point
IDefault<V> IDefault<V>.Peek(in Action<V> action) => Peek(action) as IDefault<V> ?? default;

public V GetSwitch() => switchValue.Equals(default(V)) || switchValue == null ? switchValueGhost : switchValue;
public V GetCase() => caseValue.Equals(default(V)) || caseValue == null ? caseValueGhost : caseValue;
public V GetSwitch() => switchValue.Equals(default(V)) ? switchValueGhost : switchValue;
public V GetCase() => caseValue.Equals(default(V)) ? caseValueGhost : caseValue;

public V GetSwitchCustomized(in Func<V, V> funcCustom) => !IsSwitchValueNull ? funcCustom(GetSwitch()) : default;
public U GetSwitchCustomized<U>(in Func<V, U> funcCustom) => !IsSwitchValueNull ? funcCustom(GetSwitch()) : default;
Expand Down

0 comments on commit 879f7a3

Please sign in to comment.