| @@ -1,66 +1,71 | |||
| 
             | 
        1 | 1 | using System; | 
| 
             | 
        2 | 2 | using System.Collections.Generic; | 
| 
             | 
        3 | 3 | using System.Linq; | 
| 
             | 
        4 | 4 | using System.Text; | 
| 
             | 
        5 | 5 | using System.Text.RegularExpressions; | 
| 
             | 
        6 | 6 | using System.Diagnostics; | 
| 
             | 
        7 | 7 | |
| 
             | 
        8 | 8 | namespace Implab | 
| 
             | 
        9 | 9 | { | 
| 
             | 
        10 | 10 | public static class Safe | 
| 
             | 
        11 | 11 | { | 
| 
             | 
        12 | 12 | public static void ArgumentMatch(string param, string name, Regex rx) { | 
| 
             | 
        13 | 13 | if (rx == null) | 
| 
             | 
        14 | 14 | throw new ArgumentNullException("rx"); | 
| 
             | 
        15 | 15 | if (!rx.IsMatch(param)) | 
| 
             | 
        16 | 
            
                             throw new ArgumentException(String.Format(" | 
    |
| 
             | 
        16 | throw new ArgumentException(String.Format("The prameter value must match {0}", rx), name); | |
| 
             | 
        17 | 17 | } | 
| 
             | 
        18 | 18 | |
| 
             | 
        19 | 19 | public static void ArgumentNotEmpty(string param, string name) { | 
| 
             | 
        20 | 20 | if (String.IsNullOrEmpty(param)) | 
| 
             | 
        21 | 
            
                             throw new ArgumentException(" | 
    |
| 
             | 
        21 | throw new ArgumentException("The parameter can't be empty", name); | |
| 
             | 
        22 | } | |
| 
             | 
        23 | ||
| 
             | 
        24 | public static void ArgumentNotEmpty<T>(T[] param, string name) { | |
| 
             | 
        25 | if (param == null || param.Length == 0) | |
| 
             | 
        26 | throw new ArgumentException("The array must be not emty"); | |
| 
             | 
        22 | 27 | } | 
| 
             | 
        23 | 28 | |
| 
             | 
        24 | 29 | public static void ArgumentNotNull(object param, string name) { | 
| 
             | 
        25 | 30 | if (param == null) | 
| 
             | 
        26 | 31 | throw new ArgumentNullException(name); | 
| 
             | 
        27 | 32 | } | 
| 
             | 
        28 | 33 | |
| 
             | 
        29 | 34 | public static void ArgumentInRange(int arg, int min, int max, string name) { | 
| 
             | 
        30 | 35 | if (arg < min || arg > max) | 
| 
             | 
        31 | 36 | throw new ArgumentOutOfRangeException(name); | 
| 
             | 
        32 | 37 | } | 
| 
             | 
        33 | 38 | |
| 
             | 
        34 | 39 | public static void Dispose<T>(T obj) where T : class | 
| 
             | 
        35 | 40 | { | 
| 
             | 
        36 | 41 | var disp = obj as IDisposable; | 
| 
             | 
        37 | 42 | if (disp != null) | 
| 
             | 
        38 | 43 | disp.Dispose(); | 
| 
             | 
        39 | 44 | } | 
| 
             | 
        40 | 45 | |
| 
             | 
        41 | 46 | [DebuggerStepThrough] | 
| 
             | 
        42 | 47 | public static IPromise<T> GuargPromise<T>(Func<T> action) { | 
| 
             | 
        43 | 48 | ArgumentNotNull(action, "action"); | 
| 
             | 
        44 | 49 | |
| 
             | 
        45 | 50 | var p = new Promise<T>(); | 
| 
             | 
        46 | 51 | try { | 
| 
             | 
        47 | 52 | p.Resolve(action()); | 
| 
             | 
        48 | 53 | } catch (Exception err) { | 
| 
             | 
        49 | 54 | p.Reject(err); | 
| 
             | 
        50 | 55 | } | 
| 
             | 
        51 | 56 | |
| 
             | 
        52 | 57 | return p; | 
| 
             | 
        53 | 58 | } | 
| 
             | 
        54 | 59 | |
| 
             | 
        55 | 60 | [DebuggerStepThrough] | 
| 
             | 
        56 | 61 | public static IPromise<T> GuardPromise<T>(Func<IPromise<T>> action) { | 
| 
             | 
        57 | 62 | ArgumentNotNull(action, "action"); | 
| 
             | 
        58 | 63 | |
| 
             | 
        59 | 64 | try { | 
| 
             | 
        60 | 65 | return action(); | 
| 
             | 
        61 | 66 | } catch (Exception err) { | 
| 
             | 
        62 | 67 | return Promise<T>.ExceptionToPromise(err); | 
| 
             | 
        63 | 68 | } | 
| 
             | 
        64 | 69 | } | 
| 
             | 
        65 | 70 | } | 
| 
             | 
        66 | 71 | } | 
        
        General Comments 0
    
    
  
  
                      You need to be logged in to leave comments.
                      Login now
                    
                