@@ -1,86 +1,86 | |||
|
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 value, string paramName, Regex rx) { |
|
13 | 13 | if (rx == null) |
|
14 | 14 | throw new ArgumentNullException("rx"); |
|
15 | 15 | if (!rx.IsMatch(value)) |
|
16 | 16 | throw new ArgumentException(String.Format("The prameter value must match {0}", rx), paramName); |
|
17 | 17 | } |
|
18 | 18 | |
|
19 | 19 | public static void ArgumentNotEmpty(string value, string paramName) { |
|
20 | 20 | if (String.IsNullOrEmpty(value)) |
|
21 | 21 | throw new ArgumentException("The parameter can't be empty", paramName); |
|
22 | 22 | } |
|
23 | 23 | |
|
24 | 24 | public static void ArgumentNotEmpty<T>(T[] value, string paramName) { |
|
25 | 25 | if (value == null || value.Length == 0) |
|
26 | 26 | throw new ArgumentException("The array must be not emty", paramName); |
|
27 | 27 | } |
|
28 | 28 | |
|
29 | 29 | public static void ArgumentNotNull(object value, string paramName) { |
|
30 | 30 | if (value == null) |
|
31 | 31 | throw new ArgumentNullException(paramName); |
|
32 | 32 | } |
|
33 | 33 | |
|
34 | 34 | public static void ArgumentInRange(int value, int min, int max, string paramName) { |
|
35 | 35 | if (value < min || value > max) |
|
36 | 36 | throw new ArgumentOutOfRangeException(paramName); |
|
37 | 37 | } |
|
38 | 38 | |
|
39 |
public static void Dispose |
|
|
39 | public static void Dispose(params IDisposable[] objects) | |
|
40 | 40 | { |
|
41 | var disp = obj as IDisposable; | |
|
42 |
if (d |
|
|
43 |
d |
|
|
41 | foreach(var d in objects) | |
|
42 | if (d != null) | |
|
43 | d.Dispose(); | |
|
44 | 44 | } |
|
45 | 45 | |
|
46 | 46 | [DebuggerStepThrough] |
|
47 | 47 | public static IPromise<T> InvokePromise<T>(Func<T> action) { |
|
48 | 48 | ArgumentNotNull(action, "action"); |
|
49 | 49 | |
|
50 | 50 | var p = new Promise<T>(); |
|
51 | 51 | try { |
|
52 | 52 | p.Resolve(action()); |
|
53 | 53 | } catch (Exception err) { |
|
54 | 54 | p.Reject(err); |
|
55 | 55 | } |
|
56 | 56 | |
|
57 | 57 | return p; |
|
58 | 58 | } |
|
59 | 59 | |
|
60 | 60 | [DebuggerStepThrough] |
|
61 | 61 | public static IPromise InvokePromise(Action action) { |
|
62 | 62 | ArgumentNotNull(action, "action"); |
|
63 | 63 | |
|
64 | 64 | var p = new Promise(); |
|
65 | 65 | try { |
|
66 | 66 | action(); |
|
67 | 67 | p.Resolve(); |
|
68 | 68 | } catch (Exception err) { |
|
69 | 69 | p.Reject(err); |
|
70 | 70 | } |
|
71 | 71 | |
|
72 | 72 | return p; |
|
73 | 73 | } |
|
74 | 74 | |
|
75 | 75 | [DebuggerStepThrough] |
|
76 | 76 | public static IPromise<T> InvokePromise<T>(Func<IPromise<T>> action) { |
|
77 | 77 | ArgumentNotNull(action, "action"); |
|
78 | 78 | |
|
79 | 79 | try { |
|
80 | 80 | return action() ?? Promise<T>.ExceptionToPromise(new Exception("The action returned null")); |
|
81 | 81 | } catch (Exception err) { |
|
82 | 82 | return Promise<T>.ExceptionToPromise(err); |
|
83 | 83 | } |
|
84 | 84 | } |
|
85 | 85 | } |
|
86 | 86 | } |
General Comments 0
You need to be logged in to leave comments.
Login now