##// END OF EJS Templates
minor changes
cin -
r131:b5c2d609d71b v2
parent child
Show More
@@ -1,99 +1,114
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 39 public static void Dispose(params IDisposable[] objects) {
40 40 foreach (var d in objects)
41 41 if (d != null)
42 42 d.Dispose();
43 43 }
44 44
45 45 public static void Dispose(params object[] objects) {
46 46 foreach (var obj in objects) {
47 47 var d = obj as IDisposable;
48 48 if (d != null)
49 49 d.Dispose();
50 50 }
51 51 }
52 52
53 53 public static void Dispose(object obj) {
54 54 var d = obj as IDisposable;
55 55 if (d != null)
56 56 d.Dispose();
57 57 }
58 58
59 59 [DebuggerStepThrough]
60 60 public static IPromise<T> InvokePromise<T>(Func<T> action) {
61 61 ArgumentNotNull(action, "action");
62 62
63 63 var p = new Promise<T>();
64 64 try {
65 65 p.Resolve(action());
66 66 } catch (Exception err) {
67 67 p.Reject(err);
68 68 }
69 69
70 70 return p;
71 71 }
72 72
73 73 [DebuggerStepThrough]
74 74 public static IPromise InvokePromise(Action action) {
75 75 ArgumentNotNull(action, "action");
76 76
77 77 var p = new Promise();
78 78 try {
79 79 action();
80 80 p.Resolve();
81 81 } catch (Exception err) {
82 82 p.Reject(err);
83 83 }
84 84
85 85 return p;
86 86 }
87 87
88 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 104 public static IPromise<T> InvokePromise<T>(Func<IPromise<T>> action) {
90 105 ArgumentNotNull(action, "action");
91 106
92 107 try {
93 108 return action() ?? Promise<T>.ExceptionToPromise(new Exception("The action returned null"));
94 109 } catch (Exception err) {
95 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