Auto status change to "Under Review"
| @@ -1,152 +1,152 | |||||
| 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 | using System.Collections; |
|
7 | using System.Collections; | |
| 8 |
|
8 | |||
| 9 | #if NET_4_5 |
|
9 | #if NET_4_5 | |
| 10 | using System.Threading.Tasks; |
|
10 | using System.Threading.Tasks; | |
| 11 | #endif |
|
11 | #endif | |
| 12 |
|
12 | |||
| 13 | namespace Implab |
|
13 | namespace Implab | |
| 14 | { |
|
14 | { | |
| 15 | public static class Safe |
|
15 | public static class Safe | |
| 16 | { |
|
16 | { | |
| 17 | public static void ArgumentAssert(bool condition, string paramName) { |
|
17 | public static void ArgumentAssert(bool condition, string paramName) { | |
| 18 | if (!condition) |
|
18 | if (!condition) | |
| 19 | throw new ArgumentException("The parameter is invalid", paramName); |
|
19 | throw new ArgumentException("The parameter is invalid", paramName); | |
| 20 | } |
|
20 | } | |
| 21 |
|
21 | |||
| 22 | public static void ArgumentMatch(string value, string paramName, Regex rx) { |
|
22 | public static void ArgumentMatch(string value, string paramName, Regex rx) { | |
| 23 | if (rx == null) |
|
23 | if (rx == null) | |
| 24 | throw new ArgumentNullException("rx"); |
|
24 | throw new ArgumentNullException("rx"); | |
| 25 | if (!rx.IsMatch(value)) |
|
25 | if (!rx.IsMatch(value)) | |
| 26 | throw new ArgumentException(String.Format("The prameter value must match {0}", rx), paramName); |
|
26 | throw new ArgumentException(String.Format("The prameter value must match {0}", rx), paramName); | |
| 27 | } |
|
27 | } | |
| 28 |
|
28 | |||
| 29 | public static void ArgumentNotEmpty(string value, string paramName) { |
|
29 | public static void ArgumentNotEmpty(string value, string paramName) { | |
| 30 | if (String.IsNullOrEmpty(value)) |
|
30 | if (String.IsNullOrEmpty(value)) | |
| 31 | throw new ArgumentException("The parameter can't be empty", paramName); |
|
31 | throw new ArgumentException("The parameter can't be empty", paramName); | |
| 32 | } |
|
32 | } | |
| 33 |
|
33 | |||
| 34 | public static void ArgumentNotEmpty<T>(T[] value, string paramName) { |
|
34 | public static void ArgumentNotEmpty<T>(T[] value, string paramName) { | |
| 35 | if (value == null || value.Length == 0) |
|
35 | if (value == null || value.Length == 0) | |
| 36 | throw new ArgumentException("The array must be not emty", paramName); |
|
36 | throw new ArgumentException("The array must be not emty", paramName); | |
| 37 | } |
|
37 | } | |
| 38 |
|
38 | |||
| 39 | public static void ArgumentNotNull(object value, string paramName) { |
|
39 | public static void ArgumentNotNull(object value, string paramName) { | |
| 40 | if (value == null) |
|
40 | if (value == null) | |
| 41 | throw new ArgumentNullException(paramName); |
|
41 | throw new ArgumentNullException(paramName); | |
| 42 | } |
|
42 | } | |
| 43 |
|
43 | |||
| 44 | public static void ArgumentInRange(int value, int min, int max, string paramName) { |
|
44 | public static void ArgumentInRange(int value, int min, int max, string paramName) { | |
| 45 | if (value < min || value > max) |
|
45 | if (value < min || value > max) | |
| 46 | throw new ArgumentOutOfRangeException(paramName); |
|
46 | throw new ArgumentOutOfRangeException(paramName); | |
| 47 | } |
|
47 | } | |
| 48 |
|
48 | |||
| 49 | public static void ArgumentOfType(object value, Type type, string paramName) { |
|
49 | public static void ArgumentOfType(object value, Type type, string paramName) { | |
| 50 | if (!type.IsInstanceOfType(value)) |
|
50 | if (!type.IsInstanceOfType(value)) | |
| 51 | throw new ArgumentException(String.Format("The parameter must be of type {0}", type), paramName); |
|
51 | throw new ArgumentException(String.Format("The parameter must be of type {0}", type), paramName); | |
| 52 | } |
|
52 | } | |
| 53 |
|
53 | |||
| 54 | public static void Dispose(params IDisposable[] objects) { |
|
54 | public static void Dispose(params IDisposable[] objects) { | |
| 55 | foreach (var d in objects) |
|
55 | foreach (var d in objects) | |
| 56 | if (d != null) |
|
56 | if (d != null) | |
| 57 | d.Dispose(); |
|
57 | d.Dispose(); | |
| 58 | } |
|
58 | } | |
| 59 |
|
59 | |||
| 60 | public static void Dispose(params object[] objects) { |
|
60 | public static void Dispose(params object[] objects) { | |
| 61 | foreach (var obj in objects) { |
|
61 | foreach (var obj in objects) { | |
| 62 | var d = obj as IDisposable; |
|
62 | var d = obj as IDisposable; | |
| 63 | if (d != null) |
|
63 | if (d != null) | |
| 64 | d.Dispose(); |
|
64 | d.Dispose(); | |
| 65 | } |
|
65 | } | |
| 66 | } |
|
66 | } | |
| 67 |
|
67 | |||
| 68 | public static void DisposeCollection(IEnumerable<IDisposable> objects) { |
|
68 | public static void DisposeCollection(IEnumerable<IDisposable> objects) { | |
| 69 | foreach (var d in objects) |
|
69 | foreach (var d in objects) | |
| 70 | Dispose(d); |
|
70 | Dispose(d); | |
| 71 | } |
|
71 | } | |
| 72 |
|
72 | |||
| 73 | public static void DisposeCollection(IEnumerable objects) { |
|
73 | public static void DisposeCollection(IEnumerable objects) { | |
| 74 | foreach (var d in objects) |
|
74 | foreach (var d in objects) | |
| 75 | Dispose(d); |
|
75 | Dispose(d); | |
| 76 | } |
|
76 | } | |
| 77 |
|
77 | |||
| 78 | public static void Dispose(object obj) { |
|
78 | public static void Dispose(object obj) { | |
| 79 | if (obj is IDisposable) { |
|
79 | if (obj is IDisposable) { | |
| 80 | Dispose((IDisposable)obj); |
|
80 | Dispose((IDisposable)obj); | |
| 81 | } else if (obj is IEnumerable) { |
|
81 | } else if (obj is IEnumerable) { | |
| 82 | Dispose((IEnumerable)obj); |
|
82 | DisposeCollection((IEnumerable)obj); | |
| 83 | } |
|
83 | } | |
| 84 | } |
|
84 | } | |
| 85 |
|
85 | |||
| 86 | [DebuggerStepThrough] |
|
86 | [DebuggerStepThrough] | |
| 87 | public static void DispatchEvent<T>(this EventHandler<T> handler, object sender, T args) { |
|
87 | public static void DispatchEvent<T>(this EventHandler<T> handler, object sender, T args) { | |
| 88 | if (handler != null) |
|
88 | if (handler != null) | |
| 89 | handler(sender, args); |
|
89 | handler(sender, args); | |
| 90 | } |
|
90 | } | |
| 91 |
|
91 | |||
| 92 | [DebuggerStepThrough] |
|
92 | [DebuggerStepThrough] | |
| 93 | public static void DispatchEvent(this EventHandler handler, object sender, EventArgs args) { |
|
93 | public static void DispatchEvent(this EventHandler handler, object sender, EventArgs args) { | |
| 94 | if (handler != null) |
|
94 | if (handler != null) | |
| 95 | handler(sender, args); |
|
95 | handler(sender, args); | |
| 96 | } |
|
96 | } | |
| 97 |
|
97 | |||
| 98 | [DebuggerStepThrough] |
|
98 | [DebuggerStepThrough] | |
| 99 | public static IPromise<T> Run<T>(Func<T> action) { |
|
99 | public static IPromise<T> Run<T>(Func<T> action) { | |
| 100 | ArgumentNotNull(action, "action"); |
|
100 | ArgumentNotNull(action, "action"); | |
| 101 |
|
101 | |||
| 102 | try { |
|
102 | try { | |
| 103 | return Promise<T>.FromResult(action()); |
|
103 | return Promise<T>.FromResult(action()); | |
| 104 | } catch (Exception err) { |
|
104 | } catch (Exception err) { | |
| 105 | return Promise<T>.FromException(err); |
|
105 | return Promise<T>.FromException(err); | |
| 106 | } |
|
106 | } | |
| 107 | } |
|
107 | } | |
| 108 |
|
108 | |||
| 109 | [DebuggerStepThrough] |
|
109 | [DebuggerStepThrough] | |
| 110 | public static IPromise Run(Action action) { |
|
110 | public static IPromise Run(Action action) { | |
| 111 | ArgumentNotNull(action, "action"); |
|
111 | ArgumentNotNull(action, "action"); | |
| 112 |
|
112 | |||
| 113 | try { |
|
113 | try { | |
| 114 | action(); |
|
114 | action(); | |
| 115 | return Promise.Success; |
|
115 | return Promise.Success; | |
| 116 | } catch (Exception err) { |
|
116 | } catch (Exception err) { | |
| 117 | return new FailedPromise(err); |
|
117 | return new FailedPromise(err); | |
| 118 | } |
|
118 | } | |
| 119 | } |
|
119 | } | |
| 120 |
|
120 | |||
| 121 | [DebuggerStepThrough] |
|
121 | [DebuggerStepThrough] | |
| 122 | public static IPromise Run(Func<IPromise> action) { |
|
122 | public static IPromise Run(Func<IPromise> action) { | |
| 123 | ArgumentNotNull(action, "action"); |
|
123 | ArgumentNotNull(action, "action"); | |
| 124 |
|
124 | |||
| 125 | try { |
|
125 | try { | |
| 126 | return action() ?? new FailedPromise(new Exception("The action returned null")); |
|
126 | return action() ?? new FailedPromise(new Exception("The action returned null")); | |
| 127 | } catch (Exception err) { |
|
127 | } catch (Exception err) { | |
| 128 | return new FailedPromise(err); |
|
128 | return new FailedPromise(err); | |
| 129 | } |
|
129 | } | |
| 130 | } |
|
130 | } | |
| 131 |
|
131 | |||
| 132 | public static void NoWait(IPromise promise) { |
|
132 | public static void NoWait(IPromise promise) { | |
| 133 | } |
|
133 | } | |
| 134 |
|
134 | |||
| 135 | [DebuggerStepThrough] |
|
135 | [DebuggerStepThrough] | |
| 136 | public static IPromise<T> Run<T>(Func<IPromise<T>> action) { |
|
136 | public static IPromise<T> Run<T>(Func<IPromise<T>> action) { | |
| 137 | ArgumentNotNull(action, "action"); |
|
137 | ArgumentNotNull(action, "action"); | |
| 138 |
|
138 | |||
| 139 | try { |
|
139 | try { | |
| 140 | return action() ?? Promise<T>.FromException(new Exception("The action returned null")); |
|
140 | return action() ?? Promise<T>.FromException(new Exception("The action returned null")); | |
| 141 | } catch (Exception err) { |
|
141 | } catch (Exception err) { | |
| 142 | return Promise<T>.FromException(err); |
|
142 | return Promise<T>.FromException(err); | |
| 143 | } |
|
143 | } | |
| 144 | } |
|
144 | } | |
| 145 |
|
145 | |||
| 146 | #if NET_4_5 |
|
146 | #if NET_4_5 | |
| 147 | public static void NoWait(Task t) { |
|
147 | public static void NoWait(Task t) { | |
| 148 | } |
|
148 | } | |
| 149 | #endif |
|
149 | #endif | |
| 150 |
|
150 | |||
| 151 | } |
|
151 | } | |
| 152 | } |
|
152 | } | |
General Comments 3
ok, latest stable version should be in default
You need to be logged in to leave comments.
Login now
