Mercurial > pub > ImplabNet
annotate Implab/Safe.cs @ 163:419aa51b04fd ref20160224
JSON moved to Formats namespace
Working in RegularDFA
author | cin |
---|---|
date | Wed, 24 Feb 2016 20:12:52 +0300 |
parents | 2a8466f0cb8a |
children | a0ff6a0e9c44 |
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 | |
128
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
44 public static void Dispose(params IDisposable[] objects) { |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
45 foreach (var d in objects) |
126 | 46 if (d != null) |
47 d.Dispose(); | |
1 | 48 } |
66 | 49 |
128
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
50 public static void Dispose(params object[] objects) { |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
51 foreach (var obj in objects) { |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
52 var d = obj as IDisposable; |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
53 if (d != null) |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
54 d.Dispose(); |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
55 } |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
56 } |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
57 |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
58 public static void Dispose(object obj) { |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
59 var d = obj as IDisposable; |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
60 if (d != null) |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
61 d.Dispose(); |
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 |
66 | 64 [DebuggerStepThrough] |
132 | 65 public static IPromise<T> WrapPromise<T>(Func<T> action) { |
66 | 66 ArgumentNotNull(action, "action"); |
67 | |
68 var p = new Promise<T>(); | |
69 try { | |
70 p.Resolve(action()); | |
71 } catch (Exception err) { | |
72 p.Reject(err); | |
73 } | |
74 | |
75 return p; | |
76 } | |
77 | |
78 [DebuggerStepThrough] | |
132 | 79 public static IPromise WrapPromise(Action action) { |
95 | 80 ArgumentNotNull(action, "action"); |
81 | |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
96
diff
changeset
|
82 var p = new Promise(); |
95 | 83 try { |
84 action(); | |
85 p.Resolve(); | |
86 } catch (Exception err) { | |
87 p.Reject(err); | |
88 } | |
89 | |
90 return p; | |
91 } | |
92 | |
93 [DebuggerStepThrough] | |
131 | 94 public static IPromise InvokePromise(Func<IPromise> action) { |
95 ArgumentNotNull(action, "action"); | |
96 | |
97 try { | |
133 | 98 var p = action(); |
99 if (p == null) { | |
100 var d = new Promise(); | |
101 d.Reject(new Exception("The action returned null")); | |
102 p = d; | |
103 } | |
104 return p; | |
131 | 105 } catch (Exception err) { |
133 | 106 var p = new Promise(); |
131 | 107 p.Reject(err); |
133 | 108 return p; |
131 | 109 } |
110 } | |
111 | |
112 [DebuggerStepThrough] | |
94 | 113 public static IPromise<T> InvokePromise<T>(Func<IPromise<T>> action) { |
66 | 114 ArgumentNotNull(action, "action"); |
115 | |
116 try { | |
145 | 117 return action() ?? Promise<T>.FromException(new Exception("The action returned null")); |
66 | 118 } catch (Exception err) { |
145 | 119 return Promise<T>.FromException(err); |
66 | 120 } |
121 } | |
1 | 122 } |
123 } |