##// END OF EJS Templates
minor changes
cin -
r131:b5c2d609d71b v2
parent child
Show More
@@ -1,99 +1,114
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 value, string paramName, Regex rx) {
12 public static void ArgumentMatch(string value, string paramName, Regex rx) {
13 if (rx == null)
13 if (rx == null)
14 throw new ArgumentNullException("rx");
14 throw new ArgumentNullException("rx");
15 if (!rx.IsMatch(value))
15 if (!rx.IsMatch(value))
16 throw new ArgumentException(String.Format("The prameter value must match {0}", rx), paramName);
16 throw new ArgumentException(String.Format("The prameter value must match {0}", rx), paramName);
17 }
17 }
18
18
19 public static void ArgumentNotEmpty(string value, string paramName) {
19 public static void ArgumentNotEmpty(string value, string paramName) {
20 if (String.IsNullOrEmpty(value))
20 if (String.IsNullOrEmpty(value))
21 throw new ArgumentException("The parameter can't be empty", paramName);
21 throw new ArgumentException("The parameter can't be empty", paramName);
22 }
22 }
23
23
24 public static void ArgumentNotEmpty<T>(T[] value, string paramName) {
24 public static void ArgumentNotEmpty<T>(T[] value, string paramName) {
25 if (value == null || value.Length == 0)
25 if (value == null || value.Length == 0)
26 throw new ArgumentException("The array must be not emty", paramName);
26 throw new ArgumentException("The array must be not emty", paramName);
27 }
27 }
28
28
29 public static void ArgumentNotNull(object value, string paramName) {
29 public static void ArgumentNotNull(object value, string paramName) {
30 if (value == null)
30 if (value == null)
31 throw new ArgumentNullException(paramName);
31 throw new ArgumentNullException(paramName);
32 }
32 }
33
33
34 public static void ArgumentInRange(int value, int min, int max, string paramName) {
34 public static void ArgumentInRange(int value, int min, int max, string paramName) {
35 if (value < min || value > max)
35 if (value < min || value > max)
36 throw new ArgumentOutOfRangeException(paramName);
36 throw new ArgumentOutOfRangeException(paramName);
37 }
37 }
38
38
39 public static void Dispose(params IDisposable[] objects) {
39 public static void Dispose(params IDisposable[] objects) {
40 foreach (var d in objects)
40 foreach (var d in objects)
41 if (d != null)
41 if (d != null)
42 d.Dispose();
42 d.Dispose();
43 }
43 }
44
44
45 public static void Dispose(params object[] objects) {
45 public static void Dispose(params object[] objects) {
46 foreach (var obj in objects) {
46 foreach (var obj in objects) {
47 var d = obj as IDisposable;
47 var d = obj as IDisposable;
48 if (d != null)
48 if (d != null)
49 d.Dispose();
49 d.Dispose();
50 }
50 }
51 }
51 }
52
52
53 public static void Dispose(object obj) {
53 public static void Dispose(object obj) {
54 var d = obj as IDisposable;
54 var d = obj as IDisposable;
55 if (d != null)
55 if (d != null)
56 d.Dispose();
56 d.Dispose();
57 }
57 }
58
58
59 [DebuggerStepThrough]
59 [DebuggerStepThrough]
60 public static IPromise<T> InvokePromise<T>(Func<T> action) {
60 public static IPromise<T> InvokePromise<T>(Func<T> action) {
61 ArgumentNotNull(action, "action");
61 ArgumentNotNull(action, "action");
62
62
63 var p = new Promise<T>();
63 var p = new Promise<T>();
64 try {
64 try {
65 p.Resolve(action());
65 p.Resolve(action());
66 } catch (Exception err) {
66 } catch (Exception err) {
67 p.Reject(err);
67 p.Reject(err);
68 }
68 }
69
69
70 return p;
70 return p;
71 }
71 }
72
72
73 [DebuggerStepThrough]
73 [DebuggerStepThrough]
74 public static IPromise InvokePromise(Action action) {
74 public static IPromise InvokePromise(Action action) {
75 ArgumentNotNull(action, "action");
75 ArgumentNotNull(action, "action");
76
76
77 var p = new Promise();
77 var p = new Promise();
78 try {
78 try {
79 action();
79 action();
80 p.Resolve();
80 p.Resolve();
81 } catch (Exception err) {
81 } catch (Exception err) {
82 p.Reject(err);
82 p.Reject(err);
83 }
83 }
84
84
85 return p;
85 return p;
86 }
86 }
87
87
88 [DebuggerStepThrough]
88 [DebuggerStepThrough]
89 public static IPromise InvokePromise(Func<IPromise> action) {
90 ArgumentNotNull(action, "action");
91
92 var p = new Promise();
93 try {
94 action();
95 p.Resolve();
96 } catch (Exception err) {
97 p.Reject(err);
98 }
99
100 return p;
101 }
102
103 [DebuggerStepThrough]
89 public static IPromise<T> InvokePromise<T>(Func<IPromise<T>> action) {
104 public static IPromise<T> InvokePromise<T>(Func<IPromise<T>> action) {
90 ArgumentNotNull(action, "action");
105 ArgumentNotNull(action, "action");
91
106
92 try {
107 try {
93 return action() ?? Promise<T>.ExceptionToPromise(new Exception("The action returned null"));
108 return action() ?? Promise<T>.ExceptionToPromise(new Exception("The action returned null"));
94 } catch (Exception err) {
109 } catch (Exception err) {
95 return Promise<T>.ExceptionToPromise(err);
110 return Promise<T>.ExceptionToPromise(err);
96 }
111 }
97 }
112 }
98 }
113 }
99 }
114 }
General Comments 0
You need to be logged in to leave comments. Login now