Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / class / Novell.Directory.Ldap / Novell.Directory.Ldap / SupportClass.cs
1 /******************************************************************************
2 * The MIT License
3 * Copyright (c) 2003 Novell Inc.  www.novell.com
4
5 * Permission is hereby granted, free of charge, to any person obtaining  a copy
6 * of this software and associated documentation files (the Software), to deal
7 * in the Software without restriction, including  without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
9 * copies of the Software, and to  permit persons to whom the Software is 
10 * furnished to do so, subject to the following conditions:
11
12 * The above copyright notice and this permission notice shall be included in 
13 * all copies or substantial portions of the Software.
14
15 * THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *******************************************************************************/
23 //
24 // Novell.Directory.Ldap.SupportClass.cs
25 //
26 // Author:
27 //   Sunil Kumar (Sunilk@novell.com)
28 //
29 // (C) 2003 Novell, Inc (http://www.novell.com)
30 //
31
32 // Support classes replicate the functionality of the original code, but in some cases they are 
33 // substantially different architecturally. Although every effort is made to preserve the 
34 // original architecture of the application in the converted project, the user should be aware that 
35 // the primary goal of these support classes is to replicate functionality, and that at times 
36 // the architecture of the resulting solution may differ somewhat.
37 //
38
39 using System;
40
41         /// <summary>
42         /// This interface should be implemented by any class whose instances are intended 
43         /// to be executed by a thread.
44         /// </summary>
45         public interface IThreadRunnable
46         {
47                 /// <summary>
48                 /// This method has to be implemented in order that starting of the thread causes the object's 
49                 /// run method to be called in that separately executing thread.
50                 /// </summary>
51                 void Run();
52         }
53
54
55         public class Integer32 : System.Object
56         {
57                 private System.Int32 _wintv;
58
59                 public Integer32(System.Int32 ival)    
60                 {
61                         _wintv=ival;
62                 }
63         
64                 public System.Int32 intValue
65                 {       
66                         get
67                         {
68                                 return _wintv;
69                         }
70                         set
71                         {
72                                 _wintv=value;
73                         }
74                 }
75         }
76                 
77         /// <summary>
78         /// Contains conversion support elements such as classes, interfaces and static methods.
79         /// </summary>
80         public class SupportClass
81         {
82                 /// <summary>
83                 /// Receives a byte array and returns it transformed in an sbyte array
84                 /// </summary>
85                 /// <param name="byteArray">Byte array to process</param>
86                 /// <returns>The transformed array</returns>
87                 [CLSCompliantAttribute(false)]
88                 public static sbyte[] ToSByteArray(byte[] byteArray)
89                 {
90                         sbyte[] sbyteArray = new sbyte[byteArray.Length];
91                         for(int index=0; index < byteArray.Length; index++)
92                                 sbyteArray[index] = (sbyte) byteArray[index];
93                         return sbyteArray;
94                 }
95                 /*******************************/
96                 /// <summary>
97                 /// Converts an array of sbytes to an array of bytes
98                 /// </summary>
99                 /// <param name="sbyteArray">The array of sbytes to be converted</param>
100                 /// <returns>The new array of bytes</returns>
101                 [CLSCompliantAttribute(false)]
102                 public static byte[] ToByteArray(sbyte[] sbyteArray)
103                 {
104                         byte[] byteArray = new byte[sbyteArray.Length];
105                         for(int index=0; index < sbyteArray.Length; index++)
106                                 byteArray[index] = (byte) sbyteArray[index];
107                         return byteArray;
108                 }
109
110                 /// <summary>
111                 /// Converts a string to an array of bytes
112                 /// </summary>
113                 /// <param name="sourceString">The string to be converted</param>
114                 /// <returns>The new array of bytes</returns>
115                 public static byte[] ToByteArray(string sourceString)
116                 {
117                         byte[] byteArray = new byte[sourceString.Length];
118                         for (int index=0; index < sourceString.Length; index++)
119                                 byteArray[index] = (byte) sourceString[index];
120                         return byteArray;
121                 }
122
123                 /// <summary>
124                 /// Converts a array of object-type instances to a byte-type array.
125                 /// </summary>
126                 /// <param name="tempObjectArray">Array to convert.</param>
127                 /// <returns>An array of byte type elements.</returns>
128                 public static byte[] ToByteArray(object[] tempObjectArray)
129                 {
130                         byte[] byteArray = new byte[tempObjectArray.Length];
131                         for (int index = 0; index < tempObjectArray.Length; index++)
132                                 byteArray[index] = (byte)tempObjectArray[index];
133                         return byteArray;
134                 }
135
136
137                 /*******************************/
138                 /// <summary>Reads a number of characters from the current source Stream and writes the data to the target array at the specified index.</summary>
139                 /// <param name="sourceStream">The source Stream to read from.</param>
140                 /// <param name="target">Contains the array of characteres read from the source Stream.</param>
141                 /// <param name="start">The starting index of the target array.</param>
142                 /// <param name="count">The maximum number of characters to read from the source Stream.</param>
143                 /// <returns>The number of characters read. The number will be less than or equal to count depending on the data available in the source Stream. Returns -1 if the end of the stream is reached.</returns>
144                 [CLSCompliantAttribute(false)]
145                 public static System.Int32 ReadInput(System.IO.Stream sourceStream, ref sbyte[] target, int start, int count)
146                 {
147                         // Returns 0 bytes if not enough space in target
148                         if (target.Length == 0)
149                                 return 0;
150
151                         byte[] receiver = new byte[target.Length];
152                         int bytesRead=0;
153                         int startIndex=start;
154                         int bytesToRead=count;
155                         while( bytesToRead > 0 )        {
156                                 int n= sourceStream.Read(receiver, startIndex, bytesToRead);
157                                 if (n==0)
158                                         break;
159                                 bytesRead+=n;
160                                 startIndex+=n;
161                                 bytesToRead-=n;
162                         }
163                         // Returns -1 if EOF
164                         if (bytesRead == 0)     
165                                 return -1;
166                 
167                         for(int i = start; i < start + bytesRead; i++)
168                                 target[i] = (sbyte)receiver[i];
169                 
170                         return bytesRead;
171                 }
172
173                 /// <summary>Reads a number of characters from the current source TextReader and writes the data to the target array at the specified index.</summary>
174                 /// <param name="sourceTextReader">The source TextReader to read from</param>
175                 /// <param name="target">Contains the array of characteres read from the source TextReader.</param>
176                 /// <param name="start">The starting index of the target array.</param>
177                 /// <param name="count">The maximum number of characters to read from the source TextReader.</param>
178                 /// <returns>The number of characters read. The number will be less than or equal to count depending on the data available in the source TextReader. Returns -1 if the end of the stream is reached.</returns>
179                 [CLSCompliantAttribute(false)]
180                 public static System.Int32 ReadInput(System.IO.TextReader sourceTextReader, ref sbyte[] target, int start, int count)
181                 {
182                         // Returns 0 bytes if not enough space in target
183                         if (target.Length == 0) return 0;
184
185                         char[] charArray = new char[target.Length];
186                         int bytesRead = sourceTextReader.Read(charArray, start, count);
187
188                         // Returns -1 if EOF
189                         if (bytesRead == 0) return -1;
190
191                         for(int index=start; index<start+bytesRead; index++)
192                                 target[index] = (sbyte)charArray[index];
193
194                         return bytesRead;
195                 }
196
197                 /*******************************/
198                 /// <summary>
199                 /// This method returns the literal value received
200                 /// </summary>
201                 /// <param name="literal">The literal to return</param>
202                 /// <returns>The received value</returns>
203                 public static long Identity(long literal)
204                 {
205                         return literal;
206                 }
207
208                 /// <summary>
209                 /// This method returns the literal value received
210                 /// </summary>
211                 /// <param name="literal">The literal to return</param>
212                 /// <returns>The received value</returns>
213                 [CLSCompliantAttribute(false)]
214                 public static ulong Identity(ulong literal)
215                 {
216                         return literal;
217                 }
218
219                 /// <summary>
220                 /// This method returns the literal value received
221                 /// </summary>
222                 /// <param name="literal">The literal to return</param>
223                 /// <returns>The received value</returns>
224                 public static float Identity(float literal)
225                 {
226                         return literal;
227                 }
228
229                 /// <summary>
230                 /// This method returns the literal value received
231                 /// </summary>
232                 /// <param name="literal">The literal to return</param>
233                 /// <returns>The received value</returns>
234                 public static double Identity(double literal)
235                 {
236                         return literal;
237                 }
238
239                 /*******************************/
240                 /// <summary>
241                 /// The class performs token processing from strings
242                 /// </summary>
243                 public class Tokenizer
244                 {
245                         //Element list identified
246                         private System.Collections.ArrayList elements;
247                         //Source string to use
248                         private string source;
249                         //The tokenizer uses the default delimiter set: the space character, the tab character, the newline character, and the carriage-return character
250                         private string delimiters = " \t\n\r";          
251
252                         private bool returnDelims=false;
253                         /// <summary>
254                         /// Initializes a new class instance with a specified string to process
255                         /// </summary>
256                         /// <param name="source">String to tokenize</param>
257                         public Tokenizer(string source)
258                         {                       
259                                 this.elements = new System.Collections.ArrayList();
260                                 this.elements.AddRange(source.Split(this.delimiters.ToCharArray()));
261                                 this.RemoveEmptyStrings();
262                                 this.source = source;
263                         }
264
265                         /// <summary>
266                         /// Initializes a new class instance with a specified string to process
267                         /// and the specified token delimiters to use
268                         /// </summary>
269                         /// <param name="source">String to tokenize</param>
270                         /// <param name="delimiters">String containing the delimiters</param>
271                         public Tokenizer(string source, string delimiters)
272                         {
273                                 this.elements = new System.Collections.ArrayList();
274                                 this.delimiters = delimiters;
275                                 this.elements.AddRange(source.Split(this.delimiters.ToCharArray()));
276                                 this.RemoveEmptyStrings();
277                                 this.source = source;
278                         }
279
280                         public Tokenizer(string source, string delimiters,bool retDel)
281                         {
282                                 this.elements = new System.Collections.ArrayList();
283                                 this.delimiters = delimiters;
284                                 this.source = source;
285                                 this.returnDelims = retDel;
286                                 if( returnDelims)
287                                         Tokenize();
288                                 else
289                                         this.elements.AddRange(source.Split(this.delimiters.ToCharArray()));
290                                 this.RemoveEmptyStrings();
291                         }
292                 
293                         private void Tokenize()
294                         {
295                                 string tempstr = this.source;
296                                 string toks = "";
297                                 if (tempstr.IndexOfAny(this.delimiters.ToCharArray()) < 0 && tempstr.Length > 0)
298                                 {
299                                         this.elements.Add(tempstr);
300                                 }
301                                 else if (tempstr.IndexOfAny(this.delimiters.ToCharArray()) < 0 && tempstr.Length <= 0)
302                                 {
303                                         return;
304                                 }
305                                 while (tempstr.IndexOfAny(this.delimiters.ToCharArray()) >= 0)
306                                 {
307                                         if(tempstr.IndexOfAny(this.delimiters.ToCharArray()) == 0)
308                                         {
309                                                 if (tempstr.Length > 1 )
310                                                 {
311                                                         this.elements.Add(tempstr.Substring(0,1));
312                                                         tempstr=tempstr.Substring(1);
313                                                 }
314                                                 else
315                                                         tempstr = "";
316                                         }
317                                         else
318                                         {
319                                                 toks = tempstr.Substring(0,tempstr.IndexOfAny(this.delimiters.ToCharArray()));
320                                                 this.elements.Add(toks);
321                                                 this.elements.Add(tempstr.Substring(toks.Length,1));
322                                                 if ( tempstr.Length > (toks.Length + 1))
323                                                 {
324                                                         tempstr = tempstr.Substring(toks.Length + 1);
325                                                 }
326                                                 else
327                                                         tempstr = "";
328                                         }
329                                 }
330                                 if (tempstr.Length > 0)
331                                 {
332                                         this.elements.Add(tempstr);
333                                 }
334                         }
335                                                 
336                         /// <summary>
337                         /// Current token count for the source string
338                         /// </summary>
339                         public int Count
340                         {
341                                 get
342                                 {
343                                         return (this.elements.Count);
344                                 }
345                         }
346
347                         /// <summary>
348                         /// Determines if there are more tokens to return from the source string
349                         /// </summary>
350                         /// <returns>True or false, depending if there are more tokens</returns>
351                         public bool HasMoreTokens()
352                         {
353                                 return (this.elements.Count > 0);                       
354                         }
355
356                         /// <summary>
357                         /// Returns the next token from the token list
358                         /// </summary>
359                         /// <returns>The string value of the token</returns>
360                         public string NextToken()
361                         {                       
362                                 string result;
363                                 if (source == "") throw new System.Exception();
364                                 else
365                                 {
366                                         if(returnDelims){
367 //                                              Tokenize();
368                                                 RemoveEmptyStrings();           
369                                                 result = (string) this.elements[0];
370                                                 this.elements.RemoveAt(0);
371                                                 return result;
372                                         }
373                                         else
374                                         {
375                                                 this.elements = new System.Collections.ArrayList();
376                                                 this.elements.AddRange(this.source.Split(delimiters.ToCharArray()));
377                                                 RemoveEmptyStrings();           
378                                                 result = (string) this.elements[0];
379                                                 this.elements.RemoveAt(0);                              
380                                                 this.source = this.source.Remove(this.source.IndexOf(result),result.Length);
381                                                 this.source = this.source.TrimStart(this.delimiters.ToCharArray());
382                                                 return result;
383                                         }
384                                 }                       
385                         }
386
387                         /// <summary>
388                         /// Returns the next token from the source string, using the provided
389                         /// token delimiters
390                         /// </summary>
391                         /// <param name="delimiters">String containing the delimiters to use</param>
392                         /// <returns>The string value of the token</returns>
393                         public string NextToken(string delimiters)
394                         {
395                                 this.delimiters = delimiters;
396                                 return NextToken();
397                         }
398
399                         /// <summary>
400                         /// Removes all empty strings from the token list
401                         /// </summary>
402                         private void RemoveEmptyStrings()
403                         {
404                                 for (int index=0; index < this.elements.Count; index++)
405                                         if ((string)this.elements[index]== "")
406                                         {
407                                                 this.elements.RemoveAt(index);
408                                                 index--;
409                                         }
410                         }
411                 }
412
413                 /*******************************/
414                 /// <summary>
415                 /// Provides support for DateFormat
416                 /// </summary>
417                 public class DateTimeFormatManager
418                 {
419                         static public DateTimeFormatHashTable manager = new DateTimeFormatHashTable();
420
421                         /// <summary>
422                         /// Hashtable class to provide functionality for dateformat properties
423                         /// </summary>
424                         public class DateTimeFormatHashTable :System.Collections.Hashtable 
425                         {
426                                 /// <summary>
427                                 /// Sets the format for datetime.
428                                 /// </summary>
429                                 /// <param name="format">DateTimeFormat instance to set the pattern</param>
430                                 /// <param name="newPattern">A string with the pattern format</param>
431                                 public void SetDateFormatPattern(System.Globalization.DateTimeFormatInfo format, System.String newPattern)
432                                 {
433                                         if (this[format] != null)
434                                                 ((DateTimeFormatProperties) this[format]).DateFormatPattern = newPattern;
435                                         else
436                                         {
437                                                 DateTimeFormatProperties tempProps = new DateTimeFormatProperties();
438                                                 tempProps.DateFormatPattern  = newPattern;
439                                                 Add(format, tempProps);
440                                         }
441                                 }
442
443                                 /// <summary>
444                                 /// Gets the current format pattern of the DateTimeFormat instance
445                                 /// </summary>
446                                 /// <param name="format">The DateTimeFormat instance which the value will be obtained</param>
447                                 /// <returns>The string representing the current datetimeformat pattern</returns>
448                                 public string GetDateFormatPattern(System.Globalization.DateTimeFormatInfo format)
449                                 {
450                                         if (this[format] == null)
451                                                 return "d-MMM-yy";
452                                         else
453                                                 return ((DateTimeFormatProperties) this[format]).DateFormatPattern;
454                                 }
455                 
456                                 /// <summary>
457                                 /// Sets the datetimeformat pattern to the giving format
458                                 /// </summary>
459                                 /// <param name="format">The datetimeformat instance to set</param>
460                                 /// <param name="newPattern">The new datetimeformat pattern</param>
461                                 public void SetTimeFormatPattern(System.Globalization.DateTimeFormatInfo format, System.String newPattern)
462                                 {
463                                         if (this[format] != null)
464                                                 ((DateTimeFormatProperties) this[format]).TimeFormatPattern = newPattern;
465                                         else
466                                         {
467                                                 DateTimeFormatProperties tempProps = new DateTimeFormatProperties();
468                                                 tempProps.TimeFormatPattern  = newPattern;
469                                                 Add(format, tempProps);
470                                         }
471                                 }
472
473                                 /// <summary>
474                                 /// Gets the current format pattern of the DateTimeFormat instance
475                                 /// </summary>
476                                 /// <param name="format">The DateTimeFormat instance which the value will be obtained</param>
477                                 /// <returns>The string representing the current datetimeformat pattern</returns>
478                                 public string GetTimeFormatPattern(System.Globalization.DateTimeFormatInfo format)
479                                 {
480                                         if (this[format] == null)
481                                                 return "h:mm:ss tt";
482                                         else
483                                                 return ((DateTimeFormatProperties) this[format]).TimeFormatPattern;
484                                 }
485
486                                 /// <summary>
487                                 /// Internal class to provides the DateFormat and TimeFormat pattern properties on .NET
488                                 /// </summary>
489                                 class DateTimeFormatProperties
490                                 {
491                                         public string DateFormatPattern = "d-MMM-yy";
492                                         public string TimeFormatPattern = "h:mm:ss tt";
493                                 }
494                         }       
495                 }
496                 /*******************************/
497                 /// <summary>
498                 /// Gets the DateTimeFormat instance and date instance to obtain the date with the format passed
499                 /// </summary>
500                 /// <param name="format">The DateTimeFormat to obtain the time and date pattern</param>
501                 /// <param name="date">The date instance used to get the date</param>
502                 /// <returns>A string representing the date with the time and date patterns</returns>
503                 public static string FormatDateTime(System.Globalization.DateTimeFormatInfo format, System.DateTime date)
504                 {
505                         string timePattern = DateTimeFormatManager.manager.GetTimeFormatPattern(format);
506                         string datePattern = DateTimeFormatManager.manager.GetDateFormatPattern(format);
507                         return date.ToString(datePattern + " " + timePattern, format);            
508                 }
509
510                 /*******************************/
511                 /// <summary>
512                 /// Adds a new key-and-value pair into the hash table
513                 /// </summary>
514                 /// <param name="collection">The collection to work with</param>
515                 /// <param name="key">Key used to obtain the value</param>
516                 /// <param name="newValue">Value asociated with the key</param>
517                 /// <returns>The old element associated with the key</returns>
518                 public static System.Object PutElement(System.Collections.IDictionary collection, System.Object key, System.Object newValue)
519                 {
520                         System.Object element = collection[key];
521                         collection[key] = newValue;
522                         return element;
523                 }
524
525                 /*******************************/
526                 /// <summary>
527                 /// This class contains static methods to manage arrays.
528                 /// </summary>
529                 public class ArrayListSupport
530                 {
531                         /// <summary>
532                         /// Obtains an array containing all the elements of the collection. 
533                         /// </summary>
534                         /// <param name="collection">The collection from wich to obtain the elements.</param>
535                         /// <param name="objects">The array containing all the elements of the collection.</param>
536                         /// <returns>The array containing all the elements of the collection.</returns>
537                         public static System.Object[] ToArray(System.Collections.ArrayList collection, System.Object[] objects)
538                         {       
539                                 int index = 0;
540                                 System.Collections.IEnumerator tempEnumerator = collection.GetEnumerator();
541                                 while (tempEnumerator.MoveNext())
542                                         objects[index++] = tempEnumerator.Current;
543                                 return objects;
544                         }
545                 }
546
547
548                 /*******************************/
549                 /// <summary>
550                 /// Removes the first occurrence of an specific object from an ArrayList instance.
551                 /// </summary>
552                 /// <param name="arrayList">The ArrayList instance</param>
553                 /// <param name="element">The element to remove</param>
554                 /// <returns>True if item is found in the ArrayList; otherwise, false</returns>  
555                 public static System.Boolean VectorRemoveElement(System.Collections.IList arrayList, System.Object element)
556                 {
557                         System.Boolean containsItem = arrayList.Contains(element);
558                         arrayList.Remove(element);
559                         return containsItem;
560                 }
561
562                 /*******************************/
563                 /// <summary>
564                 /// Support class used to handle threads
565                 /// </summary>
566                 public class ThreadClass : IThreadRunnable
567                 {
568                         /// <summary>
569                         /// The instance of System.Threading.Thread
570                         /// </summary>
571                         private System.Threading.Thread threadField;
572               
573                         /// <summary>
574                         /// Initializes a new instance of the ThreadClass class
575                         /// </summary>
576                         public ThreadClass()
577                         {
578                                 threadField = new System.Threading.Thread(new System.Threading.ThreadStart(Run));
579                         }
580          
581                         /// <summary>
582                         /// Initializes a new instance of the Thread class.
583                         /// </summary>
584                         /// <param name="Name">The name of the thread</param>
585                         public ThreadClass(string Name)
586                         {
587                                 threadField = new System.Threading.Thread(new System.Threading.ThreadStart(Run));
588                                 this.Name = Name;
589                         }
590               
591                         /// <summary>
592                         /// Initializes a new instance of the Thread class.
593                         /// </summary>
594                         /// <param name="Start">A ThreadStart delegate that references the methods to be invoked when this thread begins executing</param>
595                         public ThreadClass(System.Threading.ThreadStart Start)
596                         {
597                                 threadField = new System.Threading.Thread(Start);
598                         }
599          
600                         /// <summary>
601                         /// Initializes a new instance of the Thread class.
602                         /// </summary>
603                         /// <param name="Start">A ThreadStart delegate that references the methods to be invoked when this thread begins executing</param>
604                         /// <param name="Name">The name of the thread</param>
605                         public ThreadClass(System.Threading.ThreadStart Start, string Name)
606                         {
607                                 threadField = new System.Threading.Thread(Start);
608                                 this.Name = Name;
609                         }
610               
611                         /// <summary>
612                         /// This method has no functionality unless the method is overridden
613                         /// </summary>
614                         public virtual void Run()
615                         {
616                         }
617               
618                         /// <summary>
619                         /// Causes the operating system to change the state of the current thread instance to ThreadState.Running
620                         /// </summary>
621                         public virtual void Start()
622                         {
623                                 threadField.Start();
624                         }
625               
626                         /// <summary>
627                         /// Interrupts a thread that is in the WaitSleepJoin thread state
628                         /// </summary>
629                         public virtual void Interrupt()
630                         {
631                                 threadField.Interrupt();
632                         }
633               
634                         /// <summary>
635                         /// Gets the current thread instance
636                         /// </summary>
637                         public System.Threading.Thread Instance
638                         {
639                                 get
640                                 {
641                                         return threadField;
642                                 }
643                                 set
644                                 {
645                                         threadField = value;
646                                 }
647                         }
648               
649                         /// <summary>
650                         /// Gets or sets the name of the thread
651                         /// </summary>
652                         public System.String Name
653                         {
654                                 get
655                                 {
656                                         return threadField.Name;
657                                 }
658                                 set
659                                 {
660                                         if (threadField.Name == null)
661                                                 threadField.Name = value; 
662                                 }
663                         }
664               
665                         /// <summary>
666                         /// Gets or sets a value indicating the scheduling priority of a thread
667                         /// </summary>
668                         public System.Threading.ThreadPriority Priority
669                         {
670                                 get
671                                 {
672                                         return threadField.Priority;
673                                 }
674                                 set
675                                 {
676                                         threadField.Priority = value;
677                                 }
678                         }
679               
680                         /// <summary>
681                         /// Gets a value indicating the execution status of the current thread
682                         /// </summary>
683                         public bool IsAlive
684                         {
685                                 get
686                                 {
687                                         return threadField.IsAlive;
688                                 }
689                         }
690               
691                         /// <summary>
692                         /// Gets or sets a value indicating whether or not a thread is a background thread.
693                         /// </summary>
694                         public bool IsBackground
695                         {
696                                 get
697                                 {
698                                         return threadField.IsBackground;
699                                 } 
700                                 set
701                                 {
702                                         threadField.IsBackground = value;
703                                 }
704                         }
705               
706                         /// <summary>
707                         /// Blocks the calling thread until a thread terminates
708                         /// </summary>
709                         public void Join()
710                         {
711                                 threadField.Join();
712                         }
713               
714                         /// <summary>
715                         /// Blocks the calling thread until a thread terminates or the specified time elapses
716                         /// </summary>
717                         /// <param name="MiliSeconds">Time of wait in milliseconds</param>
718                         public void Join(long MiliSeconds)
719                         {
720                                 lock(this)
721                                 {
722                                         threadField.Join(new System.TimeSpan(MiliSeconds * 10000));
723                                 }
724                         }
725               
726                         /// <summary>
727                         /// Blocks the calling thread until a thread terminates or the specified time elapses
728                         /// </summary>
729                         /// <param name="MiliSeconds">Time of wait in milliseconds</param>
730                         /// <param name="NanoSeconds">Time of wait in nanoseconds</param>
731                         public void Join(long MiliSeconds, int NanoSeconds)
732                         {
733                                 lock(this)
734                                 {
735                                         threadField.Join(new System.TimeSpan(MiliSeconds * 10000 + NanoSeconds * 100));
736                                 }
737                         }
738               
739                         /// <summary>
740                         /// Resumes a thread that has been suspended
741                         /// </summary>
742                         public void Resume()
743                         {
744                                 threadField.Resume();
745                         }
746               
747                         /// <summary>
748                         /// Raises a ThreadAbortException in the thread on which it is invoked, 
749                         /// to begin the process of terminating the thread. Calling this method 
750                         /// usually terminates the thread
751                         /// </summary>
752                         public void Abort()
753                         {
754                                 threadField.Abort();
755                         }
756               
757                         /// <summary>
758                         /// Raises a ThreadAbortException in the thread on which it is invoked, 
759                         /// to begin the process of terminating the thread while also providing
760                         /// exception information about the thread termination. 
761                         /// Calling this method usually terminates the thread.
762                         /// </summary>
763                         /// <param name="stateInfo">An object that contains application-specific information, such as state, which can be used by the thread being aborted</param>
764                         public void Abort(System.Object stateInfo)
765                         {
766                                 lock(this)
767                                 {
768                                         threadField.Abort(stateInfo);
769                                 }
770                         }
771               
772                         /// <summary>
773                         /// Suspends the thread, if the thread is already suspended it has no effect
774                         /// </summary>
775                         public void Suspend()
776                         {
777                                 threadField.Suspend();
778                         }
779               
780                         /// <summary>
781                         /// Obtain a String that represents the current Object
782                         /// </summary>
783                         /// <returns>A String that represents the current Object</returns>
784                         public override System.String ToString()
785                         {
786                                 return "Thread[" + Name + "," + Priority.ToString() + "," + "" + "]";
787                         }
788              
789                         /// <summary>
790                         /// Gets the currently running thread
791                         /// </summary>
792                         /// <returns>The currently running thread</returns>
793                         public static ThreadClass Current()
794                         {
795                                 ThreadClass CurrentThread = new ThreadClass();
796                                 CurrentThread.Instance = System.Threading.Thread.CurrentThread;
797                                 return CurrentThread;
798                         }
799                 }
800
801
802                 /*******************************/
803                 /// <summary>
804                 /// This class contains different methods to manage Collections.
805                 /// </summary>
806                 public class CollectionSupport : System.Collections.CollectionBase
807                 {
808                         /// <summary>
809                         /// Creates an instance of the Collection by using an inherited constructor.
810                         /// </summary>
811                         public CollectionSupport() : base()
812                         {                       
813                         }
814
815                         /// <summary>
816                         /// Adds an specified element to the collection.
817                         /// </summary>
818                         /// <param name="element">The element to be added.</param>
819                         /// <returns>Returns true if the element was successfuly added. Otherwise returns false.</returns>
820                         public virtual bool Add(System.Object element)
821                         {
822                                 return (this.List.Add(element) != -1);
823                         }       
824
825                         /// <summary>
826                         /// Adds all the elements contained in the specified collection.
827                         /// </summary>
828                         /// <param name="collection">The collection used to extract the elements that will be added.</param>
829                         /// <returns>Returns true if all the elements were successfuly added. Otherwise returns false.</returns>
830                         public virtual bool AddAll(System.Collections.ICollection collection)
831                         {
832                                 bool result = false;
833                                 if (collection!=null)
834                                 {
835                                         System.Collections.IEnumerator tempEnumerator = new System.Collections.ArrayList(collection).GetEnumerator();
836                                         while (tempEnumerator.MoveNext())
837                                         {
838                                                 if (tempEnumerator.Current != null)
839                                                         result = this.Add(tempEnumerator.Current);
840                                         }
841                                 }
842                                 return result;
843                         }
844
845
846                         /// <summary>
847                         /// Adds all the elements contained in the specified support class collection.
848                         /// </summary>
849                         /// <param name="collection">The collection used to extract the elements that will be added.</param>
850                         /// <returns>Returns true if all the elements were successfuly added. Otherwise returns false.</returns>
851                         public virtual bool AddAll(CollectionSupport collection)
852                         {
853                                 return this.AddAll((System.Collections.ICollection)collection);
854                         }
855
856                         /// <summary>
857                         /// Verifies if the specified element is contained into the collection. 
858                         /// </summary>
859                         /// <param name="element"> The element that will be verified.</param>
860                         /// <returns>Returns true if the element is contained in the collection. Otherwise returns false.</returns>
861                         public virtual bool Contains(System.Object element)
862                         {
863                                 return this.List.Contains(element);
864                         }
865
866                         /// <summary>
867                         /// Verifies if all the elements of the specified collection are contained into the current collection.
868                         /// </summary>
869                         /// <param name="collection">The collection used to extract the elements that will be verified.</param>
870                         /// <returns>Returns true if all the elements are contained in the collection. Otherwise returns false.</returns>
871                         public virtual bool ContainsAll(System.Collections.ICollection collection)
872                         {
873                                 bool result = false;
874                                 System.Collections.IEnumerator tempEnumerator = new System.Collections.ArrayList(collection).GetEnumerator();
875                                 while (tempEnumerator.MoveNext())
876                                         if (!(result = this.Contains(tempEnumerator.Current)))
877                                                 break;
878                                 return result;
879                         }
880
881                         /// <summary>
882                         /// Verifies if all the elements of the specified collection are contained into the current collection.
883                         /// </summary>
884                         /// <param name="collection">The collection used to extract the elements that will be verified.</param>
885                         /// <returns>Returns true if all the elements are contained in the collection. Otherwise returns false.</returns>
886                         public virtual bool ContainsAll(CollectionSupport collection)
887                         {
888                                 return this.ContainsAll((System.Collections.ICollection) collection);
889                         }
890
891                         /// <summary>
892                         /// Verifies if the collection is empty.
893                         /// </summary>
894                         /// <returns>Returns true if the collection is empty. Otherwise returns false.</returns>
895                         public virtual bool IsEmpty()
896                         {
897                                 return (this.Count == 0);
898                         }
899
900                         /// <summary>
901                         /// Removes an specified element from the collection.
902                         /// </summary>
903                         /// <param name="element">The element to be removed.</param>
904                         /// <returns>Returns true if the element was successfuly removed. Otherwise returns false.</returns>
905                         public virtual bool Remove(System.Object element)
906                         {
907                                 bool result = false;
908                                 if (this.Contains(element))
909                                 {
910                                         this.List.Remove(element);
911                                         result = true;
912                                 }
913                                 return result;
914                         }
915
916                         /// <summary>
917                         /// Removes all the elements contained into the specified collection.
918                         /// </summary>
919                         /// <param name="collection">The collection used to extract the elements that will be removed.</param>
920                         /// <returns>Returns true if all the elements were successfuly removed. Otherwise returns false.</returns>
921                         public virtual bool RemoveAll(System.Collections.ICollection collection)
922                         { 
923                                 bool result = false;
924                                 System.Collections.IEnumerator tempEnumerator = new System.Collections.ArrayList(collection).GetEnumerator();
925                                 while (tempEnumerator.MoveNext())
926                                 {
927                                         if (this.Contains(tempEnumerator.Current))
928                                                 result = this.Remove(tempEnumerator.Current);
929                                 }
930                                 return result;
931                         }
932
933                         /// <summary>
934                         /// Removes all the elements contained into the specified collection.
935                         /// </summary>
936                         /// <param name="collection">The collection used to extract the elements that will be removed.</param>
937                         /// <returns>Returns true if all the elements were successfuly removed. Otherwise returns false.</returns>
938                         public virtual bool RemoveAll(CollectionSupport collection)
939                         { 
940                                 return this.RemoveAll((System.Collections.ICollection) collection);
941                         }
942
943                         /// <summary>
944                         /// Removes all the elements that aren't contained into the specified collection.
945                         /// </summary>
946                         /// <param name="collection">The collection used to verify the elements that will be retained.</param>
947                         /// <returns>Returns true if all the elements were successfully removed. Otherwise returns false.</returns>
948                         public virtual bool RetainAll(System.Collections.ICollection collection)
949                         {
950                                 bool result = false;
951                                 System.Collections.IEnumerator tempEnumerator = this.GetEnumerator();
952                                 CollectionSupport tempCollection = new CollectionSupport();
953                                 tempCollection.AddAll(collection);
954                                 while (tempEnumerator.MoveNext())
955                                         if (!tempCollection.Contains(tempEnumerator.Current))
956                                         {
957                                                 result = this.Remove(tempEnumerator.Current);
958                                         
959                                                 if (result == true)
960                                                 {
961                                                         tempEnumerator = this.GetEnumerator();
962                                                 }
963                                         }
964                                 return result;
965                         }
966
967                         /// <summary>
968                         /// Removes all the elements that aren't contained into the specified collection.
969                         /// </summary>
970                         /// <param name="collection">The collection used to verify the elements that will be retained.</param>
971                         /// <returns>Returns true if all the elements were successfully removed. Otherwise returns false.</returns>
972                         public virtual bool RetainAll(CollectionSupport collection)
973                         {
974                                 return this.RetainAll((System.Collections.ICollection) collection);
975                         }
976
977                         /// <summary>
978                         /// Obtains an array containing all the elements of the collection.
979                         /// </summary>
980                         /// <returns>The array containing all the elements of the collection</returns>
981                         public virtual System.Object[] ToArray()
982                         {       
983                                 int index = 0;
984                                 System.Object[] objects = new System.Object[this.Count];
985                                 System.Collections.IEnumerator tempEnumerator = this.GetEnumerator();
986                                 while (tempEnumerator.MoveNext())
987                                         objects[index++] = tempEnumerator.Current;
988                                 return objects;
989                         }
990
991                         /// <summary>
992                         /// Obtains an array containing all the elements of the collection.
993                         /// </summary>
994                         /// <param name="objects">The array into which the elements of the collection will be stored.</param>
995                         /// <returns>The array containing all the elements of the collection.</returns>
996                         public virtual System.Object[] ToArray(System.Object[] objects)
997                         {       
998                                 int index = 0;
999                                 System.Collections.IEnumerator tempEnumerator = this.GetEnumerator();
1000                                 while (tempEnumerator.MoveNext())
1001                                         objects[index++] = tempEnumerator.Current;
1002                                 return objects;
1003                         }
1004
1005                         /// <summary>
1006                         /// Creates a CollectionSupport object with the contents specified in array.
1007                         /// </summary>
1008                         /// <param name="array">The array containing the elements used to populate the new CollectionSupport object.</param>
1009                         /// <returns>A CollectionSupport object populated with the contents of array.</returns>
1010                         public static CollectionSupport ToCollectionSupport(System.Object[] array)
1011                         {
1012                                 CollectionSupport tempCollectionSupport = new CollectionSupport();             
1013                                 tempCollectionSupport.AddAll(array);
1014                                 return tempCollectionSupport;
1015                         }
1016                 }
1017
1018                 /*******************************/
1019                 /// <summary>
1020                 /// This class contains different methods to manage list collections.
1021                 /// </summary>
1022                 public class ListCollectionSupport : System.Collections.ArrayList
1023                 {
1024                         /// <summary>
1025                         /// Creates a new instance of the class ListCollectionSupport.
1026                         /// </summary>
1027                         public ListCollectionSupport() : base()
1028                         {
1029                         }
1030  
1031                         /// <summary>
1032                         /// Creates a new instance of the class ListCollectionSupport.
1033                         /// </summary>
1034                         /// <param name="collection">The collection to insert into the new object.</param>
1035                         public ListCollectionSupport(System.Collections.ICollection collection) : base(collection)
1036                         {
1037                         }
1038
1039                         /// <summary>
1040                         /// Creates a new instance of the class ListCollectionSupport with the specified capacity.
1041                         /// </summary>
1042                         /// <param name="capacity">The capacity of the new array.</param>
1043                         public ListCollectionSupport(int capacity) : base(capacity)
1044                         {
1045                         }
1046
1047                         /// <summary>
1048                         /// Adds an object to the end of the List.
1049                         /// </summary>          
1050                         /// <param name="valueToInsert">The value to insert in the array list.</param>
1051                         /// <returns>Returns true after adding the value.</returns>
1052                         public virtual bool Add(System.Object valueToInsert)
1053                         {
1054                                 base.Insert(this.Count, valueToInsert);
1055                                 return true;
1056                         }
1057
1058                         /// <summary>
1059                         /// Adds all the elements contained into the specified collection, starting at the specified position.
1060                         /// </summary>
1061                         /// <param name="index">Position at which to add the first element from the specified collection.</param>
1062                         /// <param name="list">The list used to extract the elements that will be added.</param>
1063                         /// <returns>Returns true if all the elements were successfuly added. Otherwise returns false.</returns>
1064                         public virtual bool AddAll(int index, System.Collections.IList list)
1065                         {
1066                                 bool result = false;
1067                                 if (list!=null)
1068                                 {
1069                                         System.Collections.IEnumerator tempEnumerator = new System.Collections.ArrayList(list).GetEnumerator();
1070                                         int tempIndex = index;
1071                                         while (tempEnumerator.MoveNext())
1072                                         {
1073                                                 base.Insert(tempIndex++, tempEnumerator.Current);
1074                                                 result = true;
1075                                         }
1076                                 }
1077                                 return result;
1078                         }
1079
1080                         /// <summary>
1081                         /// Adds all the elements contained in the specified collection.
1082                         /// </summary>
1083                         /// <param name="collection">The collection used to extract the elements that will be added.</param>
1084                         /// <returns>Returns true if all the elements were successfuly added. Otherwise returns false.</returns>
1085                         public virtual bool AddAll(System.Collections.IList collection)
1086                         {
1087                                 return this.AddAll(this.Count,collection);
1088                         }
1089
1090                         /// <summary>
1091                         /// Adds all the elements contained in the specified support class collection.
1092                         /// </summary>
1093                         /// <param name="collection">The collection used to extract the elements that will be added.</param>
1094                         /// <returns>Returns true if all the elements were successfuly added. Otherwise returns false.</returns>
1095                         public virtual bool AddAll(CollectionSupport collection)
1096                         {
1097                                 return this.AddAll(this.Count,collection);
1098                         }
1099
1100                         /// <summary>
1101                         /// Adds all the elements contained into the specified support class collection, starting at the specified position.
1102                         /// </summary>
1103                         /// <param name="index">Position at which to add the first element from the specified collection.</param>
1104                         /// <param name="list">The list used to extract the elements that will be added.</param>
1105                         /// <returns>Returns true if all the elements were successfuly added. Otherwise returns false.</returns>
1106                         public virtual bool AddAll(int index, CollectionSupport collection)
1107                         {
1108                                 return this.AddAll(index,(System.Collections.IList)collection);
1109                         }
1110                 
1111                         /// <summary>
1112                         /// Creates a copy of the ListCollectionSupport.
1113                         /// </summary>
1114                         /// <returns> A copy of the ListCollectionSupport.</returns>
1115                         public virtual System.Object ListCollectionClone()
1116                         {
1117                                 return MemberwiseClone();
1118                         }
1119
1120
1121                         /// <summary>
1122                         /// Returns an iterator of the collection.
1123                         /// </summary>
1124                         /// <returns>An IEnumerator.</returns>
1125                         public virtual System.Collections.IEnumerator ListIterator()
1126                         {
1127                                 return base.GetEnumerator();
1128                         }
1129
1130                         /// <summary>
1131                         /// Removes all the elements contained into the specified collection.
1132                         /// </summary>
1133                         /// <param name="collection">The collection used to extract the elements that will be removed.</param>
1134                         /// <returns>Returns true if all the elements were successfuly removed. Otherwise returns false.</returns>
1135                         public virtual bool RemoveAll(System.Collections.ICollection collection)
1136                         { 
1137                                 bool result = false;
1138                                 System.Collections.IEnumerator tempEnumerator = new System.Collections.ArrayList(collection).GetEnumerator();
1139                                 while (tempEnumerator.MoveNext())
1140                                 {
1141                                         result = true;
1142                                         if (base.Contains(tempEnumerator.Current))
1143                                                 base.Remove(tempEnumerator.Current);
1144                                 }
1145                                 return result;
1146                         }
1147                 
1148                         /// <summary>
1149                         /// Removes all the elements contained into the specified collection.
1150                         /// </summary>
1151                         /// <param name="collection">The collection used to extract the elements that will be removed.</param>
1152                         /// <returns>Returns true if all the elements were successfuly removed. Otherwise returns false.</returns>
1153                         public virtual bool RemoveAll(CollectionSupport collection)
1154                         { 
1155                                 return this.RemoveAll((System.Collections.ICollection) collection);
1156                         }               
1157
1158                         /// <summary>
1159                         /// Removes the value in the specified index from the list.
1160                         /// </summary>          
1161                         /// <param name="index">The index of the value to remove.</param>
1162                         /// <returns>Returns the value removed.</returns>
1163                         public virtual System.Object RemoveElement(int index)
1164                         {
1165                                 System.Object objectRemoved = this[index];
1166                                 this.RemoveAt(index);
1167                                 return objectRemoved;
1168                         }
1169
1170                         /// <summary>
1171                         /// Removes an specified element from the collection.
1172                         /// </summary>
1173                         /// <param name="element">The element to be removed.</param>
1174                         /// <returns>Returns true if the element was successfuly removed. Otherwise returns false.</returns>
1175                         public virtual bool RemoveElement(System.Object element)
1176                         {
1177
1178                                 bool result = false;
1179                                 if (this.Contains(element))
1180                                 {
1181                                         base.Remove(element);
1182                                         result = true;
1183                                 }
1184                                 return result;
1185                         }
1186
1187                         /// <summary>
1188                         /// Removes the first value from an array list.
1189                         /// </summary>          
1190                         /// <returns>Returns the value removed.</returns>
1191                         public virtual System.Object RemoveFirst()
1192                         {
1193                                 System.Object objectRemoved = this[0];
1194                                 this.RemoveAt(0);
1195                                 return objectRemoved;
1196                         }
1197
1198                         /// <summary>
1199                         /// Removes the last value from an array list.
1200                         /// </summary>
1201                         /// <returns>Returns the value removed.</returns>
1202                         public virtual System.Object RemoveLast()
1203                         {
1204                                 System.Object objectRemoved = this[this.Count-1];
1205                                 base.RemoveAt(this.Count-1);
1206                                 return objectRemoved;
1207                         }
1208
1209                         /// <summary>
1210                         /// Removes all the elements that aren't contained into the specified collection.
1211                         /// </summary>
1212                         /// <param name="collection">The collection used to verify the elements that will be retained.</param>
1213                         /// <returns>Returns true if all the elements were successfully removed. Otherwise returns false.</returns>
1214                         public virtual bool RetainAll(System.Collections.ICollection collection)
1215                         {
1216                                 bool result = false;
1217                                 System.Collections.IEnumerator tempEnumerator = this.GetEnumerator();
1218                                 ListCollectionSupport tempCollection = new ListCollectionSupport(collection);
1219                                 while (tempEnumerator.MoveNext())
1220                                         if (!tempCollection.Contains(tempEnumerator.Current))
1221                                         {
1222                                                 result = this.RemoveElement(tempEnumerator.Current);
1223                                         
1224                                                 if (result == true)
1225                                                 {
1226                                                         tempEnumerator = this.GetEnumerator();
1227                                                 }
1228                                         }
1229                                 return result;
1230                         }
1231                 
1232                         /// <summary>
1233                         /// Removes all the elements that aren't contained into the specified collection.
1234                         /// </summary>
1235                         /// <param name="collection">The collection used to verify the elements that will be retained.</param>
1236                         /// <returns>Returns true if all the elements were successfully removed. Otherwise returns false.</returns>
1237                         public virtual bool RetainAll(CollectionSupport collection)
1238                         {
1239                                 return this.RetainAll((System.Collections.ICollection) collection);
1240                         }               
1241
1242                         /// <summary>
1243                         /// Verifies if all the elements of the specified collection are contained into the current collection.
1244                         /// </summary>
1245                         /// <param name="collection">The collection used to extract the elements that will be verified.</param>
1246                         /// <returns>Returns true if all the elements are contained in the collection. Otherwise returns false.</returns>
1247                         public virtual bool ContainsAll(System.Collections.ICollection collection)
1248                         {
1249                                 bool result = false;
1250                                 System.Collections.IEnumerator tempEnumerator = new System.Collections.ArrayList(collection).GetEnumerator();
1251                                 while (tempEnumerator.MoveNext())
1252                                         if(!(result = this.Contains(tempEnumerator.Current)))
1253                                                 break;
1254                                 return result;
1255                         }
1256                 
1257                         /// <summary>
1258                         /// Verifies if all the elements of the specified collection are contained into the current collection.
1259                         /// </summary>
1260                         /// <param name="collection">The collection used to extract the elements that will be verified.</param>
1261                         /// <returns>Returns true if all the elements are contained in the collection. Otherwise returns false.</returns>
1262                         public virtual bool ContainsAll(CollectionSupport collection)
1263                         {
1264                                 return this.ContainsAll((System.Collections.ICollection) collection);
1265                         }               
1266
1267                         /// <summary>
1268                         /// Returns a new list containing a portion of the current list between a specified range. 
1269                         /// </summary>
1270                         /// <param name="startIndex">The start index of the range.</param>
1271                         /// <param name="endIndex">The end index of the range.</param>
1272                         /// <returns>A ListCollectionSupport instance containing the specified elements.</returns>
1273                         public virtual ListCollectionSupport SubList(int startIndex, int endIndex)
1274                         {
1275                                 int index = 0;
1276                                 System.Collections.IEnumerator tempEnumerator = this.GetEnumerator();
1277                                 ListCollectionSupport result = new ListCollectionSupport();
1278                                 for(index = startIndex; index < endIndex; index++)
1279                                         result.Add(this[index]);
1280                                 return (ListCollectionSupport)result;
1281                         }
1282
1283                         /// <summary>
1284                         /// Obtains an array containing all the elements of the collection.
1285                         /// </summary>
1286                         /// <param name="objects">The array into which the elements of the collection will be stored.</param>
1287                         /// <returns>The array containing all the elements of the collection.</returns>
1288                         public virtual System.Object[] ToArray(System.Object[] objects)
1289                         {       
1290                                 if (objects.Length < this.Count)
1291                                         objects = new System.Object[this.Count];
1292                                 int index = 0;
1293                                 System.Collections.IEnumerator tempEnumerator = this.GetEnumerator();
1294                                 while (tempEnumerator.MoveNext())
1295                                         objects[index++] = tempEnumerator.Current;
1296                                 return objects;
1297                         }
1298
1299                         /// <summary>
1300                         /// Returns an iterator of the collection starting at the specified position.
1301                         /// </summary>
1302                         /// <param name="index">The position to set the iterator.</param>
1303                         /// <returns>An IEnumerator at the specified position.</returns>
1304                         public virtual System.Collections.IEnumerator ListIterator(int index)
1305                         {
1306                                 if ((index < 0) || (index > this.Count)) throw new System.IndexOutOfRangeException();                   
1307                                 System.Collections.IEnumerator tempEnumerator= this.GetEnumerator();
1308                                 if (index > 0)
1309                                 {
1310                                         int i=0;
1311                                         while ((tempEnumerator.MoveNext()) && (i < index - 1))
1312                                                 i++;
1313                                 }
1314                                 return tempEnumerator;                  
1315                         }
1316         
1317                         /// <summary>
1318                         /// Gets the last value from a list.
1319                         /// </summary>
1320                         /// <returns>Returns the last element of the list.</returns>
1321                         public virtual System.Object GetLast()
1322                         {
1323                                 if (this.Count == 0) throw new System.ArgumentOutOfRangeException();
1324                                 else
1325                                 {
1326                                         return this[this.Count - 1];
1327                                 }                                                                        
1328                         }
1329                 
1330                         /// <summary>
1331                         /// Return whether this list is empty.
1332                         /// </summary>
1333                         /// <returns>True if the list is empty, false if it isn't.</returns>
1334                         public virtual bool IsEmpty()
1335                         {
1336                                 return (this.Count == 0);
1337                         }
1338                 
1339                         /// <summary>
1340                         /// Replaces the element at the specified position in this list with the specified element.
1341                         /// </summary>
1342                         /// <param name="index">Index of element to replace.</param>
1343                         /// <param name="element">Element to be stored at the specified position.</param>
1344                         /// <returns>The element previously at the specified position.</returns>
1345                         public virtual System.Object Set(int index, System.Object element)
1346                         {
1347                                 System.Object result = this[index];
1348                                 this[index] = element;
1349                                 return result;
1350                         } 
1351
1352                         /// <summary>
1353                         /// Returns the element at the specified position in the list.
1354                         /// </summary>
1355                         /// <param name="index">Index of element to return.</param>
1356                         /// <param name="element">Element to be stored at the specified position.</param>
1357                         /// <returns>The element at the specified position in the list.</returns>
1358                         public virtual System.Object Get(int index)
1359                         {
1360                                 return this[index];
1361                         }
1362                 }
1363
1364                 /*******************************/
1365                 /// <summary>
1366                 /// This class manages array operations.
1367                 /// </summary>
1368                 public class ArraysSupport
1369                 {
1370                         /// <summary>
1371                         /// Compares the entire members of one array whith the other one.
1372                         /// </summary>
1373                         /// <param name="array1">The array to be compared.</param>
1374                         /// <param name="array2">The array to be compared with.</param>
1375                         /// <returns>True if both arrays are equals otherwise it returns false.</returns>
1376                         /// <remarks>Two arrays are equal if they contains the same elements in the same order.</remarks>
1377                         public static bool IsArrayEqual(System.Array array1, System.Array array2)
1378                         {
1379                                 if (array1.Length != array2.Length)
1380                                         return false;
1381                                 for (int i = 0; i < array1.Length; i++)
1382                                         if (!(array1.GetValue(i).Equals(array2.GetValue(i))))
1383                                                 return false;
1384                                 return true;
1385                         }
1386
1387                         /// <summary>
1388                         /// Fills the array with an specific value from an specific index to an specific index.
1389                         /// </summary>
1390                         /// <param name="array">The array to be filled.</param>
1391                         /// <param name="fromindex">The first index to be filled.</param>
1392                         /// <param name="toindex">The last index to be filled.</param>
1393                         /// <param name="val">The value to fill the array with.</param>
1394                         public static void FillArray(System.Array array, System.Int32 fromindex,System.Int32 toindex, System.Object val)
1395                         {
1396                                 System.Object Temp_Object = val;
1397                                 System.Type elementtype = array.GetType().GetElementType();
1398                                 if (elementtype != val.GetType())
1399                                         Temp_Object = System.Convert.ChangeType(val, elementtype);
1400                                 if (array.Length == 0)
1401                                         throw (new System.NullReferenceException());
1402                                 if (fromindex > toindex)
1403                                         throw (new System.ArgumentException());
1404                                 if ((fromindex < 0) || ((System.Array)array).Length < toindex)
1405                                         throw (new System.IndexOutOfRangeException());
1406                                 for (int index = (fromindex > 0) ? fromindex-- : fromindex; index < toindex; index++)
1407                                         array.SetValue(Temp_Object, index);
1408                         }
1409
1410                         /// <summary>
1411                         /// Fills the array with an specific value.
1412                         /// </summary>
1413                         /// <param name="array">The array to be filled.</param>
1414                         /// <param name="val">The value to fill the array with.</param>
1415                         public static void FillArray(System.Array array, System.Object val)
1416                         {
1417                                 FillArray(array, 0, array.Length, val);
1418                         }
1419                 }
1420
1421
1422                 /*******************************/
1423                 /// <summary>
1424                 /// This class manages a set of elements.
1425                 /// </summary>
1426                 public class SetSupport : System.Collections.ArrayList
1427                 {
1428                         /// <summary>
1429                         /// Creates a new set.
1430                         /// </summary>
1431                         public SetSupport(): base()
1432                         {           
1433                         }
1434
1435                         /// <summary>
1436                         /// Creates a new set initialized with System.Collections.ICollection object
1437                         /// </summary>
1438                         /// <param name="collection">System.Collections.ICollection object to initialize the set object</param>
1439                         public SetSupport(System.Collections.ICollection collection): base(collection)
1440                         {           
1441                         }
1442
1443                         /// <summary>
1444                         /// Creates a new set initialized with a specific capacity.
1445                         /// </summary>
1446                         /// <param name="capacity">value to set the capacity of the set object</param>
1447                         public SetSupport(int capacity): base(capacity)
1448                         {           
1449                         }
1450          
1451                         /// <summary>
1452                         /// Adds an element to the set.
1453                         /// </summary>
1454                         /// <param name="objectToAdd">The object to be added.</param>
1455                         /// <returns>True if the object was added, false otherwise.</returns>
1456                         public new virtual bool Add(object objectToAdd)
1457                         {
1458                                 if (this.Contains(objectToAdd))
1459                                         return false;
1460                                 else
1461                                 {
1462                                         base.Add(objectToAdd);
1463                                         return true;
1464                                 }
1465                         }
1466          
1467                         /// <summary>
1468                         /// Adds all the elements contained in the specified collection.
1469                         /// </summary>
1470                         /// <param name="collection">The collection used to extract the elements that will be added.</param>
1471                         /// <returns>Returns true if all the elements were successfuly added. Otherwise returns false.</returns>
1472                         public virtual bool AddAll(System.Collections.ICollection collection)
1473                         {
1474                                 bool result = false;
1475                                 if (collection!=null)
1476                                 {
1477                                         System.Collections.IEnumerator tempEnumerator = new System.Collections.ArrayList(collection).GetEnumerator();
1478                                         while (tempEnumerator.MoveNext())
1479                                         {
1480                                                 if (tempEnumerator.Current != null)
1481                                                         result = this.Add(tempEnumerator.Current);
1482                                         }
1483                                 }
1484                                 return result;
1485                         }
1486                 
1487                         /// <summary>
1488                         /// Adds all the elements contained in the specified support class collection.
1489                         /// </summary>
1490                         /// <param name="collection">The collection used to extract the elements that will be added.</param>
1491                         /// <returns>Returns true if all the elements were successfuly added. Otherwise returns false.</returns>
1492                         public virtual bool AddAll(CollectionSupport collection)
1493                         {
1494                                 return this.AddAll((System.Collections.ICollection)collection);
1495                         }
1496          
1497                         /// <summary>
1498                         /// Verifies that all the elements of the specified collection are contained into the current collection. 
1499                         /// </summary>
1500                         /// <param name="collection">The collection used to extract the elements that will be verified.</param>
1501                         /// <returns>True if the collection contains all the given elements.</returns>
1502                         public virtual bool ContainsAll(System.Collections.ICollection collection)
1503                         {
1504                                 bool result = false;
1505                                 System.Collections.IEnumerator tempEnumerator = collection.GetEnumerator();
1506                                 while (tempEnumerator.MoveNext())
1507                                         if (!(result = this.Contains(tempEnumerator.Current)))
1508                                                 break;
1509                                 return result;
1510                         }
1511                 
1512                         /// <summary>
1513                         /// Verifies if all the elements of the specified collection are contained into the current collection.
1514                         /// </summary>
1515                         /// <param name="collection">The collection used to extract the elements that will be verified.</param>
1516                         /// <returns>Returns true if all the elements are contained in the collection. Otherwise returns false.</returns>
1517                         public virtual bool ContainsAll(CollectionSupport collection)
1518                         {
1519                                 return this.ContainsAll((System.Collections.ICollection) collection);
1520                         }               
1521          
1522                         /// <summary>
1523                         /// Verifies if the collection is empty.
1524                         /// </summary>
1525                         /// <returns>True if the collection is empty, false otherwise.</returns>
1526                         public virtual bool IsEmpty()
1527                         {
1528                                 return (this.Count == 0);
1529                         }
1530                  
1531                         /// <summary>
1532                         /// Removes an element from the set.
1533                         /// </summary>
1534                         /// <param name="elementToRemove">The element to be removed.</param>
1535                         /// <returns>True if the element was removed.</returns>
1536                         public new virtual bool Remove(object elementToRemove)
1537                         {
1538                                 bool result = false;
1539                                 if (this.Contains(elementToRemove))
1540                                         result = true;
1541                                 base.Remove(elementToRemove);
1542                                 return result;
1543                         }
1544                 
1545                         /// <summary>
1546                         /// Removes all the elements contained in the specified collection.
1547                         /// </summary>
1548                         /// <param name="collection">The collection used to extract the elements that will be removed.</param>
1549                         /// <returns>True if all the elements were successfuly removed, false otherwise.</returns>
1550                         public virtual bool RemoveAll(System.Collections.ICollection collection)
1551                         { 
1552                                 bool result = false;
1553                                 System.Collections.IEnumerator tempEnumerator = collection.GetEnumerator();
1554                                 while (tempEnumerator.MoveNext())
1555                                 {
1556                                         if ((result == false) && (this.Contains(tempEnumerator.Current)))
1557                                                 result = true;
1558                                         this.Remove(tempEnumerator.Current);
1559                                 }
1560                                 return result;
1561                         }
1562                 
1563                         /// <summary>
1564                         /// Removes all the elements contained into the specified collection.
1565                         /// </summary>
1566                         /// <param name="collection">The collection used to extract the elements that will be removed.</param>
1567                         /// <returns>Returns true if all the elements were successfuly removed. Otherwise returns false.</returns>
1568                         public virtual bool RemoveAll(CollectionSupport collection)
1569                         { 
1570                                 return this.RemoveAll((System.Collections.ICollection) collection);
1571                         }               
1572
1573                         /// <summary>
1574                         /// Removes all the elements that aren't contained in the specified collection.
1575                         /// </summary>
1576                         /// <param name="collection">The collection used to verify the elements that will be retained.</param>
1577                         /// <returns>True if all the elements were successfully removed, false otherwise.</returns>
1578                         public virtual bool RetainAll(System.Collections.ICollection collection)
1579                         {
1580                                 bool result = false;
1581                                 System.Collections.IEnumerator tempEnumerator = collection.GetEnumerator();
1582                                 SetSupport tempSet = (SetSupport)collection;
1583                                 while (tempEnumerator.MoveNext())
1584                                         if (!tempSet.Contains(tempEnumerator.Current))
1585                                         {
1586                                                 result = this.Remove(tempEnumerator.Current);
1587                                                 tempEnumerator = this.GetEnumerator();
1588                                         }
1589                                 return result;
1590                         }
1591                 
1592                         /// <summary>
1593                         /// Removes all the elements that aren't contained into the specified collection.
1594                         /// </summary>
1595                         /// <param name="collection">The collection used to verify the elements that will be retained.</param>
1596                         /// <returns>Returns true if all the elements were successfully removed. Otherwise returns false.</returns>
1597                         public virtual bool RetainAll(CollectionSupport collection)
1598                         {
1599                                 return this.RetainAll((System.Collections.ICollection) collection);
1600                         }               
1601          
1602                         /// <summary>
1603                         /// Obtains an array containing all the elements of the collection.
1604                         /// </summary>
1605                         /// <returns>The array containing all the elements of the collection.</returns>
1606                         public new virtual object[] ToArray()
1607                         {
1608                                 int index = 0;
1609                                 object[] tempObject= new object[this.Count];
1610                                 System.Collections.IEnumerator tempEnumerator = this.GetEnumerator();
1611                                 while (tempEnumerator.MoveNext())
1612                                         tempObject[index++] = tempEnumerator.Current;
1613                                 return tempObject;
1614                         }
1615
1616                         /// <summary>
1617                         /// Obtains an array containing all the elements in the collection.
1618                         /// </summary>
1619                         /// <param name="objects">The array into which the elements of the collection will be stored.</param>
1620                         /// <returns>The array containing all the elements of the collection.</returns>
1621                         public virtual object[] ToArray(object[] objects)
1622                         {
1623                                 int index = 0;
1624                                 System.Collections.IEnumerator tempEnumerator = this.GetEnumerator();
1625                                 while (tempEnumerator.MoveNext())
1626                                         objects[index++] = tempEnumerator.Current;
1627                                 return objects;
1628                         }
1629                 }
1630                 /*******************************/
1631                 /// <summary>
1632                 /// This class manages different operation with collections.
1633                 /// </summary>
1634                 public class AbstractSetSupport : SetSupport
1635                 {
1636                         /// <summary>
1637                         /// The constructor with no parameters to create an abstract set.
1638                         /// </summary>
1639                         public AbstractSetSupport()
1640                         {
1641                         }
1642                 }
1643
1644
1645                 /*******************************/
1646                 /// <summary>
1647                 /// Removes the element with the specified key from a Hashtable instance.
1648                 /// </summary>
1649                 /// <param name="hashtable">The Hashtable instance</param>
1650                 /// <param name="key">The key of the element to remove</param>
1651                 /// <returns>The element removed</returns>  
1652                 public static System.Object HashtableRemove(System.Collections.Hashtable hashtable, System.Object key)
1653                 {
1654                         System.Object element = hashtable[key];
1655                         hashtable.Remove(key);
1656                         return element;
1657                 }
1658
1659                 /*******************************/
1660                 /// <summary>
1661                 /// Sets the size of the ArrayList. If the new size is greater than the current capacity, then new null items are added to the end of the ArrayList. If the new size is lower than the current size, then all elements after the new size are discarded
1662                 /// </summary>
1663                 /// <param name="arrayList">The ArrayList to be changed</param>
1664                 /// <param name="newSize">The new ArrayList size</param>
1665                 public static void SetSize(System.Collections.ArrayList arrayList, int newSize)
1666                 {
1667                         if (newSize < 0) throw new System.ArgumentException();
1668                         else
1669                         {
1670                                 if (newSize < arrayList.Count)
1671                                         arrayList.RemoveRange(newSize,(arrayList.Count-newSize));
1672                                 else
1673                                         while(newSize > arrayList.Count)
1674                                                 arrayList.Add(null);
1675                         }
1676                 }
1677
1678                 /*******************************/
1679                 /// <summary>
1680                 /// Adds an element to the top end of a Stack instance.
1681                 /// </summary>
1682                 /// <param name="stack">The Stack instance</param>
1683                 /// <param name="element">The element to add</param>
1684                 /// <returns>The element added</returns>  
1685                 public static System.Object StackPush(System.Collections.Stack stack, System.Object element)
1686                 {
1687                         stack.Push(element);
1688                         return element;
1689                 }
1690
1691                 /*******************************/
1692                 /// <summary>
1693                 /// Copies an array of chars obtained from a String into a specified array of chars
1694                 /// </summary>
1695                 /// <param name="sourceString">The String to get the chars from</param>
1696                 /// <param name="sourceStart">Position of the String to start getting the chars</param>
1697                 /// <param name="sourceEnd">Position of the String to end getting the chars</param>
1698                 /// <param name="destinationArray">Array to return the chars</param>
1699                 /// <param name="destinationStart">Position of the destination array of chars to start storing the chars</param>
1700                 /// <returns>An array of chars</returns>
1701                 public static void GetCharsFromString(string sourceString, int sourceStart, int sourceEnd, ref char[] destinationArray, int destinationStart)
1702                 {       
1703                         int sourceCounter;
1704                         int destinationCounter;
1705                         sourceCounter = sourceStart;
1706                         destinationCounter = destinationStart;
1707                         while (sourceCounter < sourceEnd)
1708                         {
1709                                 destinationArray[destinationCounter] = (char) sourceString[sourceCounter];
1710                                 sourceCounter++;
1711                                 destinationCounter++;
1712                         }
1713                 }
1714
1715                 /*******************************/
1716                 /// <summary>
1717                 /// Creates an output file stream to write to the file with the specified name.
1718                 /// </summary>
1719                 /// <param name="FileName">Name of the file to write.</param>
1720                 /// <param name="Append">True in order to write to the end of the file, false otherwise.</param>
1721                 /// <returns>New instance of FileStream with the proper file mode.</returns>
1722                 public static System.IO.FileStream GetFileStream(string FileName, bool Append)
1723                 {
1724                         if (Append)
1725                                 return new System.IO.FileStream(FileName, System.IO.FileMode.Append);
1726                         else
1727                                 return new System.IO.FileStream(FileName, System.IO.FileMode.Create);
1728                 }
1729
1730
1731                 /*******************************/
1732                 /// <summary>
1733                 /// Converts an array of sbytes to an array of chars
1734                 /// </summary>
1735                 /// <param name="sByteArray">The array of sbytes to convert</param>
1736                 /// <returns>The new array of chars</returns>
1737                 [CLSCompliantAttribute(false)]
1738                 public static char[] ToCharArray(sbyte[] sByteArray) 
1739                 {
1740                         char[] charArray = new char[sByteArray.Length];    
1741                         sByteArray.CopyTo(charArray, 0);
1742                         return charArray;
1743                 }
1744
1745                 /// <summary>
1746                 /// Converts an array of bytes to an array of chars
1747                 /// </summary>
1748                 /// <param name="byteArray">The array of bytes to convert</param>
1749                 /// <returns>The new array of chars</returns>
1750                 public static char[] ToCharArray(byte[] byteArray) 
1751                 {
1752                         char[] charArray = new char[byteArray.Length];     
1753                         byteArray.CopyTo(charArray, 0);
1754                         return charArray;
1755                 }
1756
1757                 /*******************************/
1758                 /// <summary>
1759                 /// Encapsulates the functionality of message digest algorithms such as SHA-1 or MD5.
1760                 /// </summary>
1761                 public class MessageDigestSupport
1762                 {
1763                         private System.Security.Cryptography.HashAlgorithm algorithm;
1764                         private byte[] data;
1765                         private int position;
1766                         private string algorithmName;
1767
1768                         /// <summary>
1769                         /// The HashAlgorithm instance that provide the cryptographic hash algorithm
1770                         /// </summary>
1771                         public System.Security.Cryptography.HashAlgorithm Algorithm
1772                         {
1773                                 get
1774                                 {
1775                                         return this.algorithm;
1776                                 }
1777                                 set
1778                                 {
1779                                         this.algorithm  = value;
1780                                 }
1781                         }
1782
1783                         /// <summary>
1784                         /// The digest data
1785                         /// </summary>
1786                         public byte[] Data
1787                         {
1788                                 get
1789                                 {
1790                                         return this.data;
1791                                 }
1792                                 set
1793                                 {
1794                                         this.data  = value;
1795                                 }
1796                         }
1797
1798                         /// <summary>
1799                         /// The name of the cryptographic hash algorithm used in the instance
1800                         /// </summary>
1801                         public string AlgorithmName
1802                         {
1803                                 get
1804                                 {
1805                                         return this.algorithmName;
1806                                 }
1807                         }
1808
1809                         /// <summary>
1810                         /// Creates a message digest using the specified name to set Algorithm property.
1811                         /// </summary>
1812                         /// <param name="algorithm">The name of the algorithm to use</param>
1813                         public MessageDigestSupport(System.String algorithm)
1814                         {                       
1815                                 if (algorithm.Equals("SHA-1"))
1816                                 {
1817                                         this.algorithmName = "SHA";
1818                                 }
1819                                 else 
1820                                 {
1821                                         this.algorithmName = algorithm;
1822                                 }
1823                                 this.Algorithm = (System.Security.Cryptography.HashAlgorithm) System.Security.Cryptography.CryptoConfig.CreateFromName(this.algorithmName);                     
1824                                 this.position  = 0;
1825                         }
1826
1827                         /// <summary>
1828                         /// Computes the hash value for the internal data digest.
1829                         /// </summary>
1830                         /// <returns>The array of signed bytes with the resulting hash value</returns>
1831                         [CLSCompliantAttribute(false)]
1832                         public sbyte[] DigestData()
1833                         {
1834                                 sbyte[] result = ToSByteArray(this.Algorithm.ComputeHash(this.data));
1835                                 this.Reset();
1836                                 return result;
1837                         }
1838
1839                         /// <summary>
1840                         /// Performs and update on the digest with the specified array and then completes the digest
1841                         /// computation.
1842                         /// </summary>
1843                         /// <param name="newData">The array of bytes for final update to the digest</param>
1844                         /// <returns>An array of signed bytes with the resulting hash value</returns>
1845                         [CLSCompliantAttribute(false)]
1846                         public sbyte[] DigestData(byte[] newData)
1847                         {
1848                                 this.Update(newData);
1849                                 return this.DigestData();
1850                         }
1851
1852                         /// <summary>
1853                         /// Updates the digest data with the specified array of bytes by making an append
1854                         /// operation in the internal array of data.
1855                         /// </summary>
1856                         /// <param name="newData">The array of bytes for the update operation</param>
1857                         public void Update(byte[] newData)
1858                         {
1859                                 if (position == 0)
1860                                 {
1861                                         this.Data = newData;
1862                                         this.position = this.Data.Length - 1;
1863                                 }
1864                                 else
1865                                 {
1866                                         byte[] oldData = this.Data;
1867                                         this.Data = new byte[newData.Length + position + 1];
1868                                         oldData.CopyTo(this.Data, 0);
1869                                         newData.CopyTo(this.Data, oldData.Length);
1870                     
1871                                         this.position = this.Data.Length - 1;
1872                                 }
1873                         }
1874         
1875                         /// <summary>
1876                         /// Updates the digest data with the input byte by calling the method Update with an array.
1877                         /// </summary>
1878                         /// <param name="newData">The input byte for the update</param>
1879                         public void Update(byte newData)
1880                         {
1881                                 byte[] newDataArray = new byte[1];
1882                                 newDataArray[0] = newData;
1883                                 this.Update(newDataArray);
1884                         }
1885
1886                         /// <summary>
1887                         /// Updates the specified count of bytes with the input array of bytes starting at the
1888                         /// input offset.
1889                         /// </summary>
1890                         /// <param name="newData">The array of bytes for the update operation</param>
1891                         /// <param name="offset">The initial position to start from in the array of bytes</param>
1892                         /// <param name="count">The number of bytes fot the update</param>
1893                         public void Update(byte[] newData, int offset, int count)
1894                         {
1895                                 byte[] newDataArray = new byte[count];
1896                                 System.Array.Copy(newData, offset, newDataArray, 0, count);
1897                                 this.Update(newDataArray);
1898                         }
1899                 
1900                         /// <summary>
1901                         /// Resets the digest data to the initial state.
1902                         /// </summary>
1903                         public void Reset()
1904                         {
1905                                 this.data = null;
1906                                 this.position = 0;
1907                         }
1908
1909                         /// <summary>
1910                         /// Returns a string representation of the Message Digest
1911                         /// </summary>
1912                         /// <returns>A string representation of the object</returns>
1913                         public override string ToString()
1914                         {
1915                                 return this.Algorithm.ToString();
1916                         }
1917
1918                         /// <summary>
1919                         /// Generates a new instance of the MessageDigestSupport class using the specified algorithm
1920                         /// </summary>
1921                         /// <param name="algorithm">The name of the algorithm to use</param>
1922                         /// <returns>A new instance of the MessageDigestSupport class</returns>
1923                         public static MessageDigestSupport GetInstance(System.String algorithm)
1924                         {
1925                                 return new MessageDigestSupport(algorithm);
1926                         }
1927                 
1928                         /// <summary>
1929                         /// Compares two arrays of signed bytes evaluating equivalence in digest data
1930                         /// </summary>
1931                         /// <param name="firstDigest">An array of signed bytes for comparison</param>
1932                         /// <param name="secondDigest">An array of signed bytes for comparison</param>
1933                         /// <returns>True if the input digest arrays are equal</returns>
1934                         [CLSCompliantAttribute(false)]
1935                         public static bool EquivalentDigest(System.SByte[] firstDigest, System.SByte[] secondDigest)
1936                         {
1937                                 bool result = false;
1938                                 if (firstDigest.Length == secondDigest.Length)
1939                                 {
1940                                         int index = 0;
1941                                         result = true;
1942                                         while(result && index < firstDigest.Length)
1943                                         {
1944                                                 result = firstDigest[index] == secondDigest[index];
1945                                                 index++;
1946                                         }
1947                                 }
1948                         
1949                                 return result;
1950                         }
1951                 }
1952
1953                 /*******************************/
1954                 /// <summary>
1955                 /// This class uses a cryptographic Random Number Generator to provide support for
1956                 /// strong pseudo-random number generation.
1957                 /// </summary>
1958                 public class SecureRandomSupport
1959                 {
1960                         private System.Security.Cryptography.RNGCryptoServiceProvider generator;
1961
1962                         /// <summary>
1963                         /// Initializes a new instance of the random number generator.
1964                         /// </summary>
1965                         public SecureRandomSupport()
1966                         {
1967                                 this.generator = new System.Security.Cryptography.RNGCryptoServiceProvider();
1968                         }
1969
1970                         /// <summary>
1971                         /// Initializes a new instance of the random number generator with the given seed.
1972                         /// </summary>
1973                         /// <param name="seed">The initial seed for the generator</param>
1974                         public SecureRandomSupport(byte[] seed)
1975                         {
1976                                 this.generator = new System.Security.Cryptography.RNGCryptoServiceProvider(seed);
1977                         }
1978
1979                         /// <summary>
1980                         /// Returns an array of bytes with a sequence of cryptographically strong random values
1981                         /// </summary>
1982                         /// <param name="randomnumbersarray">The array of bytes to fill</param>
1983                         [CLSCompliantAttribute(false)]
1984                         public sbyte[] NextBytes(byte[] randomnumbersarray)
1985                         {                       
1986                                 this.generator.GetBytes(randomnumbersarray);
1987                                 return ToSByteArray(randomnumbersarray);
1988                         }
1989
1990                         /// <summary>
1991                         /// Returns the given number of seed bytes generated for the first running of a new instance 
1992                         /// of the random number generator
1993                         /// </summary>
1994                         /// <param name="numberOfBytes">Number of seed bytes to generate</param>
1995                         /// <returns>Seed bytes generated</returns>
1996                         public static byte[] GetSeed(int numberOfBytes)
1997                         {
1998                                 System.Security.Cryptography.RNGCryptoServiceProvider generatedSeed = new System.Security.Cryptography.RNGCryptoServiceProvider();
1999                                 byte[] seeds = new byte[numberOfBytes];
2000                                 generatedSeed.GetBytes(seeds);
2001                                 return seeds;
2002                         }
2003
2004                         /// <summary>
2005                         /// Creates a new instance of the random number generator with the seed provided by the user
2006                         /// </summary>
2007                         /// <param name="newSeed">Seed to create a new random number generator</param>
2008                         public void SetSeed(byte[] newSeed)
2009                         {
2010                                 this.generator = new System.Security.Cryptography.RNGCryptoServiceProvider(newSeed);
2011                         }
2012
2013                         /// <summary>
2014                         /// Creates a new instance of the random number generator with the seed provided by the user
2015                         /// </summary>
2016                         /// <param name="newSeed">Seed to create a new random number generator</param>
2017                         public void SetSeed(long newSeed)
2018                         {
2019                                 byte[] bytes = new byte[8];
2020                                 for (int index= 7; index > 0 ; index--)
2021                                 {
2022                                         bytes[index] = (byte)(newSeed - (long)((newSeed >> 8) << 8));
2023                                         newSeed  = (long)(newSeed >> 8);
2024                                 }                       
2025                                 SetSeed(bytes);
2026                         }
2027                 }
2028
2029                 /*******************************/
2030                 /// <summary>
2031                 /// Interface used by classes which must be single threaded.
2032                 /// </summary>
2033                 public interface SingleThreadModel
2034                 {
2035                 }
2036
2037
2038                 /*******************************/
2039                 /// <summary>
2040                 /// Creates an instance of a received Type.
2041                 /// </summary>
2042                 /// <param name="classType">The Type of the new class instance to return.</param>
2043                 /// <returns>An Object containing the new instance.</returns>
2044                 public static System.Object CreateNewInstance(System.Type classType)
2045                 {
2046                         System.Object instance = null;
2047                         System.Type[] constructor = new System.Type[]{};
2048                         System.Reflection.ConstructorInfo[] constructors = null;
2049        
2050                         constructors = classType.GetConstructors();
2051
2052                         if (constructors.Length == 0)
2053                                 throw new System.UnauthorizedAccessException();
2054                         else
2055                         {
2056                                 for(int i = 0; i < constructors.Length; i++)
2057                                 {
2058                                         System.Reflection.ParameterInfo[] parameters = constructors[i].GetParameters();
2059
2060                                         if (parameters.Length == 0)
2061                                         {
2062                                                 instance = classType.GetConstructor(constructor).Invoke(new System.Object[]{});
2063                                                 break;
2064                                         }
2065                                         else if (i == constructors.Length -1)     
2066                                                 throw new System.MethodAccessException();
2067                                 }                       
2068                         }
2069                         return instance;
2070                 }
2071
2072
2073                 /*******************************/
2074                 /// <summary>
2075                 /// Writes the exception stack trace to the received stream
2076                 /// </summary>
2077                 /// <param name="throwable">Exception to obtain information from</param>
2078                 /// <param name="stream">Output sream used to write to</param>
2079                 public static void WriteStackTrace(System.Exception throwable, System.IO.TextWriter stream)
2080                 {
2081                         stream.Write(throwable.StackTrace);
2082                         stream.Flush();
2083                 }
2084
2085                 /*******************************/
2086                 /// <summary>
2087                 /// Determines whether two Collections instances are equals.
2088                 /// </summary>
2089                 /// <param name="source">The first Collections to compare. </param>
2090                 /// <param name="target">The second Collections to compare. </param>
2091                 /// <returns>Return true if the first collection is the same instance as the second collection, otherwise return false.</returns>
2092                 public static bool EqualsSupport(System.Collections.ICollection source, System.Collections.ICollection target )
2093                 {
2094                         System.Collections.IEnumerator sourceEnumerator = ReverseStack(source);
2095                         System.Collections.IEnumerator targetEnumerator = ReverseStack(target);
2096      
2097                         if (source.Count != target.Count)
2098                                 return false;
2099                         while(sourceEnumerator.MoveNext() && targetEnumerator.MoveNext())
2100                                 if (!sourceEnumerator.Current.Equals(targetEnumerator.Current))
2101                                         return false;
2102                         return true;
2103                 }
2104         
2105                 /// <summary>
2106                 /// Determines if a Collection is equal to the Object.
2107                 /// </summary>
2108                 /// <param name="source">The first Collections to compare.</param>
2109                 /// <param name="target">The Object to compare.</param>
2110                 /// <returns>Return true if the first collection contains the same values of the second Object, otherwise return false.</returns>
2111                 public static bool EqualsSupport(System.Collections.ICollection source, System.Object target)
2112                 {
2113                         if((target.GetType())!= (typeof(System.Collections.ICollection)))
2114                                 return false;
2115                         else
2116                                 return EqualsSupport(source,(System.Collections.ICollection)target);
2117                 }
2118
2119                 /// <summary>
2120                 /// Determines if a IDictionaryEnumerator is equal to the Object.
2121                 /// </summary>
2122                 /// <param name="source">The first IDictionaryEnumerator to compare.</param>
2123                 /// <param name="target">The second Object to compare.</param>
2124                 /// <returns>Return true if the first IDictionaryEnumerator contains the same values of the second Object, otherwise return false.</returns>
2125                 public static bool EqualsSupport(System.Collections.IDictionaryEnumerator source, System.Object target)
2126                 {
2127                         if((target.GetType())!= (typeof(System.Collections.IDictionaryEnumerator)))
2128                                 return false;
2129                         else
2130                                 return EqualsSupport(source,(System.Collections.IDictionaryEnumerator)target);
2131                 }
2132
2133                 /// <summary>
2134                 /// Determines whether two IDictionaryEnumerator instances are equals.
2135                 /// </summary>
2136                 /// <param name="source">The first IDictionaryEnumerator to compare.</param>
2137                 /// <param name="target">The second IDictionaryEnumerator to compare.</param>
2138                 /// <returns>Return true if the first IDictionaryEnumerator contains the same values as the second IDictionaryEnumerator, otherwise return false.</returns>
2139                 public static bool EqualsSupport(System.Collections.IDictionaryEnumerator source, System.Collections.IDictionaryEnumerator target )
2140                 {
2141                         while(source.MoveNext() && target.MoveNext())
2142                                 if (source.Key.Equals(target.Key))
2143                                         if(source.Value.Equals(target.Value))
2144                                                 return true;
2145                         return false;
2146                 }
2147
2148                 /// <summary>
2149                 /// Reverses the Stack Collection received.
2150                 /// </summary>
2151                 /// <param name="collection">The collection to reverse.</param>
2152                 /// <returns>The collection received in reverse order if it was a System.Collections.Stack type, otherwise it does 
2153                 /// nothing to the collection.</returns>
2154                 public static System.Collections.IEnumerator ReverseStack(System.Collections.ICollection collection)
2155                 {
2156                         if((collection.GetType()) == (typeof(System.Collections.Stack)))
2157                         {
2158                                 System.Collections.ArrayList collectionStack = new System.Collections.ArrayList(collection);
2159                                 collectionStack.Reverse();
2160                                 return collectionStack.GetEnumerator();
2161                         }
2162                         else
2163                                 return collection.GetEnumerator();
2164                 }
2165
2166         }