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