|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | + |
| 4 | +namespace SimpleStateMachineLibrary.Helpers |
| 5 | +{ |
| 6 | + public class Check |
| 7 | + { |
| 8 | + public static string Name(string name) |
| 9 | + { |
| 10 | + if (String.IsNullOrEmpty(name)) |
| 11 | + throw new ArgumentNullException("Name must be not Empty"); |
| 12 | + return name; |
| 13 | + } |
| 14 | + |
| 15 | + public static TObject Object<TObject>(TObject objectRequested) |
| 16 | + { |
| 17 | + if (Equals(objectRequested, default(TObject))) |
| 18 | + throw new ArgumentNullException(String.Format("Object of type {0} must be not null", typeof(TObject).ToString())); |
| 19 | + return objectRequested; |
| 20 | + } |
| 21 | + |
| 22 | + public static TObject NamedObject<TObject>(TObject objectRequested) where TObject : NamedObject |
| 23 | + { |
| 24 | + Check.Object(objectRequested); |
| 25 | + Check.Name(objectRequested.Name); |
| 26 | + return objectRequested; |
| 27 | + } |
| 28 | + |
| 29 | + private static bool _Contains<TObject>(Dictionary<string, TObject> dictionary, string nameObject, bool needContains, bool exeption) where TObject : NamedObject |
| 30 | + { |
| 31 | + dictionary = Check.Object(dictionary); |
| 32 | + nameObject = Check.Name(nameObject); |
| 33 | + |
| 34 | + if (needContains == dictionary.ContainsKey(nameObject)) |
| 35 | + return true; |
| 36 | + if (exeption) |
| 37 | + if (needContains) |
| 38 | + throw new KeyNotFoundException(String.Format("Element with name {0} is not found", nameObject)); |
| 39 | + else |
| 40 | + throw new ArgumentException(String.Format("Element with name {0} already exists", nameObject)); |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + private static bool _Contains<TObject>(Dictionary<string, TObject> dictionary, TObject objectRequested, bool needContains, bool exeption) where TObject : NamedObject |
| 45 | + { |
| 46 | + dictionary = Check.Object(dictionary); |
| 47 | + objectRequested = Check.Object(objectRequested); |
| 48 | + |
| 49 | + if (needContains == dictionary.ContainsValue(objectRequested)) |
| 50 | + return true; |
| 51 | + |
| 52 | + if (exeption) |
| 53 | + if (needContains) |
| 54 | + throw new KeyNotFoundException(String.Format("Element of type {0} not found", typeof(TObject).ToString())); |
| 55 | + else |
| 56 | + throw new ArgumentException(String.Format("Element of type {0} already exists", typeof(TObject).ToString())); |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + public static bool Contains<TObject>(Dictionary<string, TObject> dictionary, string nameObject, bool exeption = true) where TObject : NamedObject |
| 62 | + { |
| 63 | + return _Contains(dictionary, nameObject, true, exeption); |
| 64 | + } |
| 65 | + |
| 66 | + public static bool Contains<TObject>(Dictionary<string, TObject> dictionary, TObject objectRequested, bool exeption = true) where TObject : NamedObject |
| 67 | + { |
| 68 | + return _Contains(dictionary, objectRequested, true, exeption); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + public static bool NotContains<TObject>(Dictionary<string, TObject> dictionary, string nameObject, bool exeption = true) where TObject : NamedObject |
| 73 | + { |
| 74 | + return _Contains(dictionary, nameObject, false, exeption); |
| 75 | + } |
| 76 | + |
| 77 | + public static bool NotContains<TObject>(Dictionary<string, TObject> dictionary, TObject objectRequested, bool exeption = true) where TObject : NamedObject |
| 78 | + { |
| 79 | + return _Contains(dictionary, objectRequested, false, exeption); |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + public static TObject Remove<TObject>(Dictionary<string, TObject> dictionary, string nameObject, bool exeption = true) where TObject : NamedObject |
| 84 | + { |
| 85 | + dictionary = Check.Object(dictionary); |
| 86 | + nameObject = Check.Name(nameObject); |
| 87 | + |
| 88 | + TObject removedObj = default(TObject); |
| 89 | + dictionary?.TryGetValue(nameObject, out removedObj); |
| 90 | + |
| 91 | + if (removedObj == default(TObject)) |
| 92 | + { |
| 93 | + if (exeption) |
| 94 | + throw new KeyNotFoundException(String.Format("Element with name {0} is not deleted because not found. ", nameObject)); |
| 95 | + else |
| 96 | + return default(TObject); |
| 97 | + } |
| 98 | + |
| 99 | + dictionary.Remove(nameObject); |
| 100 | + return removedObj; |
| 101 | + } |
| 102 | + |
| 103 | + public static TObject Remove<TObject>(Dictionary<string, TObject> dictionary, TObject obj, bool exeption = true) where TObject : NamedObject |
| 104 | + { |
| 105 | + dictionary = Check.Object(dictionary); |
| 106 | + obj = Check.NamedObject(obj); |
| 107 | + |
| 108 | + TObject removedObj = default(TObject); |
| 109 | + dictionary?.TryGetValue(obj.Name, out removedObj); |
| 110 | + |
| 111 | + if (removedObj == default(TObject)) |
| 112 | + { |
| 113 | + if (exeption) |
| 114 | + throw new KeyNotFoundException(String.Format("Element with name {0} is not deleted because not found. ", obj.Name)); |
| 115 | + else |
| 116 | + return default(TObject); |
| 117 | + } |
| 118 | + |
| 119 | + dictionary.Remove(obj.Name); |
| 120 | + return removedObj; |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + public static TObject GetElement<TObject>(Dictionary<string, TObject> dictionary, string nameObject, bool exeption = true) where TObject : NamedObject |
| 125 | + { |
| 126 | + bool contains = Contains(dictionary, nameObject, exeption); |
| 127 | + return contains ? dictionary[nameObject] : default(TObject); |
| 128 | + } |
| 129 | + |
| 130 | + public static TObject GetElement<TObject>(Dictionary<string, TObject> dictionary, TObject obj, bool exeption = true) where TObject : NamedObject |
| 131 | + { |
| 132 | + bool contains = Contains(dictionary, obj, exeption); |
| 133 | + return contains ? obj : default(TObject); |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + public static TObject AddElement<TObject>(Dictionary<string, TObject> dictionary, TObject obj, bool exeption = true) where TObject : NamedObject |
| 138 | + { |
| 139 | + return AddElement(dictionary, obj?.Name, obj, exeption); |
| 140 | + } |
| 141 | + |
| 142 | + public static TObject AddElement<TObject>(Dictionary<string, TObject> dictionary, string name, TObject obj, bool exeption = true) where TObject : NamedObject |
| 143 | + { |
| 144 | + obj = Check.NamedObject(obj); |
| 145 | + bool nonContains = NotContains(dictionary, name, exeption); |
| 146 | + |
| 147 | + if (nonContains) |
| 148 | + return default(TObject); |
| 149 | + |
| 150 | + dictionary.Add(name, obj); |
| 151 | + return obj; |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments