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