annotate Implab/Safe.cs @ 96:daffa72a1cec v2

Added the chaining method to the non-generic IPromise
author cin
date Thu, 30 Oct 2014 10:06:16 +0300
parents 0141a165d032
children 2573b562e328
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
1 using System;
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
2 using System.Collections.Generic;
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
3 using System.Linq;
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
4 using System.Text;
51
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
5 using System.Text.RegularExpressions;
66
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
6 using System.Diagnostics;
1
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
7
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
8 namespace Implab
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
9 {
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
10 public static class Safe
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
11 {
51
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
12 public static void ArgumentMatch(string param, string name, Regex rx) {
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
13 if (rx == null)
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
14 throw new ArgumentNullException("rx");
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
15 if (!rx.IsMatch(param))
90
efcb076407a7 code cleanup
cin
parents: 66
diff changeset
16 throw new ArgumentException(String.Format("The prameter value must match {0}", rx), name);
51
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
17 }
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
18
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
19 public static void ArgumentNotEmpty(string param, string name) {
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
20 if (String.IsNullOrEmpty(param))
90
efcb076407a7 code cleanup
cin
parents: 66
diff changeset
21 throw new ArgumentException("The parameter can't be empty", name);
efcb076407a7 code cleanup
cin
parents: 66
diff changeset
22 }
efcb076407a7 code cleanup
cin
parents: 66
diff changeset
23
efcb076407a7 code cleanup
cin
parents: 66
diff changeset
24 public static void ArgumentNotEmpty<T>(T[] param, string name) {
efcb076407a7 code cleanup
cin
parents: 66
diff changeset
25 if (param == null || param.Length == 0)
efcb076407a7 code cleanup
cin
parents: 66
diff changeset
26 throw new ArgumentException("The array must be not emty");
51
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
27 }
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
28
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
29 public static void ArgumentNotNull(object param, string name) {
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
30 if (param == null)
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
31 throw new ArgumentNullException(name);
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
32 }
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
33
55
c0bf853aa04f Added initial JSON support
cin
parents: 51
diff changeset
34 public static void ArgumentInRange(int arg, int min, int max, string name) {
c0bf853aa04f Added initial JSON support
cin
parents: 51
diff changeset
35 if (arg < min || arg > max)
c0bf853aa04f Added initial JSON support
cin
parents: 51
diff changeset
36 throw new ArgumentOutOfRangeException(name);
c0bf853aa04f Added initial JSON support
cin
parents: 51
diff changeset
37 }
c0bf853aa04f Added initial JSON support
cin
parents: 51
diff changeset
38
31
dafaadca5b9f minor cleanup
cin
parents: 2
diff changeset
39 public static void Dispose<T>(T obj) where T : class
1
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
40 {
2
aa367305156b small fixes
cin
parents: 1
diff changeset
41 var disp = obj as IDisposable;
aa367305156b small fixes
cin
parents: 1
diff changeset
42 if (disp != null)
aa367305156b small fixes
cin
parents: 1
diff changeset
43 disp.Dispose();
1
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
44 }
66
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
45
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
46 [DebuggerStepThrough]
94
a43745f81f10 minor fixes
cin
parents: 90
diff changeset
47 public static IPromise<T> InvokePromise<T>(Func<T> action) {
66
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
48 ArgumentNotNull(action, "action");
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
49
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
50 var p = new Promise<T>();
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
51 try {
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
52 p.Resolve(action());
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
53 } catch (Exception err) {
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
54 p.Reject(err);
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
55 }
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
56
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
57 return p;
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
58 }
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
59
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
60 [DebuggerStepThrough]
96
daffa72a1cec Added the chaining method to the non-generic IPromise
cin
parents: 95
diff changeset
61 public static IPromise InvokePromise(Action action) {
95
0141a165d032 minor changes
cin
parents: 94
diff changeset
62 ArgumentNotNull(action, "action");
0141a165d032 minor changes
cin
parents: 94
diff changeset
63
0141a165d032 minor changes
cin
parents: 94
diff changeset
64 var p = new Promise<object>();
0141a165d032 minor changes
cin
parents: 94
diff changeset
65 try {
0141a165d032 minor changes
cin
parents: 94
diff changeset
66 action();
0141a165d032 minor changes
cin
parents: 94
diff changeset
67 p.Resolve();
0141a165d032 minor changes
cin
parents: 94
diff changeset
68 } catch (Exception err) {
0141a165d032 minor changes
cin
parents: 94
diff changeset
69 p.Reject(err);
0141a165d032 minor changes
cin
parents: 94
diff changeset
70 }
0141a165d032 minor changes
cin
parents: 94
diff changeset
71
0141a165d032 minor changes
cin
parents: 94
diff changeset
72 return p;
0141a165d032 minor changes
cin
parents: 94
diff changeset
73 }
0141a165d032 minor changes
cin
parents: 94
diff changeset
74
0141a165d032 minor changes
cin
parents: 94
diff changeset
75 [DebuggerStepThrough]
94
a43745f81f10 minor fixes
cin
parents: 90
diff changeset
76 public static IPromise<T> InvokePromise<T>(Func<IPromise<T>> action) {
66
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
77 ArgumentNotNull(action, "action");
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
78
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
79 try {
94
a43745f81f10 minor fixes
cin
parents: 90
diff changeset
80 return action() ?? Promise<T>.ExceptionToPromise(new Exception("The action returned null"));
66
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
81 } catch (Exception err) {
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
82 return Promise<T>.ExceptionToPromise(err);
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
83 }
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
84 }
1
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
85 }
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
86 }