Mercurial > pub > ImplabNet
annotate Implab/Safe.cs @ 205:8200ab154c8a v2
Added ResetState to RunnableComponent to reset in case of failure
Added StateChanged event to IRunnable
Renamed Promise.SUCCESS -> Promise.Success
Added Promise.FromException
Renamed Bundle -> PromiseAll in PromiseExtensions
author | cin |
---|---|
date | Tue, 25 Oct 2016 17:40:33 +0300 |
parents | 4d9830a9bbb8 |
children | 558f34b2fb50 |
rev | line source |
---|---|
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 { | |
161 | 12 public static void ArgumentAssert(bool condition, string paramName) { |
13 if (!condition) | |
14 throw new ArgumentException("The parameter is invalid", paramName); | |
15 } | |
16 | |
121 | 17 public static void ArgumentMatch(string value, string paramName, Regex rx) { |
51 | 18 if (rx == null) |
19 throw new ArgumentNullException("rx"); | |
121 | 20 if (!rx.IsMatch(value)) |
21 throw new ArgumentException(String.Format("The prameter value must match {0}", rx), paramName); | |
51 | 22 } |
23 | |
121 | 24 public static void ArgumentNotEmpty(string value, string paramName) { |
25 if (String.IsNullOrEmpty(value)) | |
26 throw new ArgumentException("The parameter can't be empty", paramName); | |
90 | 27 } |
28 | |
121 | 29 public static void ArgumentNotEmpty<T>(T[] value, string paramName) { |
30 if (value == null || value.Length == 0) | |
31 throw new ArgumentException("The array must be not emty", paramName); | |
51 | 32 } |
33 | |
121 | 34 public static void ArgumentNotNull(object value, string paramName) { |
35 if (value == null) | |
36 throw new ArgumentNullException(paramName); | |
51 | 37 } |
38 | |
121 | 39 public static void ArgumentInRange(int value, int min, int max, string paramName) { |
40 if (value < min || value > max) | |
41 throw new ArgumentOutOfRangeException(paramName); | |
55 | 42 } |
43 | |
177 | 44 public static void ArgumentOfType(object value, Type type, string paramName) { |
45 if (!type.IsInstanceOfType(value)) | |
46 throw new ArgumentException(String.Format("The parameter must be of type {0}", type), paramName); | |
47 } | |
48 | |
128
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
49 public static void Dispose(params IDisposable[] objects) { |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
50 foreach (var d in objects) |
126 | 51 if (d != null) |
52 d.Dispose(); | |
1 | 53 } |
66 | 54 |
128
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
55 public static void Dispose(params object[] objects) { |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
56 foreach (var obj in objects) { |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
57 var d = obj as IDisposable; |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
58 if (d != null) |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
59 d.Dispose(); |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
60 } |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
61 } |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
62 |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
63 public static void Dispose(object obj) { |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
64 var d = obj as IDisposable; |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
65 if (d != null) |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
66 d.Dispose(); |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
67 } |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
68 |
66 | 69 [DebuggerStepThrough] |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
70 public static IPromise<T> Run<T>(Func<T> action) { |
131 | 71 ArgumentNotNull(action, "action"); |
72 | |
73 try { | |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
74 return Promise<T>.FromResult(action()); |
131 | 75 } catch (Exception err) { |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
76 return Promise<T>.FromException(err); |
131 | 77 } |
78 } | |
79 | |
80 [DebuggerStepThrough] | |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
81 public static IPromise Run(Action action) { |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
82 ArgumentNotNull(action, "action"); |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
83 |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
84 try { |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
85 action(); |
205
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
86 return Promise.Success; |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
87 } catch (Exception err) { |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
88 return new FailedPromise(err); |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
89 } |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
90 } |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
91 |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
92 [DebuggerStepThrough] |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
93 public static IPromise Run(Func<IPromise> action) { |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
94 ArgumentNotNull(action, "action"); |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
95 |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
96 try { |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
97 return action() ?? new FailedPromise(new Exception("The action returned null")); |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
98 } catch (Exception err) { |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
99 return new FailedPromise(err); |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
100 } |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
101 } |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
102 |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
103 [DebuggerStepThrough] |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
104 public static IPromise<T> Run<T>(Func<IPromise<T>> action) { |
66 | 105 ArgumentNotNull(action, "action"); |
106 | |
107 try { | |
145 | 108 return action() ?? Promise<T>.FromException(new Exception("The action returned null")); |
66 | 109 } catch (Exception err) { |
145 | 110 return Promise<T>.FromException(err); |
66 | 111 } |
112 } | |
1 | 113 } |
114 } |