/mcs/class/Novell.Directory.Ldap/Novell.Directory.Ldap/SupportClass.cs
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
Large files files are truncated, but you can click here to view the full file
- /******************************************************************************
- * The MIT License
- * Copyright (c) 2003 Novell Inc. www.novell.com
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the Software), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- *******************************************************************************/
- //
- // Novell.Directory.Ldap.SupportClass.cs
- //
- // Author:
- // Sunil Kumar (Sunilk@novell.com)
- //
- // (C) 2003 Novell, Inc (http://www.novell.com)
- //
- // Support classes replicate the functionality of the original code, but in some cases they are
- // substantially different architecturally. Although every effort is made to preserve the
- // original architecture of the application in the converted project, the user should be aware that
- // the primary goal of these support classes is to replicate functionality, and that at times
- // the architecture of the resulting solution may differ somewhat.
- //
- using System;
- /// <summary>
- /// This interface should be implemented by any class whose instances are intended
- /// to be executed by a thread.
- /// </summary>
- public interface IThreadRunnable
- {
- /// <summary>
- /// This method has to be implemented in order that starting of the thread causes the object's
- /// run method to be called in that separately executing thread.
- /// </summary>
- void Run();
- }
- public class Integer32 : System.Object
- {
- private System.Int32 _wintv;
- public Integer32(System.Int32 ival)
- {
- _wintv=ival;
- }
-
- public System.Int32 intValue
- {
- get
- {
- return _wintv;
- }
- set
- {
- _wintv=value;
- }
- }
- }
-
- /// <summary>
- /// Contains conversion support elements such as classes, interfaces and static methods.
- /// </summary>
- public class SupportClass
- {
- /// <summary>
- /// Receives a byte array and returns it transformed in an sbyte array
- /// </summary>
- /// <param name="byteArray">Byte array to process</param>
- /// <returns>The transformed array</returns>
- [CLSCompliantAttribute(false)]
- public static sbyte[] ToSByteArray(byte[] byteArray)
- {
- #if TARGET_JVM
- return vmw.common.TypeUtils.ToSByteArray(byteArray);
- #else
- sbyte[] sbyteArray = new sbyte[byteArray.Length];
- for(int index=0; index < byteArray.Length; index++)
- sbyteArray[index] = (sbyte) byteArray[index];
- return sbyteArray;
- #endif
- }
- /*******************************/
- /// <summary>
- /// Converts an array of sbytes to an array of bytes
- /// </summary>
- /// <param name="sbyteArray">The array of sbytes to be converted</param>
- /// <returns>The new array of bytes</returns>
- [CLSCompliantAttribute(false)]
- public static byte[] ToByteArray(sbyte[] sbyteArray)
- {
- #if TARGET_JVM
- return (byte[])vmw.common.TypeUtils.ToByteArray(sbyteArray);;
- #else
- byte[] byteArray = new byte[sbyteArray.Length];
- for(int index=0; index < sbyteArray.Length; index++)
- byteArray[index] = (byte) sbyteArray[index];
- return byteArray;
- #endif
- }
- /// <summary>
- /// Converts a string to an array of bytes
- /// </summary>
- /// <param name="sourceString">The string to be converted</param>
- /// <returns>The new array of bytes</returns>
- public static byte[] ToByteArray(string sourceString)
- {
- byte[] byteArray = new byte[sourceString.Length];
- for (int index=0; index < sourceString.Length; index++)
- byteArray[index] = (byte) sourceString[index];
- return byteArray;
- }
- /// <summary>
- /// Converts a array of object-type instances to a byte-type array.
- /// </summary>
- /// <param name="tempObjectArray">Array to convert.</param>
- /// <returns>An array of byte type elements.</returns>
- public static byte[] ToByteArray(object[] tempObjectArray)
- {
- byte[] byteArray = new byte[tempObjectArray.Length];
- for (int index = 0; index < tempObjectArray.Length; index++)
- byteArray[index] = (byte)tempObjectArray[index];
- return byteArray;
- }
- /*******************************/
- /// <summary>Reads a number of characters from the current source Stream and writes the data to the target array at the specified index.</summary>
- /// <param name="sourceStream">The source Stream to read from.</param>
- /// <param name="target">Contains the array of characteres read from the source Stream.</param>
- /// <param name="start">The starting index of the target array.</param>
- /// <param name="count">The maximum number of characters to read from the source Stream.</param>
- /// <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>
- [CLSCompliantAttribute(false)]
- public static System.Int32 ReadInput(System.IO.Stream sourceStream, ref sbyte[] target, int start, int count)
- {
- // Returns 0 bytes if not enough space in target
- if (target.Length == 0)
- return 0;
- byte[] receiver = new byte[target.Length];
- int bytesRead=0;
- int startIndex=start;
- int bytesToRead=count;
- while( bytesToRead > 0 ) {
- int n= sourceStream.Read(receiver, startIndex, bytesToRead);
- if (n==0)
- break;
- bytesRead+=n;
- startIndex+=n;
- bytesToRead-=n;
- }
- // Returns -1 if EOF
- if (bytesRead == 0)
- return -1;
-
- for(int i = start; i < start + bytesRead; i++)
- target[i] = (sbyte)receiver[i];
-
- return bytesRead;
- }
- /// <summary>Reads a number of characters from the current source TextReader and writes the data to the target array at the specified index.</summary>
- /// <param name="sourceTextReader">The source TextReader to read from</param>
- /// <param name="target">Contains the array of characteres read from the source TextReader.</param>
- /// <param name="start">The starting index of the target array.</param>
- /// <param name="count">The maximum number of characters to read from the source TextReader.</param>
- /// <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>
- [CLSCompliantAttribute(false)]
- public static System.Int32 ReadInput(System.IO.TextReader sourceTextReader, ref sbyte[] target, int start, int count)
- {
- // Returns 0 bytes if not enough space in target
- if (target.Length == 0) return 0;
- char[] charArray = new char[target.Length];
- int bytesRead = sourceTextReader.Read(charArray, start, count);
- // Returns -1 if EOF
- if (bytesRead == 0) return -1;
- for(int index=start; index<start+bytesRead; index++)
- target[index] = (sbyte)charArray[index];
- return bytesRead;
- }
- /*******************************/
- /// <summary>
- /// This method returns the literal value received
- /// </summary>
- /// <param name="literal">The literal to return</param>
- /// <returns>The received value</returns>
- public static long Identity(long literal)
- {
- return literal;
- }
- /// <summary>
- /// This method returns the literal value received
- /// </summary>
- /// <param name="literal">The literal to return</param>
- /// <returns>The received value</returns>
- [CLSCompliantAttribute(false)]
- public static ulong Identity(ulong literal)
- {
- return literal;
- }
- /// <summary>
- /// This method returns the literal value received
- /// </summary>
- /// <param name="literal">The literal to return</param>
- /// <returns>The received value</returns>
- public static float Identity(float literal)
- {
- return literal;
- }
- /// <summary>
- /// This method returns the literal value received
- /// </summary>
- /// <param name="literal">The literal to return</param>
- /// <returns>The received value</returns>
- public static double Identity(double literal)
- {
- return literal;
- }
- /*******************************/
- /// <summary>
- /// The class performs token processing from strings
- /// </summary>
- public class Tokenizer
- {
- //Element list identified
- private System.Collections.ArrayList elements;
- //Source string to use
- private string source;
- //The tokenizer uses the default delimiter set: the space character, the tab character, the newline character, and the carriage-return character
- private string delimiters = " \t\n\r";
- private bool returnDelims=false;
- /// <summary>
- /// Initializes a new class instance with a specified string to process
- /// </summary>
- /// <param name="source">String to tokenize</param>
- public Tokenizer(string source)
- {
- this.elements = new System.Collections.ArrayList();
- this.elements.AddRange(source.Split(this.delimiters.ToCharArray()));
- this.RemoveEmptyStrings();
- this.source = source;
- }
- /// <summary>
- /// Initializes a new class instance with a specified string to process
- /// and the specified token delimiters to use
- /// </summary>
- /// <param name="source">String to tokenize</param>
- /// <param name="delimiters">String containing the delimiters</param>
- public Tokenizer(string source, string delimiters)
- {
- this.elements = new System.Collections.ArrayList();
- this.delimiters = delimiters;
- this.elements.AddRange(source.Split(this.delimiters.ToCharArray()));
- this.RemoveEmptyStrings();
- this.source = source;
- }
- public Tokenizer(string source, string delimiters,bool retDel)
- {
- this.elements = new System.Collections.ArrayList();
- this.delimiters = delimiters;
- this.source = source;
- this.returnDelims = retDel;
- if( returnDelims)
- Tokenize();
- else
- this.elements.AddRange(source.Split(this.delimiters.ToCharArray()));
- this.RemoveEmptyStrings();
- }
-
- private void Tokenize()
- {
- string tempstr = this.source;
- string toks = "";
- if (tempstr.IndexOfAny(this.delimiters.ToCharArray()) < 0 && tempstr.Length > 0)
- {
- this.elements.Add(tempstr);
- }
- else if (tempstr.IndexOfAny(this.delimiters.ToCharArray()) < 0 && tempstr.Length <= 0)
- {
- return;
- }
- while (tempstr.IndexOfAny(this.delimiters.ToCharArray()) >= 0)
- {
- if(tempstr.IndexOfAny(this.delimiters.ToCharArray()) == 0)
- {
- if (tempstr.Length > 1 )
- {
- this.elements.Add(tempstr.Substring(0,1));
- tempstr=tempstr.Substring(1);
- }
- else
- tempstr = "";
- }
- else
- {
- toks = tempstr.Substring(0,tempstr.IndexOfAny(this.delimiters.ToCharArray()));
- this.elements.Add(toks);
- this.elements.Add(tempstr.Substring(toks.Length,1));
- if ( tempstr.Length > (toks.Length + 1))
- {
- tempstr = tempstr.Substring(toks.Length + 1);
- }
- else
- tempstr = "";
- }
- }
- if (tempstr.Length > 0)
- {
- this.elements.Add(tempstr);
- }
- }
-
- /// <summary>
- /// Current token count for the source string
- /// </summary>
- public int Count
- {
- get
- {
- return (this.elements.Count);
- }
- }
- /// <summary>
- /// Determines if there are more tokens to return from the source string
- /// </summary>
- /// <returns>True or false, depending if there are more tokens</returns>
- public bool HasMoreTokens()
- {
- return (this.elements.Count > 0);
- }
- /// <summary>
- /// Returns the next token from the token list
- /// </summary>
- /// <returns>The string value of the token</returns>
- public string NextToken()
- {
- string result;
- if (source == "") throw new System.Exception();
- else
- {
- if(returnDelims){
- // Tokenize();
- RemoveEmptyStrings();
- result = (string) this.elements[0];
- this.elements.RemoveAt(0);
- return result;
- }
- else
- {
- this.elements = new System.Collections.ArrayList();
- this.elements.AddRange(this.source.Split(delimiters.ToCharArray()));
- RemoveEmptyStrings();
- result = (string) this.elements[0];
- this.elements.RemoveAt(0);
- this.source = this.source.Remove(this.source.IndexOf(result),result.Length);
- this.source = this.source.TrimStart(this.delimiters.ToCharArray());
- return result;
- }
- }
- }
- /// <summary>
- /// Returns the next token from the source string, using the provided
- /// token delimiters
- /// </summary>
- /// <param name="delimiters">String containing the delimiters to use</param>
- /// <returns>The string value of the token</returns>
- public string NextToken(string delimiters)
- {
- this.delimiters = delimiters;
- return NextToken();
- }
- /// <summary>
- /// Removes all empty strings from the token list
- /// </summary>
- private void RemoveEmptyStrings()
- {
- for (int index=0; index < this.elements.Count; index++)
- if ((string)this.elements[index]== "")
- {
- this.elements.RemoveAt(index);
- index--;
- }
- }
- }
- /*******************************/
- /// <summary>
- /// Provides support for DateFormat
- /// </summary>
- public class DateTimeFormatManager
- {
- static public DateTimeFormatHashTable manager = new DateTimeFormatHashTable();
- /// <summary>
- /// Hashtable class to provide functionality for dateformat properties
- /// </summary>
- public class DateTimeFormatHashTable :System.Collections.Hashtable
- {
- /// <summary>
- /// Sets the format for datetime.
- /// </summary>
- /// <param name="format">DateTimeFormat instance to set the pattern</param>
- /// <param name="newPattern">A string with the pattern format</param>
- public void SetDateFormatPattern(System.Globalization.DateTimeFormatInfo format, System.String newPattern)
- {
- if (this[format] != null)
- ((DateTimeFormatProperties) this[format]).DateFormatPattern = newPattern;
- else
- {
- DateTimeFormatProperties tempProps = new DateTimeFormatProperties();
- tempProps.DateFormatPattern = newPattern;
- Add(format, tempProps);
- }
- }
- /// <summary>
- /// Gets the current format pattern of the DateTimeFormat instance
- /// </summary>
- /// <param name="format">The DateTimeFormat instance which the value will be obtained</param>
- /// <returns>The string representing the current datetimeformat pattern</returns>
- public string GetDateFormatPattern(System.Globalization.DateTimeFormatInfo format)
- {
- if (this[format] == null)
- return "d-MMM-yy";
- else
- return ((DateTimeFormatProperties) this[format]).DateFormatPattern;
- }
-
- /// <summary>
- /// Sets the datetimeformat pattern to the giving format
- /// </summary>
- /// <param name="format">The datetimeformat instance to set</param>
- /// <param name="newPattern">The new datetimeformat pattern</param>
- public void SetTimeFormatPattern(System.Globalization.DateTimeFormatInfo format, System.String newPattern)
- {
- if (this[format] != null)
- ((DateTimeFormatProperties) this[format]).TimeFormatPattern = newPattern;
- else
- {
- DateTimeFormatProperties tempProps = new DateTimeFormatProperties();
- tempProps.TimeFormatPattern = newPattern;
- Add(format, tempProps);
- }
- }
- /// <summary>
- /// Gets the current format pattern of the DateTimeFormat instance
- /// </summary>
- /// <param name="format">The DateTimeFormat instance which the value will be obtained</param>
- /// <returns>The string representing the current datetimeformat pattern</returns>
- public string GetTimeFormatPattern(System.Globalization.DateTimeFormatInfo format)
- {
- if (this[format] == null)
- return "h:mm:ss tt";
- else
- return ((DateTimeFormatProperties) this[format]).TimeFormatPattern;
- }
- /// <summary>
- /// Internal class to provides the DateFormat and TimeFormat pattern properties on .NET
- /// </summary>
- class DateTimeFormatProperties
- {
- public string DateFormatPattern = "d-MMM-yy";
- public string TimeFormatPattern = "h:mm:ss tt";
- }
- }
- }
- /*******************************/
- /// <summary>
- /// Gets the DateTimeFormat instance and date instance to obtain the date with the format passed
- /// </summary>
- /// <param name="format">The DateTimeFormat to obtain the time and date pattern</param>
- /// <param name="date">The date instance used to get the date</param>
- /// <returns>A string representing the date with the time and date patterns</returns>
- public static string FormatDateTime(System.Globalization.DateTimeFormatInfo format, System.DateTime date)
- {
- string timePattern = DateTimeFormatManager.manager.GetTimeFormatPattern(format);
- string datePattern = DateTimeFormatManager.manager.GetDateFormatPattern(format);
- return date.ToString(datePattern + " " + timePattern, format);
- }
- /*******************************/
- /// <summary>
- /// Adds a new key-and-value pair into the hash table
- /// </summary>
- /// <param name="collection">The collection to work with</param>
- /// <param name="key">Key used to obtain the value</param>
- /// <param name="newValue">Value asociated with the key</param>
- /// <returns>The old element associated with the key</returns>
- public static System.Object PutElement(System.Collections.IDictionary collection, System.Object key, System.Object newValue)
- {
- System.Object element = collection[key];
- collection[key] = newValue;
- return element;
- }
- /*******************************/
- /// <summary>
- /// This class contains static methods to manage arrays.
- /// </summary>
- public class ArrayListSupport
- {
- /// <summary>
- /// Obtains an array containing all the elements of the collection.
- /// </summary>
- /// <param name="collection">The collection from wich to obtain the elements.</param>
- /// <param name="objects">The array containing all the elements of the collection.</param>
- /// <returns>The array containing all the elements of the collection.</returns>
- public static System.Object[] ToArray(System.Collections.ArrayList collection, System.Object[] objects)
- {
- int index = 0;
- System.Collections.IEnumerator tempEnumerator = collection.GetEnumerator();
- while (tempEnumerator.MoveNext())
- objects[index++] = tempEnumerator.Current;
- return objects;
- }
- }
- /*******************************/
- /// <summary>
- /// Removes the first occurrence of an specific object from an ArrayList instance.
- /// </summary>
- /// <param name="arrayList">The ArrayList instance</param>
- /// <param name="element">The element to remove</param>
- /// <returns>True if item is found in the ArrayList; otherwise, false</returns>
- public static System.Boolean VectorRemoveElement(System.Collections.IList arrayList, System.Object element)
- {
- System.Boolean containsItem = arrayList.Contains(element);
- arrayList.Remove(element);
- return containsItem;
- }
- /*******************************/
- /// <summary>
- /// Support class used to handle threads
- /// </summary>
- public class ThreadClass : IThreadRunnable
- {
- /// <summary>
- /// The instance of System.Threading.Thread
- /// </summary>
- private System.Threading.Thread threadField;
-
- /// <summary>
- /// Initializes a new instance of the ThreadClass class
- /// </summary>
- public ThreadClass()
- {
- threadField = new System.Threading.Thread(new System.Threading.ThreadStart(Run));
- }
-
- /// <summary>
- /// Initializes a new instance of the Thread class.
- /// </summary>
- /// <param name="Name">The name of the thread</param>
- public ThreadClass(string Name)
- {
- threadField = new System.Threading.Thread(new System.Threading.ThreadStart(Run));
- this.Name = Name;
- }
-
- /// <summary>
- /// Initializes a new instance of the Thread class.
- /// </summary>
- /// <param name="Start">A ThreadStart delegate that references the methods to be invoked when this thread begins executing</param>
- public ThreadClass(System.Threading.ThreadStart Start)
- {
- threadField = new System.Threading.Thread(Start);
- }
-
- /// <summary>
- /// Initializes a new instance of the Thread class.
- /// </summary>
- /// <param name="Start">A ThreadStart delegate that references the methods to be invoked when this thread begins executing</param>
- /// <param name="Name">The name of the thread</param>
- public ThreadClass(System.Threading.ThreadStart Start, string Name)
- {
- threadField = new System.Threading.Thread(Start);
- this.Name = Name;
- }
-
- /// <summary>
- /// This method has no functionality unless the method is overridden
- /// </summary>
- public virtual void Run()
- {
- }
-
- /// <summary>
- /// Causes the operating system to change the state of the current thread instance to ThreadState.Running
- /// </summary>
- public virtual void Start()
- {
- threadField.Start();
- }
-
- /// <summary>
- /// Interrupts a thread that is in the WaitSleepJoin thread state
- /// </summary>
- public virtual void Interrupt()
- {
- threadField.Interrupt();
- }
-
- /// <summary>
- /// Gets the current thread instance
- /// </summary>
- public System.Threading.Thread Instance
- {
- get
- {
- return threadField;
- }
- set
- {
- threadField = value;
- }
- }
-
- /// <summary>
- /// Gets or sets the name of the thread
- /// </summary>
- public System.String Name
- {
- get
- {
- return threadField.Name;
- }
- set
- {
- if (threadField.Name == null)
- threadField.Name = value;
- }
- }
-
- /// <summary>
- /// Gets or sets a value indicating the scheduling priority of a thread
- /// </summary>
- public System.Threading.ThreadPriority Priority
- {
- get
- {
- return threadField.Priority;
- }
- set
- {
- threadField.Priority = value;
- }
- }
-
- /// <summary>
- /// Gets a value indicating the execution status of the current thread
- /// </summary>
- public bool IsAlive
- {
- get
- {
- return threadField.IsAlive;
- }
- }
-
- /// <summary>
- /// Gets or sets a value indicating whether or not a thread is a background thread.
- /// </summary>
- public bool IsBackground
- {
- get
- {
- return threadField.IsBackground;
- }
- set
- {
- threadField.IsBackground = value;
- }
- }
-
- /// <summary>
- /// Blocks the calling thread until a thread terminates
- /// </summary>
- public void Join()
- {
- threadField.Join();
- }
-
- /// <summary>
- /// Blocks the calling thread until a thread terminates or the specified time elapses
- /// </summary>
- /// <param name="MiliSeconds">Time of wait in milliseconds</param>
- public void Join(long MiliSeconds)
- {
- lock(this)
- {
- threadField.Join(new System.TimeSpan(MiliSeconds * 10000));
- }
- }
-
- /// <summary>
- /// Blocks the calling thread until a thread terminates or the specified time elapses
- /// </summary>
- /// <param name="MiliSeconds">Time of wait in milliseconds</param>
- /// <param name="NanoSeconds">Time of wait in nanoseconds</param>
- public void Join(long MiliSeconds, int NanoSeconds)
- {
- lock(this)
- {
- threadField.Join(new System.TimeSpan(MiliSeconds * 10000 + NanoSeconds * 100));
- }
- }
-
- /// <summary>
- /// Resumes a thread that has been suspended
- /// </summary>
- public void Resume()
- {
- threadField.Resume();
- }
-
- /// <summary>
- /// Raises a ThreadAbortException in the thread on which it is invoked,
- /// to begin the process of terminating the thread. Calling this method
- /// usually terminates the thread
- /// </summary>
- public void Abort()
- {
- threadField.Abort();
- }
-
- /// <summary>
- /// Raises a ThreadAbortException in the thread on which it is invoked,
- /// to begin the process of terminating the thread while also providing
- /// exception information about the thread termination.
- /// Calling this method usually terminates the thread.
- /// </summary>
- /// <param name="stateInfo">An object that contains application-specific information, such as state, which can be used by the thread being aborted</param>
- public void Abort(System.Object stateInfo)
- {
- lock(this)
- {
- threadField.Abort(stateInfo);
- }
- }
-
- /// <summary>
- /// Suspends the thread, if the thread is already suspended it has no effect
- /// </summary>
- public void Suspend()
- {
- threadField.Suspend();
- }
-
- /// <summary>
- /// Obtain a String that represents the current Object
- /// </summary>
- /// <returns>A String that represents the current Object</returns>
- public override System.String ToString()
- {
- return "Thread[" + Name + "," + Priority.ToString() + "," + "" + "]";
- }
-
- /// <summary>
- /// Gets the currently running thread
- /// </summary>
- /// <returns>The currently running thread</returns>
- public static ThreadClass Current()
- {
- ThreadClass CurrentThread = new ThreadClass();
- CurrentThread.Instance = System.Threading.Thread.CurrentThread;
- return CurrentThread;
- }
- }
- /*******************************/
- /// <summary>
- /// This class contains different methods to manage Collections.
- /// </summary>
- public class CollectionSupport : System.Collections.CollectionBase
- {
- /// <summary>
- /// Creates an instance of the Collection by using an inherited constructor.
- /// </summary>
- public CollectionSupport() : base()
- {
- }
- /// <summary>
- /// Adds an specified element to the collection.
- /// </summary>
- /// <param name="element">The element to be added.</param>
- /// <returns>Returns true if the element was successfuly added. Otherwise returns false.</returns>
- public virtual bool Add(System.Object element)
- {
- return (this.List.Add(element) != -1);
- }
- /// <summary>
- /// Adds all the elements contained in the specified collection.
- /// </summary>
- /// <param name="collection">The collection used to extract the elements that will be added.</param>
- /// <returns>Returns true if all the elements were successfuly added. Otherwise returns false.</returns>
- public virtual bool AddAll(System.Collections.ICollection collection)
- {
- bool result = false;
- if (collection!=null)
- {
- System.Collections.IEnumerator tempEnumerator = new System.Collections.ArrayList(collection).GetEnumerator();
- while (tempEnumerator.MoveNext())
- {
- if (tempEnumerator.Current != null)
- result = this.Add(tempEnumerator.Current);
- }
- }
- return result;
- }
- /// <summary>
- /// Adds all the elements contained in the specified support class collection.
- /// </summary>
- /// <param name="collection">The collection used to extract the elements that will be added.</param>
- /// <returns>Returns true if all the elements were successfuly added. Otherwise returns false.</returns>
- public virtual bool AddAll(CollectionSupport collection)
- {
- return this.AddAll((System.Collections.ICollection)collection);
- }
- /// <summary>
- /// Verifies if the specified element is contained into the collection.
- /// </summary>
- /// <param name="element"> The element that will be verified.</param>
- /// <returns>Returns true if the element is contained in the collection. Otherwise returns false.</returns>
- public virtual bool Contains(System.Object element)
- {
- return this.List.Contains(element);
- }
- /// <summary>
- /// Verifies if all the elements of the specified collection are contained into the current collection.
- /// </summary>
- /// <param name="collection">The collection used to extract the elements that will be verified.</param>
- /// <returns>Returns true if all the elements are contained in the collection. Otherwise returns false.</returns>
- public virtual bool ContainsAll(System.Collections.ICollection collection)
- {
- bool result = false;
- System.Collections.IEnumerator tempEnumerator = new System.Collections.ArrayList(collection).GetEnumerator();
- while (tempEnumerator.MoveNext())
- if (!(result = this.Contains(tempEnumerator.Current)))
- break;
- return result;
- }
- /// <summary>
- /// Verifies if all the elements of the specified collection are contained into the current collection.
- /// </summary>
- /// <param name="collection">The collection used to extract the elements that will be verified.</param>
- /// <returns>Returns true if all the elements are contained in the collection. Otherwise returns false.</returns>
- public virtual bool ContainsAll(CollectionSupport collection)
- {
- return this.ContainsAll((System.Collections.ICollection) collection);
- }
- /// <summary>
- /// Verifies if the collection is empty.
- /// </summary>
- /// <returns>Returns true if the collection is empty. Otherwise returns false.</returns>
- public virtual bool IsEmpty()
- {
- return (this.Count == 0);
- }
- /// <summary>
- /// Removes an specified element from the collection.
- /// </summary>
- /// <param name="element">The element to be removed.</param>
- /// <returns>Returns true if the element was successfuly removed. Otherwise returns false.</returns>
- public virtual bool Remove(System.Object element)
- {
- bool result = false;
- if (this.Contains(element))
- {
- this.List.Remove(element);
- result = true;
- }
- return result;
- }
- /// <summary>
- /// Removes all the elements contained into the specified collection.
- /// </summary>
- /// <param name="collection">The collection used to extract the elements that will be removed.</param>
- /// <returns>Returns true if all the elements were successfuly removed. Otherwise returns false.</returns>
- public virtual bool RemoveAll(System.Collections.ICollection collection)
- {
- bool result = false;
- System.Collections.IEnumerator tempEnumerator = new System.Collections.ArrayList(collection).GetEnumerator();
- while (tempEnumerator.MoveNext())
- {
- if (this.Contains(tempEnumerator.Current))
- result = this.Remove(tempEnumerator.Current);
- }
- return result;
- }
- /// <summary>
- /// Removes all the elements contained into the specified collection.
- /// </summary>
- /// <param name="collection">The collection used to extract the elements that will be removed.</param>
- /// <returns>Returns true if all the elements were successfuly removed. Otherwise returns false.</returns>
- public virtual bool RemoveAll(CollectionSupport collection)
- {
- return this.RemoveAll((System.Collections.ICollection) collection);
- }
- /// <summary>
- /// Removes all the elements that aren't contained into the specified collection.
- /// </summary>
- /// <param name="collection">The collection used to verify the elements that will be retained.</param>
- /// <returns>Returns true if all the elements were successfully removed. Otherwise returns false.</returns>
- public virtual bool RetainAll(System.Collections.ICollection collection)
- {
- bool result = false;
- System.Collections.IEnumerator tempEnumerator = this.GetEnumerator();
- CollectionSupport tempCollection = new CollectionSupport();
- tempCollection.AddAll(collection);
- while (tempEnumerator.MoveNext())
- if (!tempCollection.Contains(tempEnumerator.Current))
- {
- result = this.Remove(tempEnumerator.Current);
-
- if (result == true)
- {
- tempEnumerator = this.GetEnumerator();
- }
- }
- return result;
- }
- /// <summary>
- /// Removes all the elements that aren't contained into the specified collection.
- /// </summary>
- /// <param name="collection">The collection used to verify the elements that will be retained.</param>
- /// <returns>Returns true if all the elements were successfully removed. Otherwise returns false.</returns>
- public virtual bool RetainAll(CollectionSupport collection)
- {
- return this.RetainAll((System.Collections.ICollection) collection);
- }
- /// <summary>
- /// Obtains an array containing all the elements of the collection.
- /// </summary>
- /// <returns>The array containing all the elements of the collection</returns>
- public virtual System.Object[] ToArray()
- {
- int index = 0;
- System.Object[] objects = new System.Object[this.Count];
- System.Collections.IEnumerator tempEnumerator = this.GetEnumerator();
- while (tempEnumerator.MoveNext())
- objects[index++] = tempEnumerator.Current;
- return objects;
- }
- /// <summary>
- /// Obtains an array containing all the elements of the collection.
- /// </summary>
- /// <param name="objects">The array into which the elements of the collection will be stored.</param>
- /// <returns>The array containing all the elements of the collection.</returns>
- public virtual System.Object[] ToArray(System.Object[] objects)
- {
- int index = 0;
- System.Collections.IEnumerator tempEnumerator = this.GetEnumerator();
- while (tempEnumerator.MoveNext())
- objects[index++] = tempEnumerator.Current;
- return objects;
- }
- /// <summary>
- /// Creates a CollectionSupport object with the contents specified in array.
- /// </summary>
- /// <param name="array">The array containing the elements used to populate the new CollectionSupport object.</param>
- /// <returns>A CollectionSupport object populated with the contents of array.</returns>
- public static CollectionSupport ToCollectionSupport(System.Object[] array)
- {
- CollectionSupport tempCollectionSupport = new CollectionSupport();
- tempCollectionSupport.AddAll(array);
- return tempCollectionSupport;
- }
- }
- /*******************************/
- /// <summary>
- /// This class contains different methods to manage list collections.
- /// </summary>
- public class ListCollectionSupport : System.Collections.ArrayList
- {
- /// <summary>
- /// Creates a new instance of the class ListCollectionSupport.
- /// </summary>
- public ListCollectionSupport() : base()
- {
- }
-
- /// <summary>
- /// Creates a new instance of the class ListCollectionSupport.
- /// </summary>
- /// <param name="collection">The collection to insert into the new object.</param>
- public ListCollectionSupport(System.Collections.ICollection collection) : base(collection)
- {
- }
- /// <summary>
- /// Creates a new instance of the class ListCollectionSupport with the specified capacity.
- /// </summary>
- /// <param name="capacity">The capacity of the new array.</param>
- public ListCollectionSupport(int capacity) : base(capacity)
- {
- }
- /// <summary>
- /// Adds an object to the end of the List.
- /// </summary>
- /// <param name="valueToInsert">The value to insert in the array list.</param>
- /// <returns>Returns true after adding the value.</returns>
- public virtual bool Add(System.Object valueToInsert)
- {
- base.Insert(this.Count, valueToInsert);
- return true;
- }
- /// <summary>
- /// Adds all the elements contained into the specified collection, starting at the specified position.
- /// </summary>
- /// <param name="index">Position at which to add the first element from the specified collection.</param>
- /// <param name="list">The list used to extract the elements that will be added.</param>
- /// <returns>Returns true if all the elements were successfuly added. Otherwise returns false.</returns>
- public virtual bool AddAll(int index, System.Collections.IList list)
- {
- bool result = false;
- if (list!=null)
- {
- System.Collections.IEnumerator tempEnumerator = new System.Collections.ArrayList(list).GetEnumerator();
- int tempIndex = index;
- while (tempEnumerator.MoveNext())
- {
- base.Insert(tempIndex++, tempEnumerator.Current);
- result = true;
- }
- }
- return result;
- }
- /// <summary>
- /// Adds all the elements contained in the specified collection.
- /// </summary>
- /// <param name="collection">The collection used to extract the elements that will be added.</param>
- /// <returns>Returns true if all the elements were successfuly added. Otherwise returns false.</returns>
- public virtual bool AddAll(System.Collections.IList collection)
- {
- return this.AddAll(this.Count,collection);
- }
- /// <summary>
- /// Adds all the elements contained in the specified support class collection.
- /// </summary>
- /// <param name="collection">The collection used to extract the elements that will be added.</param>
- /// <returns>Returns true if all the elements were successfuly added. Otherwise returns false.</returns>
- public virtual bool AddAll(CollectionSupport collection)
- {
- return this.AddAll(this.Count,collection);
- }
- /// <summary>
- /// Adds all the elements contained into the specified support class collection, starting at the specified position.
- /// </summary>
- /// <param name="index">Position at which to add the first element from the specified collection.</param>
- /// <param name="list">The list used to extract the elements that will be added.</param>
- /// <returns>Returns true if all the elements were successfuly added. Otherwise returns false.</returns>
- public virtual bool AddAll(int index, CollectionSupport collection)
- {
- return this.AddAll(index,(System.Collections.IList)collection);
- }
-
- /// <summary>
- /// Creates a copy of the ListCollectionSupport.
- /// </summary>
- /// <returns> A copy of the ListCollectionSupport.</returns>
- public virtual System.Object ListCollectionClone()
- {
- return MemberwiseClone();
- }
- /// <summary>
- /// Returns an iterator of the collection.
- /// </summary>
- /// <returns>An IEnumerator.</returns>
- public virtual System.Collections.IEnumerator ListIterator()
- {
- return base.GetEnumerator();
- }
- /// <summary>
- /// Removes all the elements contained into the specified collection.
- /// </summary>
- /// <param name="collection">The collection used to extract the elements that will be removed.</param>
- /// <returns>Returns true if all the elements were successfuly removed. Otherwise returns false.</returns>
- public virtual bool RemoveAll(System.Collections.ICollection collection)
- {
- bool result = false;
- System.Collections.IEnumerator tempEnumerator = new System.Collections.ArrayList(collection).GetEnumerator();
- while (tempEnumerator.MoveNext())
- {
- result = true;
- if (base.Contains(tempEnumerator.Current))
- base.Remove(tempEnumerator.Current);
- }
- return result;
- }
-
- /// <summary>
- /// Removes all the elements contained into the specified collection.
- /// </summary>
- /// <param name="collection">The collection used to extract the elements that will be removed.</param>
- /// <returns>Returns true if all the elements were successfuly removed. Otherwise returns false.</returns>
- public virtual bool RemoveAll(CollectionSupport collection)
- {
- return this.RemoveAll((System.Collections.ICollection) collection);
- }
- /// <summary>
- /// Removes the value in the specified index from the list.
- /// </summary>
- /// <param name="index">The index of the value to remove.</param>
- /// <returns>Returns the value removed.</returns>
- public virtual System.Object RemoveElement(int index)
- {
- System.Object objectRemoved = this[index];
- this.RemoveAt(index);
- return objectRemoved;
- }
- /// <summary>
- /// Removes an specified element from the collection.
- /// </summary>
- /// <param name="element">The element to be removed.</param>
- /// <returns>Returns true if the element was successfuly removed. Otherwise returns false.</returns>
- public virtual bool RemoveElement(System.Object element)
- {
- bool result = false;
- if (this.Contains(element))
- {
- base.Remove(element);
- result = true;
- }
- return result;
- }
- /// <summary>
- /// Removes the first value from an array list.
- /// </summary>
- /// <returns>Returns the value removed.</returns>
- public virtual System.Object RemoveFirst()
- {
- System.Object objectRemoved = this[0];
- this.RemoveAt(0);
- return objectRemoved;
- }
- /// <summary>
- /// Removes the last value from an array list.
- /// </summary>
- /// <returns>Returns the value removed.</returns>
- public virtual System.Object RemoveLast()
- {
- System.Object objectRemoved = this[this.Count-1];
- base.RemoveAt(this.Count-1);
- return objectRemoved;
- }
- /// <summary>
- /// Removes all the elements that aren't contained into the specified collection.
- /// </summary>
- /// <param name="collection">The collection used to verify the elements that will be retained.</param>
- /// <returns>Returns true if all the elements were successfully removed. Otherwise returns false.</returns>
- public virtual bool RetainAll(System.Collections.ICollection collection)
- {
- bool result = false;
- System.Collections.IEnumerator tempEnumerator = this.GetEnumerator();
- ListCollectionSupport tempCollection = new ListCollectionSupport(collection);
- while (tempEnumerator.MoveNext())
- if (!tempCollection.Contains(tempEnumerator.Current))
- {
- result = this.RemoveElement(tempEnumerator.Current);
-
- if (result == true)
- {
- tempEnumerator = this.GetEnumerator();
- }
- }
- return result;
- }
-
- /// <summary>
- /// Removes all the elements that aren't contained into the specified collection.
- /// </summary>
- /// <param name="collection">The collection used to verify the elements that will be retained.</param>
- /// <returns>Returns true if all the elements were successfully removed. Otherwise returns false.</returns>
- public virtual bool RetainAll(CollectionSupport collection)
- {
- return this.RetainAll((System.Collections.ICollection) collection);
- }
- /// <summary>
- /// Verifies if all the elements of the specified collection are contained into the current collection.
- /// </summary>
- /// <param name="collection">The collection used to extract the elements that will be verified.</param>
- /// <returns>Returns true if all the elements are contained in the collection. Otherwise returns false.</returns>
- public virtual bool ContainsAll(System.Collections.ICollection collection)
- {
- bool result = false;
- System.Collections.IEnumerator tempEnumerator = new System.Collections.ArrayList(collection).GetEnumerator();
- while (tempEnumerator.MoveNext())
- if(!(result = this.Contains(tempEnumerator.Current)))
- break;
- return result;
- }
-
- /// <summary>
- /// Verifies if all the elements of the specified collection are contained into the current collection.
- /// </summary>
- /// <param name="collection">The collection used to extract the elements that will be verified.</param>
- /// <returns>Returns true if all the elements are contained in the collection. Otherwise returns false.</returns>
- public virtual bool ContainsAll(CollectionSupport collection)
- {
- return this.ContainsAll((System.Collections.ICollection) collection);
- }
- /// <summary>
- /// Returns a new list containing a portion of the current list between a specified range.
- /// </summary>
- /// <param name="startIndex">The start index of the range.</param>
- /// <param name="endIndex">The end index of the range.</param>
- /// <returns>A ListCollectionSupport instance containing the specified elements.</returns>
- public virtual ListCollectionSupport SubList(int startIndex, int endIndex)
- {
- int index = 0;
- System.Collections.IEnumerator tempEnumerator = this.GetEnumerator();
- ListCollectionSupport result = new ListCollectionSupport();
- for(index = startIndex; index < endIndex; index++)
- result.Add(this[index]);
- return (ListCollectionSupport)result;
- }
- /// <summary>
- /// Obtains an array containing all the elements of the collection.
- /// </summary>
- /// <param name="objects">The array into which the elements of the collection will be stored.</param>
- /// <returns>The array containing all the elements of the collection.</returns>
- public virtual System.Object[] ToArray(System.Object[] objects)
- {
- if (objects.Length < this.Count)
- objects = new System.Object[this.Count];
- int index = 0;
- System.Collections.IEnumerator tempEnumerator = this.GetEnumerator();
- while (tempEnumerator.MoveNext())
- objects[index++] = tempEnumerator.Current;
- return objects;
- }
- /// <summary>
- /// Returns an iterator of the collection starting at the specified position.
- /// </summary>
- /// <param name="index">The position to set the iterator.</param>
- /// <returns>An IEnumerator at the specified position.</returns>
- public virtual System.Collections.IEnumerator ListIterator(int index)
- {
- if ((index < 0) || (index > this.Count)) throw new System.IndexOutOfRangeException();
- System.Collections.IEnumerator tempEnumerator= this.GetEnumerator();
- if (index > 0)
- {
- int i=0;
- while ((tempEnumerator.MoveNext()) && (i < index - 1))
- i++;
- }
- return tempEnumerator;
- }
-
- /// <summary>
- /// Gets the last value from a list.
- /// </summary>
- /// <returns>Returns the last element of the list.</returns>
- public virtual System.Object GetLast()
- {
- if (this.Count == 0) throw new System.ArgumentOutOfRangeException();
- else
- {
- return this[this.Count - 1];
- }
- }
-
- /// <summary>
- /// Return whether this list is empty.
- /// </summary>
- /// <returns>True if the list is empty, false if it isn't.</returns>
- public virtual bool IsEmpty()
- {
- return (this.Count == 0);
- }
-
- /// <summary>
- /// Replaces the element at the specified position in this list with the specified element.
- /// </summary>
- /// <param name="index">Index of element to replace.</param>
- /// <param name="element">Element to be stored at the specified position.</param>
- /// <returns>The element previously at the specified position.</returns>
- public virtual System.Object Set(int index, System.Object element)
- {
- System.Object result = this[index];
- this[index] = element;
- return result;
- }
- /// <summary>
- /// Returns the element at the specified position in the list.
- /// </summary>
- /// <param name="index">Index of element to return.</param>
- /// <param name="element">Element to be stored at the specified position.</param>
- /// <returns>The element at the specified position in the list.</returns>
- public virtual System.Object Get(int index)
- {
- return this[index];
- }
- }
- /*******************************/
- /// <summary>
- /// This class manages array operations.
- /// </summary>
- public class ArraysSupport
- {
- /// <summary>
- /// Compares the entire members of one array whith the other one.
- /// </summary>
- /// <param name="array1">The array to be compared.</param>
- /// <param name="array2">The array to be compared with.</param>
- /// <returns>True if both arrays are equals otherwise it returns false.</returns>
- /// <remarks>Two arrays are equal if they contains the same elements in the same order.</remarks>
- public static bool IsArrayEqual(System.Array array1, System.Array array2)
- {
- if (array1.Length != array2.Length)
- return false;
- for (int i = 0; i < array1.Length; i++)
- if (!(array1.GetValue(i).Equals(array2.GetValue(i))))
- return false;
- return true;
- }
- /// <summary>
- /// Fills the array with an specific value from an specific index to an specific index.
- /// </summary>
- /// <param name="array">The array to be filled.</param>
- /// <param name="fromindex">The first index to be filled.</param>
- /// <param name="toindex">The last index to be filled.</param>
- /// <param name="val">The value to fill the array with.</param>
- public static void FillArray(System.Array array, System.Int32 fromindex,System.Int32 toindex, System.Object val)
- {
- System.Object Temp_Object = val;
- System.Type elementtype = array.GetType().GetElementType();
- if (elementtype != val.GetType())
- Temp_Object = System.Convert.ChangeType(val, elementtype);
- if (array.Length == 0)
- throw (new System.NullReferenceException());
- if (fromindex > toindex)
- throw (new System.ArgumentException());
- if ((fromindex < 0) || ((System.Array)array).Length < toindex)
- throw (new System.IndexOutOfRangeException());
- for (int index = (fromindex > 0) ? fromindex-- : fromindex; index < toindex; index++)
- array.SetValue(Temp_Object, index);
- }
- /// <summary>
- /// Fills the array with an specific value.
- /// </summary>
- /// <param name="array">The array to be filled.</param>
- /// <param name="val">The value to fill the array with.</param>
- public static void FillArray(System.Array array, System.Object val)
- {
- FillArray(array, 0, array.Length, val);
- }
- }
- /*******************************/
- /// <summary>
- /// This class manages a set of elements.
- /// </summary>
- public class SetSupport : System.Collections.ArrayList
- {
- /// <summary>
- /// Creates a new set.
- /// </summary>
- public SetSupport(): base()
- {
- …
Large files files are truncated, but you can click here to view the full file