cvsimport
[beagle.git] / beagled / Lucene.Net / SupportClass.cs
blob0a7f7db0ada5af375bb723ee3037f36a1800cd1b
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
17 using System;
19 /// <summary>
20 /// This interface should be implemented by any class whose instances are intended
21 /// to be executed by a thread.
22 /// </summary>
23 public interface IThreadRunnable
25 /// <summary>
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.
28 /// </summary>
29 void Run();
32 /// <summary>
33 /// Contains conversion support elements such as classes, interfaces and static methods.
34 /// </summary>
35 public class SupportClass
37 /// <summary>
38 /// Support class used to handle threads
39 /// </summary>
40 public class ThreadClass : IThreadRunnable
42 /// <summary>
43 /// The instance of System.Threading.Thread
44 /// </summary>
45 private System.Threading.Thread threadField;
47 /// <summary>
48 /// Initializes a new instance of the ThreadClass class
49 /// </summary>
50 public ThreadClass()
52 threadField = new System.Threading.Thread(new System.Threading.ThreadStart(Run));
55 /// <summary>
56 /// Initializes a new instance of the Thread class.
57 /// </summary>
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));
62 this.Name = Name;
65 /// <summary>
66 /// Initializes a new instance of the Thread class.
67 /// </summary>
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);
74 /// <summary>
75 /// Initializes a new instance of the Thread class.
76 /// </summary>
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);
82 this.Name = Name;
85 /// <summary>
86 /// This method has no functionality unless the method is overridden
87 /// </summary>
88 public virtual void Run()
92 /// <summary>
93 /// Causes the operating system to change the state of the current thread instance to ThreadState.Running
94 /// </summary>
95 public virtual void Start()
97 threadField.Start();
100 /// <summary>
101 /// Interrupts a thread that is in the WaitSleepJoin thread state
102 /// </summary>
103 public virtual void Interrupt()
105 threadField.Interrupt();
108 /// <summary>
109 /// Gets the current thread instance
110 /// </summary>
111 public System.Threading.Thread Instance
115 return threadField;
119 threadField = value;
123 /// <summary>
124 /// Gets or sets the name of the thread
125 /// </summary>
126 public System.String Name
130 return threadField.Name;
134 if (threadField.Name == null)
135 threadField.Name = value;
139 /// <summary>
140 /// Gets or sets a value indicating the scheduling priority of a thread
141 /// </summary>
142 public System.Threading.ThreadPriority Priority
146 return threadField.Priority;
150 threadField.Priority = value;
154 /// <summary>
155 /// Gets a value indicating the execution status of the current thread
156 /// </summary>
157 public bool IsAlive
161 return threadField.IsAlive;
165 /// <summary>
166 /// Gets or sets a value indicating whether or not a thread is a background thread.
167 /// </summary>
168 public bool IsBackground
172 return threadField.IsBackground;
176 threadField.IsBackground = value;
180 /// <summary>
181 /// Blocks the calling thread until a thread terminates
182 /// </summary>
183 public void Join()
185 threadField.Join();
188 /// <summary>
189 /// Blocks the calling thread until a thread terminates or the specified time elapses
190 /// </summary>
191 /// <param name="MiliSeconds">Time of wait in milliseconds</param>
192 public void Join(long MiliSeconds)
194 lock(this)
196 threadField.Join(new System.TimeSpan(MiliSeconds * 10000));
200 /// <summary>
201 /// Blocks the calling thread until a thread terminates or the specified time elapses
202 /// </summary>
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)
207 lock(this)
209 threadField.Join(new System.TimeSpan(MiliSeconds * 10000 + NanoSeconds * 100));
213 /// <summary>
214 /// Resumes a thread that has been suspended
215 /// </summary>
216 public void Resume()
218 threadField.Resume();
221 /// <summary>
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
225 /// </summary>
226 public void Abort()
228 threadField.Abort();
231 /// <summary>
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.
236 /// </summary>
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)
240 lock(this)
242 threadField.Abort(stateInfo);
246 /// <summary>
247 /// Suspends the thread, if the thread is already suspended it has no effect
248 /// </summary>
249 public void Suspend()
251 threadField.Suspend();
254 /// <summary>
255 /// Obtain a String that represents the current Object
256 /// </summary>
257 /// <returns>A String that represents the current Object</returns>
258 public override System.String ToString()
260 return "Thread[" + Name + "," + Priority.ToString() + "," + "" + "]";
263 /// <summary>
264 /// Gets the currently running thread
265 /// </summary>
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;
275 /// <summary>
276 /// Represents the methods to support some operations over files.
277 /// </summary>
278 public class FileSupport
280 /// <summary>
281 /// Returns an array of abstract pathnames representing the files and directories of the specified path.
282 /// </summary>
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]);
293 return result;
295 else
296 return null;
300 /// <summary>
301 /// A simple class for number conversions.
302 /// </summary>
303 public class Number
305 /// <summary>
306 /// Min radix value.
307 /// </summary>
308 public const int MIN_RADIX = 2;
309 /// <summary>
310 /// Max radix value.
311 /// </summary>
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";
322 else
324 return f.ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
328 /// <summary>
329 /// Converts a number to System.String in the specified radix.
330 /// </summary>
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)
337 radix = 10;
339 char[] buf = new char[65];
340 int charPos = 64;
341 bool negative = (i < 0);
343 if (!negative)
345 i = -i;
348 while (i <= -radix)
350 buf[charPos--] = digits[(int)(-(i % radix))];
351 i = i / radix;
353 buf[charPos] = digits[(int)(-i)];
355 if (negative)
357 buf[--charPos] = '-';
360 return new System.String(buf, charPos, (65 - charPos));
363 /// <summary>
364 /// Parses a number in the specified radix.
365 /// </summary>
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)
371 if (s == null)
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");
387 long result = 0;
388 long mult = 1;
390 s = s.ToLower();
392 for (int i = s.Length - 1; i >= 0; i--)
394 int weight = digits.IndexOf(s[i]);
395 if (weight == -1)
396 throw new FormatException("Invalid number for the specified radix");
398 result += (weight * mult);
399 mult *= radix;
402 return result;
405 /// <summary>
406 /// Performs an unsigned bitwise right shift with the specified number
407 /// </summary>
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)
413 if (number >= 0)
414 return number >> bits;
415 else
416 return (number >> bits) + (2 << ~bits);
419 /// <summary>
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.
423 /// </summary>
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++)
431 if (bits[i] == true)
433 return i;
436 return -1;
440 /// <summary>
441 /// Mimics Java's Character class.
442 /// </summary>
443 public class Character
445 private const char charNull= '\0';
446 private const char charZero = '0';
447 private const char charA = 'a';
449 /// <summary>
450 /// </summary>
451 public static int MAX_RADIX
455 return 36;
459 /// <summary>
460 /// </summary>
461 public static int MIN_RADIX
465 return 2;
469 /// <summary>
470 ///
471 /// </summary>
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)
480 return charNull;
481 if (radix > Character.MAX_RADIX)
482 return charNull;
483 if (digit < 0)
484 return charNull;
485 if (digit >= radix)
486 return charNull;
488 // if digit is less than 10,
489 // return '0' plus digit
490 if (digit < 10)
491 return (char) ( (int) charZero + digit);
493 // otherwise, return 'a' plus digit.
494 return (char) ((int) charA + digit - 10);
498 /// <summary>
499 ///
500 /// </summary>
501 public class Date
503 /// <summary>
504 ///
505 /// </summary>
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;
516 /// <summary>
517 ///
518 /// </summary>
519 public class Single
521 /// <summary>
522 ///
523 /// </summary>
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);
534 else
535 return System.Single.Parse(s, style, provider);
537 catch (System.FormatException fex)
539 throw fex;
543 /// <summary>
544 ///
545 /// </summary>
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);
555 else
556 return System.Single.Parse(s, provider);
558 catch (System.FormatException fex)
560 throw fex;
564 /// <summary>
565 ///
566 /// </summary>
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);
576 else
577 return System.Single.Parse(s, style);
579 catch(System.FormatException fex)
581 throw fex;
585 /// <summary>
586 ///
587 /// </summary>
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));
596 else
597 return System.Single.Parse(s);
599 catch(System.FormatException fex)
601 throw fex;
606 /// <summary>
607 ///
608 /// </summary>
609 public class AppSettings
611 /// <summary>
612 ///
613 /// </summary>
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)
622 return defValue;
624 return System.Convert.ToInt16(theValue.Trim());
627 /// <summary>
628 ///
629 /// </summary>
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)
638 return defValue;
640 return System.Convert.ToInt32(theValue.Trim());
643 /// <summary>
644 ///
645 /// </summary>
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)
654 return defValue;
656 return theValue;
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();
665 if (list != null)
667 if (list.Count > 0)
669 int index = 0;
670 while (comparer.Compare(list.GetKey(index), limit) < 0)
671 index++;
673 for (; index < list.Count; index++)
674 newList.Add(list.GetKey(index), list[list.GetKey(index)]);
678 return newList;
681 /// <summary>
682 /// Use for .NET 1.1 Framework only.
683 /// </summary>
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");