Exemplo n.º 1
0
 public static MaybeNotNull <K> SelectMany <T, K>(this MaybeNotNull <T> source, Func <T, K> maybeSelector)
 {
     //Contract.Requires( source != null );
     //Contract.Requires( maybeSelector != null );
     //Contract.Ensures( //Contract.Result<MaybeNotNull<K>>() != null );
     return(source.SelectMany(maybeSelector, (_, x) => x));
 }
Exemplo n.º 2
0
 public static MaybeNotNull <R> SelectMany <T, K, R>(this MaybeNotNull <T> source, Func <T, K> maybeSelector, Func <T, K, R> resultSelector)
 {
     //Contract.Requires( source != null );
     //Contract.Requires( maybeSelector != null );
     //Contract.Requires( resultSelector != null );
     return(source.SelectMany(t => Value(maybeSelector(t)), resultSelector));
 }
Exemplo n.º 3
0
        public static MaybeNotNull <T> Catch <T, TException>(this MaybeNotNull <T> source, Func <TException, T> getValue)
            where TException : Exception
        {
            //Contract.Requires( source != null );
            //Contract.Ensures( //Contract.Result<MaybeNotNull<T>>() != null );

            return(source.Catch((TException ex) => Maybe.Value(getValue(ex))));
        }
Exemplo n.º 4
0
        public static MaybeNotNull <T> Catch <T>(this MaybeNotNull <T> source, MaybeNotNull <T> defaultValue)
        {
            //Contract.Requires( source != null );
            //Contract.Requires( defaultValue != null );
            //Contract.Ensures( //Contract.Result<MaybeNotNull<T>>() != null );

            return(source.Kind == MaybeKind.Error ? defaultValue : source);
        }
Exemplo n.º 5
0
 public static MaybeNotNull <T> Where <T>(this MaybeNotNull <T> source, Func <T, bool> predicate)
 {
     //Contract.Requires( source != null );
     //Contract.Requires( predicate != null );
     //Contract.Ensures( //Contract.Result<MaybeNotNull<T>>() != null );
     return(from t in source
            from _ in predicate(t) ? "" : null
            select t);
 }
Exemplo n.º 6
0
        public static OpResult Lift(this MaybeNotNull <OpResult> maybe, OpResult nullResult = null)
        {
            //Contract.Requires( maybe != null );
            //Contract.Ensures( //Contract.Result<OpResult>() != null );

            return
                (maybe.Kind == MaybeKind.Error ? OpResult.FromException(maybe.Exception) :
                 (maybe.ValueOrNull() ?? nullResult ?? OpResult.Failed("Unknown error has occurred")));
        }
Exemplo n.º 7
0
        public static MaybeNotNull <T> OrMaybe <T>(this MaybeNotNull <T> source, Func <MaybeNotNull <T> > whenNull)
        {
            //Contract.Requires( source != null );
            //Contract.Requires( whenNull != null );

            if (source.Kind == MaybeKind.Null)
            {
                return(whenNull());
            }
            return(source);
        }
Exemplo n.º 8
0
        public static MaybeNotNull <T> Catch <T, TException>(this MaybeNotNull <T> source, Func <TException, MaybeNotNull <T> > getValue)
            where TException : Exception
        {
            //Contract.Requires( source != null );
            //Contract.Ensures( //Contract.Result<MaybeNotNull<T>>() != null );

            if (source.Kind == MaybeKind.Error)
            {
                var e = source.Exception as TException;
                if (e != null)
                {
                    return(getValue(e));
                }
            }

            return(source);
        }
Exemplo n.º 9
0
 public override MaybeNotNull <R> SelectMany <K, R>(Func <T, MaybeNotNull <K> > maybeSelector, Func <T, K, R> resultSelector)
 {
     try
     {
         var k = maybeSelector(this.Value);
         if (k.Kind == MaybeKind.Value)
         {
             var res = resultSelector(this.Value, k.Value);
             return(isNull(res) ? MaybeNotNull <R> .MakeNull() : MaybeNotNull <R> .MakeValue(res));
         }
         else
         {
             return(k.SelectMany <R, R>(null, null));
         }
     }
     catch (Exception ex)
     {
         return(MaybeNotNull <R> .MakeError(ex));
     }
 }
Exemplo n.º 10
0
 public static T ValueOrDefault <T>(this MaybeNotNull <T> maybe, T defaultValue)
 {
     //Contract.Requires( maybe != null );
     return(maybe.Kind == MaybeKind.Null ? defaultValue : maybe.Value);
 }
Exemplo n.º 11
0
 public static MaybeNotNull <T> Or <T>(this MaybeNotNull <T> source, Func <T> whenNull)
 {
     //Contract.Requires( source != null );
     //Contract.Requires( whenNull != null );
     return(source.OrMaybe(() => Maybe.Value(whenNull())));
 }
Exemplo n.º 12
0
 public static MaybeNotNull <T> Value <T>(T value)
 {
     return(value == null?Null <T>() : MaybeNotNull <T> .MakeValue(value));
 }
Exemplo n.º 13
0
 public static T?AsNullable <T>(this MaybeNotNull <T?> source) where T : struct
 {
     //Contract.Requires( source != null );
     return(source.Kind == MaybeKind.Null ? new T?() : source.Value);
 }
Exemplo n.º 14
0
 public static MaybeNotNull <T> Throw <T>(Exception ex)
 {
     return(MaybeNotNull <T> .MakeError(ex));
 }
Exemplo n.º 15
0
 public static MaybeNotNull <T> Do <T>(this MaybeNotNull <T> source, Action <T> a)
 {
     //Contract.Requires( source != null );
     //Contract.Requires( a != null );
     return(source.Select(x => { a(x); return x; }));
 }
Exemplo n.º 16
0
 public static MaybeNotNull <T> Catch <T>(this MaybeNotNull <T> source, T defaultValue)
 {
     //Contract.Requires( source != null );
     //Contract.Ensures( //Contract.Result<MaybeNotNull<T>>() != null );
     return(source.Catch(Maybe.Value(defaultValue)));
 }
Exemplo n.º 17
0
 public static T?ValueOrNull <T>(this MaybeNotNull <T?> maybe) where T : struct
 {
     //Contract.Requires( maybe != null );
     return(maybe.Kind == MaybeKind.Null ? null : maybe.Value);
 }
Exemplo n.º 18
0
 public static T ValueOrDefault <T>(this MaybeNotNull <T> maybe) where T : struct
 {
     //Contract.Requires( maybe != null );
     return(maybe.Kind == MaybeKind.Null ? default(T) : maybe.Value);
 }
Exemplo n.º 19
0
 public static MaybeNotNull <T> Null <T>()
 {
     return(MaybeNotNull <T> .MakeNull());
 }