PageRenderTime 59ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/mcs/class/Novell.Directory.Ldap/Novell.Directory.Ldap/SupportClass.cs

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