|
1 | 1 | using Microsoft.Extensions.Logging; |
2 | 2 | using System; |
3 | 3 | using System.Collections.Generic; |
| 4 | +using System.Linq; |
4 | 5 |
|
5 | 6 | namespace SimpleStateMachineLibrary.Helpers |
6 | 7 | { |
@@ -41,81 +42,99 @@ public static TObject NamedObject<TObject>(TObject objectRequested, ILogger logg |
41 | 42 | return objectRequested; |
42 | 43 | } |
43 | 44 |
|
44 | | - |
| 45 | + |
45 | 46 | public static bool Contains<TObject>(Dictionary<string, TObject> dictionary, string nameObject, ILogger logger, bool exception = true) where TObject : NamedObject |
| 47 | + { |
| 48 | + nameObject = Contains(dictionary, nameObject, logger, out bool result, exception); |
| 49 | + return result; |
| 50 | + } |
| 51 | + public static bool Contains<TObject>(Dictionary<string, TObject> dictionary, TObject objectRequested, ILogger logger, bool exception = true) where TObject : NamedObject |
| 52 | + { |
| 53 | + objectRequested = Contains(dictionary, objectRequested, logger, out bool result, exception); |
| 54 | + return result; |
| 55 | + } |
| 56 | + public static string Contains<TObject>(Dictionary<string, TObject> dictionary, string nameObject, ILogger logger, out bool result, bool exception = true) where TObject : NamedObject |
46 | 57 | { |
47 | 58 | dictionary = Check.Object(dictionary, logger); |
48 | 59 | nameObject = Check.Name(nameObject, logger); |
49 | | - bool contains = dictionary.ContainsKey(nameObject); |
| 60 | + result = dictionary.ContainsKey(nameObject); |
50 | 61 |
|
51 | | - if ((exception)&&(!contains)) |
| 62 | + if ((exception) && (!result)) |
52 | 63 | { |
53 | 64 | object[] args = { nameObject }; |
54 | 65 | string message = "Element with name \"{0}\" is not found"; |
55 | 66 | var ex = new KeyNotFoundException(message: String.Format(message, args)); |
56 | | - logger?.LogError(ex, message, args); |
| 67 | + logger?.LogError(ex, message, args); |
57 | 68 | throw ex; |
58 | 69 | } |
59 | 70 |
|
60 | | - return contains; |
| 71 | + return nameObject; |
61 | 72 | } |
62 | | - |
63 | | - public static bool Contains<TObject>(Dictionary<string, TObject> dictionary, TObject objectRequested, ILogger logger, bool exception = true) where TObject : NamedObject |
| 73 | + public static TObject Contains<TObject>(Dictionary<string, TObject> dictionary, TObject objectRequested, ILogger logger, out bool result, bool exception = true) where TObject : NamedObject |
64 | 74 | { |
65 | 75 | dictionary = Check.Object(dictionary, logger); |
66 | 76 | objectRequested = Check.Object(objectRequested, logger); |
67 | 77 |
|
68 | | - bool contains = dictionary.ContainsValue(objectRequested); |
| 78 | + result = dictionary.ContainsValue(objectRequested); |
69 | 79 |
|
70 | | - if ((exception) && (!contains)) |
| 80 | + if ((exception) && (!result)) |
71 | 81 | { |
72 | 82 | object[] args = { objectRequested.Name }; |
73 | 83 | string message = "Element with name \"{0}\" is not found"; |
74 | 84 | var ex = new KeyNotFoundException(message: String.Format(message, args)); |
75 | | - logger?.LogError(ex, message, args); |
| 85 | + logger?.LogError(ex, message, args); |
76 | 86 | throw ex; |
77 | 87 | } |
78 | 88 |
|
79 | | - return contains; |
| 89 | + return objectRequested; |
80 | 90 | } |
81 | 91 |
|
| 92 | + |
82 | 93 | public static bool NotContains<TObject>(Dictionary<string, TObject> dictionary, string nameObject, ILogger logger, bool exception = true) where TObject : NamedObject |
| 94 | + { |
| 95 | + nameObject = NotContains(dictionary, nameObject, logger, out bool result, exception); |
| 96 | + return result; |
| 97 | + } |
| 98 | + public static bool NotContains<TObject>(Dictionary<string, TObject> dictionary, TObject objectRequested, ILogger logger, bool exception = true) where TObject : NamedObject |
| 99 | + { |
| 100 | + objectRequested = NotContains(dictionary, objectRequested, logger, out bool result, exception); |
| 101 | + return result; |
| 102 | + } |
| 103 | + public static string NotContains<TObject>(Dictionary<string, TObject> dictionary, string nameObject, ILogger logger, out bool result, bool exception = true) where TObject : NamedObject |
83 | 104 | { |
84 | 105 | dictionary = Check.Object(dictionary, logger); |
85 | 106 | nameObject = Check.Name(nameObject, logger); |
86 | | - bool notContains = !dictionary.ContainsKey(nameObject); |
| 107 | + result = !dictionary.ContainsKey(nameObject); |
87 | 108 |
|
88 | | - if ((exception) && (!notContains)) |
| 109 | + if ((exception) && (!result)) |
89 | 110 | { |
90 | 111 | object[] args = { nameObject }; |
91 | | - string message = "Element of type \"{0}\" already exists"; |
92 | | - var ex = new KeyNotFoundException(message: String.Format(message, args)); |
93 | | - logger?.LogError(ex, message, args); |
| 112 | + string message = "Element with name \"{0}\" already exists"; |
| 113 | + var ex = new ArgumentException(message: String.Format(message, args)); |
| 114 | + logger?.LogError(ex, message, args); |
94 | 115 | throw ex; |
95 | 116 | } |
96 | 117 |
|
97 | | - return notContains; |
| 118 | + return nameObject; |
98 | 119 | } |
99 | | - |
100 | | - public static bool NotContains<TObject>(Dictionary<string, TObject> dictionary, TObject objectRequested, ILogger logger, bool exception = true) where TObject : NamedObject |
| 120 | + public static TObject NotContains<TObject>(Dictionary<string, TObject> dictionary, TObject objectRequested, ILogger logger, out bool result, bool exception = true) where TObject : NamedObject |
101 | 121 | { |
102 | 122 | dictionary = Check.Object(dictionary, logger); |
103 | 123 | objectRequested = Check.Object(objectRequested, logger); |
| 124 | + result = !dictionary.ContainsValue(objectRequested); |
104 | 125 |
|
105 | | - bool notContains = !dictionary.ContainsValue(objectRequested); |
106 | | - |
107 | | - if ((exception) && (!notContains)) |
| 126 | + if ((exception) && (!result)) |
108 | 127 | { |
109 | 128 | object[] args = { objectRequested.Name }; |
110 | | - string message = "Element of type \"{0}\" already exists"; |
111 | | - var ex = new KeyNotFoundException(message: String.Format(message, args)); |
112 | | - logger?.LogError(ex, message, args); |
| 129 | + string message = "Element with name \"{0}\" already exists"; |
| 130 | + var ex = new ArgumentException(message: String.Format(message, args)); |
| 131 | + logger?.LogError(ex, message, args); |
113 | 132 | throw ex; |
114 | 133 | } |
115 | 134 |
|
116 | | - return notContains; |
| 135 | + return objectRequested; |
117 | 136 | } |
118 | | - |
| 137 | + |
119 | 138 |
|
120 | 139 | public static TObject Remove<TObject>(Dictionary<string, TObject> dictionary, string nameObject, ILogger logger, out bool result, bool exception = true) where TObject : NamedObject |
121 | 140 | { |
@@ -145,7 +164,6 @@ public static TObject Remove<TObject>(Dictionary<string, TObject> dictionary, st |
145 | 164 | result = true; |
146 | 165 | return removedObj; |
147 | 166 | } |
148 | | - |
149 | 167 | public static TObject Remove<TObject>(Dictionary<string, TObject> dictionary, TObject obj, ILogger logger, out bool result, bool exception = true) where TObject : NamedObject |
150 | 168 | { |
151 | 169 | result = false; |
@@ -181,12 +199,29 @@ public static TObject GetElement<TObject>(Dictionary<string, TObject> dictionary |
181 | 199 | result = Contains(dictionary, nameObject, logger, exception); |
182 | 200 | return result ? dictionary[nameObject] : default(TObject); |
183 | 201 | } |
184 | | - |
185 | 202 | public static TObject GetElement<TObject>(Dictionary<string, TObject> dictionary, TObject obj, ILogger logger, out bool result, bool exception = true) where TObject : NamedObject |
186 | 203 | { |
187 | 204 | result = Contains(dictionary, obj, logger, exception); |
188 | 205 | return result ? obj : default(TObject); |
189 | 206 | } |
190 | 207 |
|
| 208 | + |
| 209 | + public static Dictionary<string, TObject> GetValuesWhere<TObject>(Dictionary<string, TObject> dictionary, Func<TObject, bool> action, ILogger logger, out bool result, bool exception = true) where TObject : NamedObject |
| 210 | + { |
| 211 | + dictionary = Check.Object(dictionary, logger); |
| 212 | + Dictionary<string, TObject> foundElements = dictionary.Values.Where(action).ToDictionary(x => x.Name, x => x); |
| 213 | + result = foundElements.Count > 1; |
| 214 | + if ((exception) && (!result)) |
| 215 | + { |
| 216 | + object[] args = { }; |
| 217 | + string message = "Elements aren't found"; |
| 218 | + var ex = new KeyNotFoundException(message: String.Format(message, args)); |
| 219 | + logger?.LogError(ex, message, args); |
| 220 | + throw ex; |
| 221 | + } |
| 222 | + |
| 223 | + return foundElements; |
| 224 | + } |
| 225 | + |
191 | 226 | } |
192 | 227 | } |
0 commit comments