11using System ;
22using System . Collections . Generic ;
3+ using System . Linq ;
34
45namespace SimpleStateMachineLibrary . Helpers
56{
@@ -15,18 +16,19 @@ public static string Name(string name)
1516 public static TObject Object < TObject > ( TObject objectRequested )
1617 {
1718 if ( Equals ( objectRequested , default ( TObject ) ) )
18- throw new ArgumentNullException ( String . Format ( "Object of type {0} must be not null" , typeof ( TObject ) . ToString ( ) ) ) ;
19+ throw new ArgumentNullException ( String . Format ( "Object of type \" {0}\" must be not null" , typeof ( TObject ) . Name ) ) ;
1920 return objectRequested ;
2021 }
2122
22- public static TObject NamedObject < TObject > ( TObject objectRequested ) where TObject : NamedObject
23+ public static TObject NamedObject < TObject > ( TObject objectRequested ) where TObject : NamedObject < TObject >
2324 {
2425 Check . Object ( objectRequested ) ;
2526 Check . Name ( objectRequested . Name ) ;
2627 return objectRequested ;
2728 }
2829
29- private static bool _Contains < TObject > ( Dictionary < string , TObject > dictionary , string nameObject , bool needContains , bool exeption ) where TObject : NamedObject
30+
31+ private static bool _Contains < TObject > ( Dictionary < string , TObject > dictionary , string nameObject , bool needContains , bool exeption ) where TObject : NamedObject < TObject >
3032 {
3133 dictionary = Check . Object ( dictionary ) ;
3234 nameObject = Check . Name ( nameObject ) ;
@@ -35,13 +37,13 @@ private static bool _Contains<TObject>(Dictionary<string, TObject> dictionary, s
3537 return true ;
3638 if ( exeption )
3739 if ( needContains )
38- throw new KeyNotFoundException ( String . Format ( "Element with name {0} is not found" , nameObject ) ) ;
40+ throw new KeyNotFoundException ( String . Format ( "Element with name \" {0}\" is not found" , nameObject ) ) ;
3941 else
40- throw new ArgumentException ( String . Format ( "Element with name {0} already exists" , nameObject ) ) ;
42+ throw new ArgumentException ( String . Format ( "Element with name \" {0}\" already exists" , nameObject ) ) ;
4143 return false ;
4244 }
4345
44- private static bool _Contains < TObject > ( Dictionary < string , TObject > dictionary , TObject objectRequested , bool needContains , bool exeption ) where TObject : NamedObject
46+ private static bool _Contains < TObject > ( Dictionary < string , TObject > dictionary , TObject objectRequested , bool needContains , bool exeption ) where TObject : NamedObject < TObject >
4547 {
4648 dictionary = Check . Object ( dictionary ) ;
4749 objectRequested = Check . Object ( objectRequested ) ;
@@ -51,36 +53,36 @@ private static bool _Contains<TObject>(Dictionary<string, TObject> dictionary, T
5153
5254 if ( exeption )
5355 if ( needContains )
54- throw new KeyNotFoundException ( String . Format ( "Element of type {0} not found" , typeof ( TObject ) . ToString ( ) ) ) ;
56+ throw new KeyNotFoundException ( String . Format ( "Element of type \" {0}\" not found" , typeof ( TObject ) . Name ) ) ;
5557 else
56- throw new ArgumentException ( String . Format ( "Element of type {0} already exists" , typeof ( TObject ) . ToString ( ) ) ) ;
58+ throw new ArgumentException ( String . Format ( "Element of type \" {0}\" already exists" , typeof ( TObject ) . Name ) ) ;
5759 return false ;
5860 }
5961
60-
61- public static bool Contains < TObject > ( Dictionary < string , TObject > dictionary , string nameObject , bool exeption = true ) where TObject : NamedObject
62+
63+ public static bool Contains < TObject > ( Dictionary < string , TObject > dictionary , string nameObject , bool exeption = true ) where TObject : NamedObject < TObject >
6264 {
6365 return _Contains ( dictionary , nameObject , true , exeption ) ;
6466 }
6567
66- public static bool Contains < TObject > ( Dictionary < string , TObject > dictionary , TObject objectRequested , bool exeption = true ) where TObject : NamedObject
68+ public static bool Contains < TObject > ( Dictionary < string , TObject > dictionary , TObject objectRequested , bool exeption = true ) where TObject : NamedObject < TObject >
6769 {
6870 return _Contains ( dictionary , objectRequested , true , exeption ) ;
6971 }
7072
71-
72- public static bool NotContains < TObject > ( Dictionary < string , TObject > dictionary , string nameObject , bool exeption = true ) where TObject : NamedObject
73+
74+ public static bool NotContains < TObject > ( Dictionary < string , TObject > dictionary , string nameObject , bool exeption = true ) where TObject : NamedObject < TObject >
7375 {
7476 return _Contains ( dictionary , nameObject , false , exeption ) ;
7577 }
7678
77- public static bool NotContains < TObject > ( Dictionary < string , TObject > dictionary , TObject objectRequested , bool exeption = true ) where TObject : NamedObject
79+ public static bool NotContains < TObject > ( Dictionary < string , TObject > dictionary , TObject objectRequested , bool exeption = true ) where TObject : NamedObject < TObject >
7880 {
7981 return _Contains ( dictionary , objectRequested , false , exeption ) ;
8082 }
8183
82-
83- public static TObject Remove < TObject > ( Dictionary < string , TObject > dictionary , string nameObject , bool exeption = true ) where TObject : NamedObject
84+
85+ public static TObject Remove < TObject > ( Dictionary < string , TObject > dictionary , string nameObject , bool exeption = true ) where TObject : NamedObject < TObject >
8486 {
8587 dictionary = Check . Object ( dictionary ) ;
8688 nameObject = Check . Name ( nameObject ) ;
@@ -91,7 +93,7 @@ public static TObject Remove<TObject>(Dictionary<string, TObject> dictionary, st
9193 if ( removedObj == default ( TObject ) )
9294 {
9395 if ( exeption )
94- throw new KeyNotFoundException ( String . Format ( "Element with name {0} is not deleted because not found. " , nameObject ) ) ;
96+ throw new KeyNotFoundException ( String . Format ( "Element with name \" {0}\" is not deleted because not found. " , nameObject ) ) ;
9597 else
9698 return default ( TObject ) ;
9799 }
@@ -100,7 +102,7 @@ public static TObject Remove<TObject>(Dictionary<string, TObject> dictionary, st
100102 return removedObj ;
101103 }
102104
103- public static TObject Remove < TObject > ( Dictionary < string , TObject > dictionary , TObject obj , bool exeption = true ) where TObject : NamedObject
105+ public static TObject Remove < TObject > ( Dictionary < string , TObject > dictionary , TObject obj , bool exeption = true ) where TObject : NamedObject < TObject >
104106 {
105107 dictionary = Check . Object ( dictionary ) ;
106108 obj = Check . NamedObject ( obj ) ;
@@ -111,7 +113,7 @@ public static TObject Remove<TObject>(Dictionary<string, TObject> dictionary, TO
111113 if ( removedObj == default ( TObject ) )
112114 {
113115 if ( exeption )
114- throw new KeyNotFoundException ( String . Format ( "Element with name {0} is not deleted because not found. " , obj . Name ) ) ;
116+ throw new KeyNotFoundException ( String . Format ( "Element with name \" {0}\" is not deleted because not found. " , obj . Name ) ) ;
115117 else
116118 return default ( TObject ) ;
117119 }
@@ -121,34 +123,17 @@ public static TObject Remove<TObject>(Dictionary<string, TObject> dictionary, TO
121123 }
122124
123125
124- public static TObject GetElement < TObject > ( Dictionary < string , TObject > dictionary , string nameObject , bool exeption = true ) where TObject : NamedObject
126+ public static TObject GetElement < TObject > ( Dictionary < string , TObject > dictionary , string nameObject , bool exeption = true ) where TObject : NamedObject < TObject >
125127 {
126128 bool contains = Contains ( dictionary , nameObject , exeption ) ;
127129 return contains ? dictionary [ nameObject ] : default ( TObject ) ;
128130 }
129131
130- public static TObject GetElement < TObject > ( Dictionary < string , TObject > dictionary , TObject obj , bool exeption = true ) where TObject : NamedObject
132+ public static TObject GetElement < TObject > ( Dictionary < string , TObject > dictionary , TObject obj , bool exeption = true ) where TObject : NamedObject < TObject >
131133 {
132134 bool contains = Contains ( dictionary , obj , exeption ) ;
133135 return contains ? obj : default ( TObject ) ;
134136 }
135137
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- }
153138 }
154139}
0 commit comments