using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; namespace ScreenConnect; public static class FunctionalExtensions { public static T? As(this object? @this) where T : class { return @this as T; } public static T? AsNullable(this T @this) where T : struct { return @this; } public static T? AsNullable(this object? @this) where T : struct { return @this as T?; } public static T? ToNullable(this T @this) where T : class? { return @this; } [return: NotNullIfNotNull("this")] public static T? To(this object? @this) { return (T)@this; } public static T? If(this T @this, Func predicate) where T : class { if (!predicate(@this)) { return null; } return @this; } public static T? IfNullable(this T @this, Func predicate) where T : struct { if (!predicate(@this)) { return null; } return @this; } public static string? IfNotNullOrEmpty(this string? @this) { return @this?.IfNotEmpty(); } public static string? IfNotEmpty(this string? @this) { return @this?.If((string s) => s != string.Empty); } public static ICollection? IfNotEmpty(this ICollection? @this) { if (!@this.IsNullOrEmpty()) { return @this; } return null; } public static T? IfNotDefault(this T @this) where T : struct { if (!@this.EqualsDefault()) { return @this; } return null; } public static T? SafeIf(this T? @this, Func predicate) where T : class { if (@this == null) { return null; } return @this.If(predicate); } public static T Else(this T? @this, Func creator) where T : class { return @this ?? creator(); } public static T Else(this T? @this, Func creator) where T : struct { return @this ?? creator(); } public static T Else(this T? @this, T otherObj) where T : class { return @this ?? otherObj; } public static T Else(this T? @this, T otherObj) where T : struct { return @this ?? otherObj; } [return: NotNullIfNotNull("this")] public static T? ElseDo(this T? @this, Proc proc) where T : class { if (@this == null) { proc(); } return @this; } public static T SafeDo(this T obj, Proc action) { if (obj != null) { action(obj); } return obj; } public static U? SafeNav(this T obj, Func selector) { if (obj != null) { return selector(obj); } return default(U); } [Obsolete("Use SafePipe")] public static U SafeNav(this T obj, Func selector, U defaultValue) { if (obj != null) { return selector(obj); } return defaultValue; } public static U Pipe(this T @this, Func func) { return func(@this); } public static T PipeIf(this T @this, Func func, bool shouldInvoke) { if (!shouldInvoke) { return @this; } return func(@this); } public static T PipeIf(this T @this, Func func, Func predicate) { if (!predicate(@this)) { return @this; } return func(@this); } public static U? SafePipe(this T? @this, Func func) where T : class { if (@this != null) { return func(@this); } return default(U); } public static U? SafePipe(this T? @this, Func func) where T : struct { if (@this.HasValue) { return func(@this.Value); } return default(U); } public static U SafePipe(this T? @this, Func func, U defaultValue) where T : class { if (@this != null) { return func(@this); } return defaultValue; } public static U SafePipe(this T? @this, Func func, U defaultValue) where T : struct { if (@this.HasValue) { return func(@this.Value); } return defaultValue; } public static U? TryPipe(this T @this, Func func, U? defaultValue = default(U?)) { return Extensions.TryGet((Func)(() => @this.Pipe(func)), defaultValue, true); } public static U? TrySafePipe(this T? @this, Func func, U? defaultValue = default(U?)) where T : class { return @this.SafePipe((T it) => it.TryPipe(func, defaultValue), defaultValue); } public static U? TrySafePipe(this T? @this, Func func, U? defaultValue = default(U?)) where T : struct { return @this.SafePipe((T it) => it.TryPipe(func, defaultValue), defaultValue); } public static T Pass(this T @this, Proc interceptor) { interceptor(@this); return @this; } public static T PassIf(this T @this, Proc interceptor, bool shouldIntercept) { if (!shouldIntercept) { return @this; } return @this.Pass(interceptor); } public static T PassIf(this T @this, Proc interceptor, Func predicate) { return @this.PassIf(interceptor, predicate(@this)); } [return: NotNullIfNotNull("this")] public static T? SafePass(this T? @this, Proc interceptor) where T : class { if (@this == null) { return @this; } return @this.Pass(interceptor); } public static T? TryPass(this T @this, Proc interceptor) { return Extensions.TryGet(() => @this.Pass(interceptor), @this); } public static T? TrySafePass(this T? @this, Proc interceptor) where T : class { return @this.SafePass(delegate(T it) { it.TryPass(interceptor); }); } public static T Mutate(this T @this, Proc mutator) { mutator(@this); return @this; } public static T MutateIf(this T @this, Proc mutator, bool shouldMutate) { if (!shouldMutate) { return @this; } return @this.Mutate(mutator); } public static T MutateIf(this T @this, Proc mutator, Func predicate) { return @this.MutateIf(mutator, predicate(@this)); } [return: NotNullIfNotNull("this")] public static T? SafeMutate(this T? @this, Proc mutator) where T : class { if (@this == null) { return @this; } return @this.Mutate(mutator); } public static T TryMutate(this T @this, Proc mutator) { return Extensions.TryGet(() => @this.Mutate(mutator), @this); } public static T? TrySafeMutate(this T? @this, Proc mutator) where T : class { return @this.SafeMutate(delegate(T it) { it.TryMutate(mutator); }); } }