2 * Copyright 2004 The Apache Software Foundation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 /// This interface should be implemented by any class whose instances are intended
21 /// to be executed by a thread.
23 public interface IThreadRunnable
26 /// This method has to be implemented in order that starting of the thread causes the object's
27 /// run method to be called in that separately executing thread.
33 /// Contains conversion support elements such as classes, interfaces and static methods.
35 public class SupportClass
38 /// Support class used to handle threads
40 public class ThreadClass
: IThreadRunnable
43 /// The instance of System.Threading.Thread
45 private System
.Threading
.Thread threadField
;
48 /// Initializes a new instance of the ThreadClass class
52 threadField
= new System
.Threading
.Thread(new System
.Threading
.ThreadStart(Run
));
56 /// Initializes a new instance of the Thread class.
58 /// <param name="Name">The name of the thread</param>
59 public ThreadClass(System
.String Name
)
61 threadField
= new System
.Threading
.Thread(new System
.Threading
.ThreadStart(Run
));
66 /// Initializes a new instance of the Thread class.
68 /// <param name="Start">A ThreadStart delegate that references the methods to be invoked when this thread begins executing</param>
69 public ThreadClass(System
.Threading
.ThreadStart Start
)
71 threadField
= new System
.Threading
.Thread(Start
);
75 /// Initializes a new instance of the Thread class.
77 /// <param name="Start">A ThreadStart delegate that references the methods to be invoked when this thread begins executing</param>
78 /// <param name="Name">The name of the thread</param>
79 public ThreadClass(System
.Threading
.ThreadStart Start
, System
.String Name
)
81 threadField
= new System
.Threading
.Thread(Start
);
86 /// This method has no functionality unless the method is overridden
88 public virtual void Run()
93 /// Causes the operating system to change the state of the current thread instance to ThreadState.Running
95 public virtual void Start()
101 /// Interrupts a thread that is in the WaitSleepJoin thread state
103 public virtual void Interrupt()
105 threadField
.Interrupt();
109 /// Gets the current thread instance
111 public System
.Threading
.Thread Instance
124 /// Gets or sets the name of the thread
126 public System
.String Name
130 return threadField
.Name
;
134 if (threadField
.Name
== null)
135 threadField
.Name
= value;
140 /// Gets or sets a value indicating the scheduling priority of a thread
142 public System
.Threading
.ThreadPriority Priority
146 return threadField
.Priority
;
150 threadField
.Priority
= value;
155 /// Gets a value indicating the execution status of the current thread
161 return threadField
.IsAlive
;
166 /// Gets or sets a value indicating whether or not a thread is a background thread.
168 public bool IsBackground
172 return threadField
.IsBackground
;
176 threadField
.IsBackground
= value;
181 /// Blocks the calling thread until a thread terminates
189 /// Blocks the calling thread until a thread terminates or the specified time elapses
191 /// <param name="MiliSeconds">Time of wait in milliseconds</param>
192 public void Join(long MiliSeconds
)
196 threadField
.Join(new System
.TimeSpan(MiliSeconds
* 10000));
201 /// Blocks the calling thread until a thread terminates or the specified time elapses
203 /// <param name="MiliSeconds">Time of wait in milliseconds</param>
204 /// <param name="NanoSeconds">Time of wait in nanoseconds</param>
205 public void Join(long MiliSeconds
, int NanoSeconds
)
209 threadField
.Join(new System
.TimeSpan(MiliSeconds
* 10000 + NanoSeconds
* 100));
214 /// Resumes a thread that has been suspended
218 threadField
.Resume();
222 /// Raises a ThreadAbortException in the thread on which it is invoked,
223 /// to begin the process of terminating the thread. Calling this method
224 /// usually terminates the thread
232 /// Raises a ThreadAbortException in the thread on which it is invoked,
233 /// to begin the process of terminating the thread while also providing
234 /// exception information about the thread termination.
235 /// Calling this method usually terminates the thread.
237 /// <param name="stateInfo">An object that contains application-specific information, such as state, which can be used by the thread being aborted</param>
238 public void Abort(System
.Object stateInfo
)
242 threadField
.Abort(stateInfo
);
247 /// Suspends the thread, if the thread is already suspended it has no effect
249 public void Suspend()
251 threadField
.Suspend();
255 /// Obtain a String that represents the current Object
257 /// <returns>A String that represents the current Object</returns>
258 public override System
.String
ToString()
260 return "Thread[" + Name
+ "," + Priority
.ToString() + "," + "" + "]";
264 /// Gets the currently running thread
266 /// <returns>The currently running thread</returns>
267 public static ThreadClass
Current()
269 ThreadClass CurrentThread
= new ThreadClass();
270 CurrentThread
.Instance
= System
.Threading
.Thread
.CurrentThread
;
271 return CurrentThread
;
276 /// Represents the methods to support some operations over files.
278 public class FileSupport
281 /// Returns an array of abstract pathnames representing the files and directories of the specified path.
283 /// <param name="path">The abstract pathname to list it childs.</param>
284 /// <returns>An array of abstract pathnames childs of the path specified or null if the path is not a directory</returns>
285 public static System
.IO
.FileInfo
[] GetFiles(System
.IO
.FileInfo path
)
287 if ((path
.Attributes
& System
.IO
.FileAttributes
.Directory
) > 0)
289 String
[] fullpathnames
= System
.IO
.Directory
.GetFileSystemEntries(path
.FullName
);
290 System
.IO
.FileInfo
[] result
= new System
.IO
.FileInfo
[fullpathnames
.Length
];
291 for (int i
= 0; i
< result
.Length
; i
++)
292 result
[i
] = new System
.IO
.FileInfo(fullpathnames
[i
]);
301 /// A simple class for number conversions.
308 public const int MIN_RADIX
= 2;
312 public const int MAX_RADIX
= 36;
314 private const System
.String digits
= "0123456789abcdefghijklmnopqrstuvwxyz";
316 public static System
.String
ToString(float f
)
318 if (((float)(int)f
) == f
)
320 return ((int)f
).ToString() + ".0";
324 return f
.ToString(System
.Globalization
.NumberFormatInfo
.InvariantInfo
);
329 /// Converts a number to System.String in the specified radix.
331 /// <param name="i">A number to be converted.</param>
332 /// <param name="radix">A radix.</param>
333 /// <returns>A System.String representation of the number in the specified redix.</returns>
334 public static System
.String
ToString(long i
, int radix
)
336 if (radix
< MIN_RADIX
|| radix
> MAX_RADIX
)
339 char[] buf
= new char[65];
341 bool negative
= (i
< 0);
350 buf
[charPos
--] = digits
[(int)(-(i
% radix
))];
353 buf
[charPos
] = digits
[(int)(-i
)];
357 buf
[--charPos
] = '-';
360 return new System
.String(buf
, charPos
, (65 - charPos
));
364 /// Parses a number in the specified radix.
366 /// <param name="s">An input System.String.</param>
367 /// <param name="radix">A radix.</param>
368 /// <returns>The parsed number in the specified radix.</returns>
369 public static long Parse(System
.String s
, int radix
)
373 throw new ArgumentException("null");
376 if (radix
< MIN_RADIX
)
378 throw new NotSupportedException("radix " + radix
+
379 " less than Number.MIN_RADIX");
381 if (radix
> MAX_RADIX
)
383 throw new NotSupportedException("radix " + radix
+
384 " greater than Number.MAX_RADIX");
392 for (int i
= s
.Length
- 1; i
>= 0; i
--)
394 int weight
= digits
.IndexOf(s
[i
]);
396 throw new FormatException("Invalid number for the specified radix");
398 result
+= (weight
* mult
);
406 /// Performs an unsigned bitwise right shift with the specified number
408 /// <param name="number">Number to operate on</param>
409 /// <param name="bits">Ammount of bits to shift</param>
410 /// <returns>The resulting number from the shift operation</returns>
411 public static int URShift(int number
, int bits
)
414 return number
>> bits
;
416 return (number
>> bits
) + (2 << ~bits
);
420 /// Returns the index of the first bit that is set to true that occurs
421 /// on or after the specified starting index. If no such bit exists
422 /// then -1 is returned.
424 /// <param name="bits">The BitArray object.</param>
425 /// <param name="fromIndex">The index to start checking from (inclusive).</param>
426 /// <returns>The index of the next set bit.</returns>
427 public static int NextSetBit(System
.Collections
.BitArray bits
, int fromIndex
)
429 for (int i
= fromIndex
; i
< bits
.Length
; i
++)
441 /// Mimics Java's Character class.
443 public class Character
445 private const char charNull
= '\0';
446 private const char charZero
= '0';
447 private const char charA
= 'a';
451 public static int MAX_RADIX
461 public static int MIN_RADIX
472 /// <param name="digit"></param>
473 /// <param name="radix"></param>
474 /// <returns></returns>
475 public static char ForDigit(int digit
, int radix
)
477 // if radix or digit is out of range,
478 // return the null character.
479 if (radix
< Character
.MIN_RADIX
)
481 if (radix
> Character
.MAX_RADIX
)
488 // if digit is less than 10,
489 // return '0' plus digit
491 return (char) ( (int) charZero
+ digit
);
493 // otherwise, return 'a' plus digit.
494 return (char) ((int) charA
+ digit
- 10);
506 /// <param name="dateTime"></param>
507 /// <returns></returns>
508 static public long GetTime(DateTime dateTime
)
510 TimeSpan ts
= dateTime
.Subtract(new DateTime(1970, 1, 1));
511 ts
= ts
.Subtract(TimeZone
.CurrentTimeZone
.GetUtcOffset(dateTime
));
512 return ts
.Ticks
/ TimeSpan
.TicksPerMillisecond
;
524 /// <param name="s"></param>
525 /// <param name="style"></param>
526 /// <param name="provider"></param>
527 /// <returns></returns>
528 public static System
.Single
Parse(System
.String s
, System
.Globalization
.NumberStyles style
, System
.IFormatProvider provider
)
532 if (s
.EndsWith("f") || s
.EndsWith("F"))
533 return System
.Single
.Parse(s
.Substring(0, s
.Length
- 1), style
, provider
);
535 return System
.Single
.Parse(s
, style
, provider
);
537 catch (System
.FormatException fex
)
546 /// <param name="s"></param>
547 /// <param name="provider"></param>
548 /// <returns></returns>
549 public static System
.Single
Parse(System
.String s
, System
.IFormatProvider provider
)
553 if (s
.EndsWith("f") || s
.EndsWith("F"))
554 return System
.Single
.Parse(s
.Substring(0, s
.Length
- 1), provider
);
556 return System
.Single
.Parse(s
, provider
);
558 catch (System
.FormatException fex
)
567 /// <param name="s"></param>
568 /// <param name="style"></param>
569 /// <returns></returns>
570 public static System
.Single
Parse(System
.String s
, System
.Globalization
.NumberStyles style
)
574 if (s
.EndsWith("f") || s
.EndsWith("F"))
575 return System
.Single
.Parse(s
.Substring(0, s
.Length
- 1), style
);
577 return System
.Single
.Parse(s
, style
);
579 catch(System
.FormatException fex
)
588 /// <param name="s"></param>
589 /// <returns></returns>
590 public static System
.Single
Parse(System
.String s
)
594 if (s
.EndsWith("f") || s
.EndsWith("F"))
595 return System
.Single
.Parse(s
.Substring(0, s
.Length
- 1));
597 return System
.Single
.Parse(s
);
599 catch(System
.FormatException fex
)
609 public class AppSettings
614 /// <param name="key"></param>
615 /// <param name="defValue"></param>
616 /// <returns></returns>
617 public static int Get(System
.String key
, int defValue
)
619 System
.String theValue
= System
.Configuration
.ConfigurationSettings
.AppSettings
.Get(key
);
620 if (theValue
== null)
624 return System
.Convert
.ToInt16(theValue
.Trim());
630 /// <param name="key"></param>
631 /// <param name="defValue"></param>
632 /// <returns></returns>
633 public static long Get(System
.String key
, long defValue
)
635 System
.String theValue
= System
.Configuration
.ConfigurationSettings
.AppSettings
.Get(key
);
636 if (theValue
== null)
640 return System
.Convert
.ToInt32(theValue
.Trim());
646 /// <param name="key"></param>
647 /// <param name="defValue"></param>
648 /// <returns></returns>
649 public static System
.String
Get(System
.String key
, System
.String defValue
)
651 System
.String theValue
= System
.Configuration
.ConfigurationSettings
.AppSettings
.Get(key
);
652 if (theValue
== null)
660 public static System
.Collections
.SortedList
TailMap(System
.Collections
.SortedList list
, System
.Object limit
)
662 System
.Collections
.Comparer comparer
= System
.Collections
.Comparer
.Default
;
663 System
.Collections
.SortedList newList
= new System
.Collections
.SortedList();
670 while (comparer
.Compare(list
.GetKey(index
), limit
) < 0)
673 for (; index
< list
.Count
; index
++)
674 newList
.Add(list
.GetKey(index
), list
[list
.GetKey(index
)]);
682 /// Use for .NET 1.1 Framework only.
684 public class CompressionSupport
686 public interface ICompressionAdapter
688 byte[] Compress(byte[] input
);
689 byte[] Uncompress(byte[] input
);
692 private static ICompressionAdapter compressionAdapter
;
694 public static byte[] Uncompress(byte[] input
)
696 CheckCompressionSupport();
697 return compressionAdapter
.Uncompress(input
);
700 public static byte[] Compress(byte[] input
)
702 CheckCompressionSupport();
703 return compressionAdapter
.Compress(input
);
706 private static void CheckCompressionSupport()
708 if (compressionAdapter
== null)
710 System
.String compressionLibClassName
= SupportClass
.AppSettings
.Get("Lucene.Net.CompressionLib.class", null);
711 if (compressionLibClassName
== null)
712 throw new System
.SystemException("Compression support not configured");
714 Type compressionLibClass
= Type
.GetType(compressionLibClassName
, true);
715 System
.Object compressionAdapterObj
= Activator
.CreateInstance(compressionLibClass
);
716 compressionAdapter
= compressionAdapterObj
as ICompressionAdapter
;
717 if (compressionAdapter
== null)
718 throw new System
.SystemException("Compression adapter does not support the ICompressionAdapter interface");