Don't need to block multiple thread access. Refactor and update a bit for an upcoming...
[zxing.git] / csharp / SupportClass.cs
index 5058d7d..7e2d298 100755 (executable)
-/*\r
-* Licensed under the Apache License, Version 2.0 (the "License");\r
-* you may not use this file except in compliance with the License.\r
-* You may obtain a copy of the License at\r
-*\r
-*      http://www.apache.org/licenses/LICENSE-2.0\r
-*\r
-* Unless required by applicable law or agreed to in writing, software\r
-* distributed under the License is distributed on an "AS IS" BASIS,\r
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
-* See the License for the specific language governing permissions and\r
-* limitations under the License.\r
-*/\r
+//\r
+// In order to convert some functionality to Visual C#, the Java Language Conversion Assistant\r
+// creates "support classes" that duplicate the original functionality.  \r
+//\r
+// Support classes replicate the functionality of the original code, but in some cases they are \r
+// substantially different architecturally. Although every effort is made to preserve the \r
+// original architecture of the application in the converted project, the user should be aware that \r
+// the primary goal of these support classes is to replicate functionality, and that at times \r
+// the architecture of the resulting solution may differ somewhat.\r
+//\r
 \r
 using System;\r
 \r
-       /// <summary>\r
-       /// This interface should be implemented by any class whose instances are intended \r
-       /// to be executed by a thread.\r
-       /// </summary>\r
-       public interface IThreadRunnable\r
-       {\r
-               /// <summary>\r
-               /// This method has to be implemented in order that starting of the thread causes the object's \r
-               /// run method to be called in that separately executing thread.\r
-               /// </summary>\r
-               void Run();\r
-       }\r
-\r
 /// <summary>\r
 /// Contains conversion support elements such as classes, interfaces and static methods.\r
 /// </summary>\r
 public class SupportClass\r
 {\r
-\r
-\r
-\r
-       /// <summary>\r
-       /// Performs an unsigned bitwise right shift with the specified number\r
-       /// </summary>\r
-       /// <param name="number">Number to operate on</param>\r
-       /// <param name="bits">Ammount of bits to shift</param>\r
-       /// <returns>The resulting number from the shift operation</returns>\r
-       public static int URShift(int number, int bits)\r
-       {\r
-               if ( number >= 0)\r
-                       return number >> bits;\r
-               else\r
-                       return (number >> bits) + (2 << ~bits);\r
-       }\r
-\r
-       /// <summary>\r
-       /// Performs an unsigned bitwise right shift with the specified number\r
-       /// </summary>\r
-       /// <param name="number">Number to operate on</param>\r
-       /// <param name="bits">Ammount of bits to shift</param>\r
-       /// <returns>The resulting number from the shift operation</returns>\r
-       public static int URShift(int number, long bits)\r
-       {\r
-               return URShift(number, (int)bits);\r
-       }\r
-\r
-       /// <summary>\r
-       /// Performs an unsigned bitwise right shift with the specified number\r
-       /// </summary>\r
-       /// <param name="number">Number to operate on</param>\r
-       /// <param name="bits">Ammount of bits to shift</param>\r
-       /// <returns>The resulting number from the shift operation</returns>\r
-       public static long URShift(long number, int bits)\r
-       {\r
-               if ( number >= 0)\r
-                       return number >> bits;\r
-               else\r
-                       return (number >> bits) + (2L << ~bits);\r
-       }\r
-\r
-       /// <summary>\r
-       /// Performs an unsigned bitwise right shift with the specified number\r
-       /// </summary>\r
-       /// <param name="number">Number to operate on</param>\r
-       /// <param name="bits">Ammount of bits to shift</param>\r
-       /// <returns>The resulting number from the shift operation</returns>\r
-       public static long URShift(long number, long bits)\r
-       {\r
-               return URShift(number, (int)bits);\r
-       }\r
-\r
-       /*******************************/\r
-       /// <summary>\r
-       /// Copies an array of chars obtained from a String into a specified array of chars\r
-       /// </summary>\r
-       /// <param name="sourceString">The String to get the chars from</param>\r
-       /// <param name="sourceStart">Position of the String to start getting the chars</param>\r
-       /// <param name="sourceEnd">Position of the String to end getting the chars</param>\r
-       /// <param name="destinationArray">Array to return the chars</param>\r
-       /// <param name="destinationStart">Position of the destination array of chars to start storing the chars</param>\r
-       /// <returns>An array of chars</returns>\r
-       public static void GetCharsFromString(System.String sourceString, int sourceStart, int sourceEnd, char[] destinationArray, int destinationStart)\r
-       {       \r
-               int sourceCounter;\r
-               int destinationCounter;\r
-               sourceCounter = sourceStart;\r
-               destinationCounter = destinationStart;\r
-               while (sourceCounter < sourceEnd)\r
-               {\r
-                       destinationArray[destinationCounter] = (char) sourceString[sourceCounter];\r
-                       sourceCounter++;\r
-                       destinationCounter++;\r
-               }\r
-       }\r
-\r
-       /*******************************/\r
        /// <summary>\r
        /// Converts an array of sbytes to an array of bytes\r
        /// </summary>\r
@@ -157,20 +63,54 @@ public class SupportClass
 \r
        /*******************************/\r
        /// <summary>\r
-       /// Sets the capacity for the specified ArrayList\r
+       /// Performs an unsigned bitwise right shift with the specified number\r
        /// </summary>\r
-       /// <param name="vector">The ArrayList which capacity will be set</param>\r
-       /// <param name="newCapacity">The new capacity value</param>\r
-       public static void SetCapacity(System.Collections.ArrayList vector, int newCapacity)\r
+       /// <param name="number">Number to operate on</param>\r
+       /// <param name="bits">Ammount of bits to shift</param>\r
+       /// <returns>The resulting number from the shift operation</returns>\r
+       public static int URShift(int number, int bits)\r
        {\r
-               if (newCapacity > vector.Count)\r
-                       vector.AddRange(new Array[newCapacity-vector.Count]);\r
-               else if (newCapacity < vector.Count)\r
-                       vector.RemoveRange(newCapacity, vector.Count - newCapacity);\r
-               vector.Capacity = newCapacity;\r
+               if ( number >= 0)\r
+                       return number >> bits;\r
+               else\r
+                       return (number >> bits) + (2 << ~bits);\r
        }\r
 \r
+       /// <summary>\r
+       /// Performs an unsigned bitwise right shift with the specified number\r
+       /// </summary>\r
+       /// <param name="number">Number to operate on</param>\r
+       /// <param name="bits">Ammount of bits to shift</param>\r
+       /// <returns>The resulting number from the shift operation</returns>\r
+       public static int URShift(int number, long bits)\r
+       {\r
+               return URShift(number, (int)bits);\r
+       }\r
 \r
+       /// <summary>\r
+       /// Performs an unsigned bitwise right shift with the specified number\r
+       /// </summary>\r
+       /// <param name="number">Number to operate on</param>\r
+       /// <param name="bits">Ammount of bits to shift</param>\r
+       /// <returns>The resulting number from the shift operation</returns>\r
+       public static long URShift(long number, int bits)\r
+       {\r
+               if ( number >= 0)\r
+                       return number >> bits;\r
+               else\r
+                       return (number >> bits) + (2L << ~bits);\r
+       }\r
+\r
+       /// <summary>\r
+       /// Performs an unsigned bitwise right shift with the specified number\r
+       /// </summary>\r
+       /// <param name="number">Number to operate on</param>\r
+       /// <param name="bits">Ammount of bits to shift</param>\r
+       /// <returns>The resulting number from the shift operation</returns>\r
+       public static long URShift(long number, long bits)\r
+       {\r
+               return URShift(number, (int)bits);\r
+       }\r
 \r
        /*******************************/\r
        /// <summary>\r
@@ -215,242 +155,61 @@ public class SupportClass
 \r
        /*******************************/\r
        /// <summary>\r
-       /// Support class used to handle threads\r
+       /// Copies an array of chars obtained from a String into a specified array of chars\r
        /// </summary>\r
-       public class ThreadClass : IThreadRunnable\r
-       {\r
-               /// <summary>\r
-               /// The instance of System.Threading.Thread\r
-               /// </summary>\r
-               private System.Threading.Thread threadField;\r
-             \r
-               /// <summary>\r
-               /// Initializes a new instance of the ThreadClass class\r
-               /// </summary>\r
-               public ThreadClass()\r
-               {\r
-                       threadField = new System.Threading.Thread(new System.Threading.ThreadStart(Run));\r
-               }\r
-        \r
-               /// <summary>\r
-               /// Initializes a new instance of the Thread class.\r
-               /// </summary>\r
-               /// <param name="Name">The name of the thread</param>\r
-               public ThreadClass(System.String Name)\r
-               {\r
-                       threadField = new System.Threading.Thread(new System.Threading.ThreadStart(Run));\r
-                       this.Name = Name;\r
-               }\r
-             \r
-               /// <summary>\r
-               /// Initializes a new instance of the Thread class.\r
-               /// </summary>\r
-               /// <param name="Start">A ThreadStart delegate that references the methods to be invoked when this thread begins executing</param>\r
-               public ThreadClass(System.Threading.ThreadStart Start)\r
-               {\r
-                       threadField = new System.Threading.Thread(Start);\r
-               }\r
-        \r
-               /// <summary>\r
-               /// Initializes a new instance of the Thread class.\r
-               /// </summary>\r
-               /// <param name="Start">A ThreadStart delegate that references the methods to be invoked when this thread begins executing</param>\r
-               /// <param name="Name">The name of the thread</param>\r
-               public ThreadClass(System.Threading.ThreadStart Start, System.String Name)\r
-               {\r
-                       threadField = new System.Threading.Thread(Start);\r
-                       this.Name = Name;\r
-               }\r
-             \r
-               /// <summary>\r
-               /// This method has no functionality unless the method is overridden\r
-               /// </summary>\r
-               public virtual void Run()\r
-               {\r
-               }\r
-             \r
-               /// <summary>\r
-               /// Causes the operating system to change the state of the current thread instance to ThreadState.Running\r
-               /// </summary>\r
-               public virtual void Start()\r
-               {\r
-                       threadField.Start();\r
-               }\r
-             \r
-               ///// <summary>\r
-               ///// Interrupts a thread that is in the WaitSleepJoin thread state\r
-               ///// </summary>\r
-               //public virtual void Interrupt()\r
-               //{\r
-               //  threadField.Interrupt();\r
-               //}\r
-             \r
-               /// <summary>\r
-               /// Gets the current thread instance\r
-               /// </summary>\r
-               public System.Threading.Thread Instance\r
-               {\r
-                       get\r
-                       {\r
-                               return threadField;\r
-                       }\r
-                       set\r
-                       {\r
-                               threadField = value;\r
-                       }\r
-               }\r
-             \r
-               /// <summary>\r
-               /// Gets or sets the name of the thread\r
-               /// </summary>\r
-               public System.String Name\r
-               {\r
-                       get\r
-                       {\r
-                               return threadField.Name;\r
-                       }\r
-                       set\r
-                       {\r
-                               if (threadField.Name == null)\r
-                                       threadField.Name = value; \r
-                       }\r
-               }\r
-             \r
-               /// <summary>\r
-               /// Gets or sets a value indicating the scheduling priority of a thread\r
-               /// </summary>\r
-               public System.Threading.ThreadPriority Priority\r
-               {\r
-                       get\r
-                       {\r
-                               return threadField.Priority;\r
-                       }\r
-                       set\r
-                       {\r
-                               threadField.Priority = value;\r
-                       }\r
-               }\r
-             \r
-               ///// <summary>\r
-               ///// Gets a value indicating the execution status of the current thread\r
-               ///// </summary>\r
-               //public bool IsAlive\r
-               //{\r
-               //  get\r
-               //  {\r
-               //    return threadField.IsAlive;\r
-               //  }\r
-               //}\r
-             \r
-               /// <summary>\r
-               /// Gets or sets a value indicating whether or not a thread is a background thread.\r
-               /// </summary>\r
-               public bool IsBackground\r
-               {\r
-                       get\r
-                       {\r
-                               return threadField.IsBackground;\r
-                       } \r
-                       set\r
-                       {\r
-                               threadField.IsBackground = value;\r
-                       }\r
-               }\r
-             \r
-               /// <summary>\r
-               /// Blocks the calling thread until a thread terminates\r
-               /// </summary>\r
-               public void Join()\r
-               {\r
-                       threadField.Join();\r
-               }\r
-             \r
-               /// <summary>\r
-               /// Blocks the calling thread until a thread terminates or the specified time elapses\r
-               /// </summary>\r
-               /// <param name="MiliSeconds">Time of wait in milliseconds</param>\r
-               public void Join(int MiliSeconds)\r
-               {\r
-                       lock(this)\r
-                       {\r
-                               threadField.Join(MiliSeconds);\r
-                       }\r
-               }\r
-             \r
-               ///// <summary>\r
-               ///// Blocks the calling thread until a thread terminates or the specified time elapses\r
-               ///// </summary>\r
-               ///// <param name="MiliSeconds">Time of wait in milliseconds</param>\r
-               ///// <param name="NanoSeconds">Time of wait in nanoseconds</param>\r
-               //public void Join(long MiliSeconds, int NanoSeconds)\r
-               //{\r
-               //  lock(this)\r
-               //  {\r
-               //    threadField.Join(new System.TimeSpan(MiliSeconds * 10000 + NanoSeconds * 100));\r
-               //  }\r
-               //}\r
-             \r
-               ///// <summary>\r
-               ///// Resumes a thread that has been suspended\r
-               ///// </summary>\r
-               //public void Resume()\r
-               //{\r
-               //  threadField.Resume();\r
-               //}\r
-             \r
-               /// <summary>\r
-               /// Raises a ThreadAbortException in the thread on which it is invoked, \r
-               /// to begin the process of terminating the thread. Calling this method \r
-               /// usually terminates the thread\r
-               /// </summary>\r
-               public void Abort()\r
-               {\r
-                       threadField.Abort();\r
-               }\r
-             \r
-               /// <summary>\r
-               /// Raises a ThreadAbortException in the thread on which it is invoked, \r
-               /// to begin the process of terminating the thread while also providing\r
-               /// exception information about the thread termination. \r
-               /// Calling this method usually terminates the thread.\r
-               /// </summary>\r
-               /// <param name="stateInfo">An object that contains application-specific information, such as state, which can be used by the thread being aborted</param>\r
-               public void Abort(System.Object stateInfo)\r
-               {\r
-                       lock(this)\r
-                       {\r
-                               threadField.Abort(stateInfo);\r
-                       }\r
-               }\r
-             \r
-               ///// <summary>\r
-               ///// Suspends the thread, if the thread is already suspended it has no effect\r
-               ///// </summary>\r
-               //public void Suspend()\r
-               //{\r
-               //  threadField.Suspend();\r
-               //}\r
-             \r
-               /// <summary>\r
-               /// Obtain a String that represents the current Object\r
-               /// </summary>\r
-               /// <returns>A String that represents the current Object</returns>\r
-               public override System.String ToString()\r
+       /// <param name="sourceString">The String to get the chars from</param>\r
+       /// <param name="sourceStart">Position of the String to start getting the chars</param>\r
+       /// <param name="sourceEnd">Position of the String to end getting the chars</param>\r
+       /// <param name="destinationArray">Array to return the chars</param>\r
+       /// <param name="destinationStart">Position of the destination array of chars to start storing the chars</param>\r
+       /// <returns>An array of chars</returns>\r
+       public static void GetCharsFromString(System.String sourceString, int sourceStart, int sourceEnd, char[] destinationArray, int destinationStart)\r
+       {       \r
+               int sourceCounter;\r
+               int destinationCounter;\r
+               sourceCounter = sourceStart;\r
+               destinationCounter = destinationStart;\r
+               while (sourceCounter < sourceEnd)\r
                {\r
-                       return "Thread[" + Name + "," + Priority.ToString() + "," + "" + "]";\r
+                       destinationArray[destinationCounter] = (char) sourceString[sourceCounter];\r
+                       sourceCounter++;\r
+                       destinationCounter++;\r
                }\r
-            \r
-               /// <summary>\r
-               /// Gets the currently running thread\r
-               /// </summary>\r
-               /// <returns>The currently running thread</returns>\r
-               public static ThreadClass Current()\r
+       }\r
+\r
+       /*******************************/\r
+       /// <summary>\r
+       /// Sets the capacity for the specified ArrayList\r
+       /// </summary>\r
+       /// <param name="vector">The ArrayList which capacity will be set</param>\r
+       /// <param name="newCapacity">The new capacity value</param>\r
+       public static void SetCapacity(System.Collections.ArrayList vector, int newCapacity)\r
+       {\r
+               if (newCapacity > vector.Count)\r
+                       vector.AddRange(new Array[newCapacity-vector.Count]);\r
+               else if (newCapacity < vector.Count)\r
+                       vector.RemoveRange(newCapacity, vector.Count - newCapacity);\r
+               vector.Capacity = newCapacity;\r
+       }\r
+\r
+\r
+\r
+       /*******************************/\r
+       /// <summary>\r
+       /// Receives a byte array and returns it transformed in an sbyte array\r
+       /// </summary>\r
+       /// <param name="byteArray">Byte array to process</param>\r
+       /// <returns>The transformed array</returns>\r
+       public static sbyte[] ToSByteArray(byte[] byteArray)\r
+       {\r
+               sbyte[] sbyteArray = null;\r
+               if (byteArray != null)\r
                {\r
-                       ThreadClass CurrentThread = new ThreadClass();\r
-                       CurrentThread.Instance = System.Threading.Thread.CurrentThread;\r
-                       return CurrentThread;\r
+                       sbyteArray = new sbyte[byteArray.Length];\r
+                       for(int index=0; index < byteArray.Length; index++)\r
+                               sbyteArray[index] = (sbyte) byteArray[index];\r
                }\r
+               return sbyteArray;\r
        }\r
 \r
-\r
 }\r