Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Lucene.Net / SupportClass.cs
blob969c098054a369cbc1d4c4097459cf009d9c6e9a
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.
16 using System;
18 /// <summary>
19 /// This interface should be implemented by any class whose instances are intended
20 /// to be executed by a thread.
21 /// </summary>
22 public interface IThreadRunnable
24 /// <summary>
25 /// This method has to be implemented in order that starting of the thread causes the object's
26 /// run method to be called in that separately executing thread.
27 /// </summary>
28 void Run();
31 /// <summary>
32 /// Contains conversion support elements such as classes, interfaces and static methods.
33 /// </summary>
34 public class SupportClass
36 /// <summary>
37 /// Support class used to handle threads
38 /// </summary>
39 public class ThreadClass : IThreadRunnable
41 /// <summary>
42 /// The instance of System.Threading.Thread
43 /// </summary>
44 private System.Threading.Thread threadField;
46 /// <summary>
47 /// Initializes a new instance of the ThreadClass class
48 /// </summary>
49 public ThreadClass()
51 threadField = new System.Threading.Thread(new System.Threading.ThreadStart(Run));
54 /// <summary>
55 /// Initializes a new instance of the Thread class.
56 /// </summary>
57 /// <param name="Name">The name of the thread</param>
58 public ThreadClass(System.String Name)
60 threadField = new System.Threading.Thread(new System.Threading.ThreadStart(Run));
61 this.Name = Name;
64 /// <summary>
65 /// Initializes a new instance of the Thread class.
66 /// </summary>
67 /// <param name="Start">A ThreadStart delegate that references the methods to be invoked when this thread begins executing</param>
68 public ThreadClass(System.Threading.ThreadStart Start)
70 threadField = new System.Threading.Thread(Start);
73 /// <summary>
74 /// Initializes a new instance of the Thread class.
75 /// </summary>
76 /// <param name="Start">A ThreadStart delegate that references the methods to be invoked when this thread begins executing</param>
77 /// <param name="Name">The name of the thread</param>
78 public ThreadClass(System.Threading.ThreadStart Start, System.String Name)
80 threadField = new System.Threading.Thread(Start);
81 this.Name = Name;
84 /// <summary>
85 /// This method has no functionality unless the method is overridden
86 /// </summary>
87 public virtual void Run()
91 /// <summary>
92 /// Causes the operating system to change the state of the current thread instance to ThreadState.Running
93 /// </summary>
94 public virtual void Start()
96 threadField.Start();
99 /// <summary>
100 /// Interrupts a thread that is in the WaitSleepJoin thread state
101 /// </summary>
102 public virtual void Interrupt()
104 threadField.Interrupt();
107 /// <summary>
108 /// Gets the current thread instance
109 /// </summary>
110 public System.Threading.Thread Instance
114 return threadField;
118 threadField = value;
122 /// <summary>
123 /// Gets or sets the name of the thread
124 /// </summary>
125 public System.String Name
129 return threadField.Name;
133 if (threadField.Name == null)
134 threadField.Name = value;
138 /// <summary>
139 /// Gets or sets a value indicating the scheduling priority of a thread
140 /// </summary>
141 public System.Threading.ThreadPriority Priority
145 return threadField.Priority;
149 threadField.Priority = value;
153 /// <summary>
154 /// Gets a value indicating the execution status of the current thread
155 /// </summary>
156 public bool IsAlive
160 return threadField.IsAlive;
164 /// <summary>
165 /// Gets or sets a value indicating whether or not a thread is a background thread.
166 /// </summary>
167 public bool IsBackground
171 return threadField.IsBackground;
175 threadField.IsBackground = value;
179 /// <summary>
180 /// Blocks the calling thread until a thread terminates
181 /// </summary>
182 public void Join()
184 threadField.Join();
187 /// <summary>
188 /// Blocks the calling thread until a thread terminates or the specified time elapses
189 /// </summary>
190 /// <param name="MiliSeconds">Time of wait in milliseconds</param>
191 public void Join(long MiliSeconds)
193 lock(this)
195 threadField.Join(new System.TimeSpan(MiliSeconds * 10000));
199 /// <summary>
200 /// Blocks the calling thread until a thread terminates or the specified time elapses
201 /// </summary>
202 /// <param name="MiliSeconds">Time of wait in milliseconds</param>
203 /// <param name="NanoSeconds">Time of wait in nanoseconds</param>
204 public void Join(long MiliSeconds, int NanoSeconds)
206 lock(this)
208 threadField.Join(new System.TimeSpan(MiliSeconds * 10000 + NanoSeconds * 100));
212 /// <summary>
213 /// Resumes a thread that has been suspended
214 /// </summary>
215 public void Resume()
217 threadField.Resume();
220 /// <summary>
221 /// Raises a ThreadAbortException in the thread on which it is invoked,
222 /// to begin the process of terminating the thread. Calling this method
223 /// usually terminates the thread
224 /// </summary>
225 public void Abort()
227 threadField.Abort();
230 /// <summary>
231 /// Raises a ThreadAbortException in the thread on which it is invoked,
232 /// to begin the process of terminating the thread while also providing
233 /// exception information about the thread termination.
234 /// Calling this method usually terminates the thread.
235 /// </summary>
236 /// <param name="stateInfo">An object that contains application-specific information, such as state, which can be used by the thread being aborted</param>
237 public void Abort(System.Object stateInfo)
239 lock(this)
241 threadField.Abort(stateInfo);
245 /// <summary>
246 /// Suspends the thread, if the thread is already suspended it has no effect
247 /// </summary>
248 public void Suspend()
250 threadField.Suspend();
253 /// <summary>
254 /// Obtain a String that represents the current Object
255 /// </summary>
256 /// <returns>A String that represents the current Object</returns>
257 public override System.String ToString()
259 return "Thread[" + Name + "," + Priority.ToString() + "," + "" + "]";
262 /// <summary>
263 /// Gets the currently running thread
264 /// </summary>
265 /// <returns>The currently running thread</returns>
266 public static ThreadClass Current()
268 ThreadClass CurrentThread = new ThreadClass();
269 CurrentThread.Instance = System.Threading.Thread.CurrentThread;
270 return CurrentThread;
274 /// <summary>
275 /// A simple class for number conversions.
276 /// </summary>
277 public class Number
279 /// <summary>
280 /// Min radix value.
281 /// </summary>
282 public const int MIN_RADIX = 2;
283 /// <summary>
284 /// Max radix value.
285 /// </summary>
286 public const int MAX_RADIX = 36;
288 private const System.String digits = "0123456789abcdefghijklmnopqrstuvwxyz";
290 public static System.String ToString(float f)
292 if (((float)(int)f) == f)
294 return ((int)f).ToString() + ".0";
296 else
298 return f.ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
302 /// <summary>
303 /// Converts a number to System.String in the specified radix.
304 /// </summary>
305 /// <param name="i">A number to be converted.</param>
306 /// <param name="radix">A radix.</param>
307 /// <returns>A System.String representation of the number in the specified redix.</returns>
308 public static System.String ToString(long i, int radix)
310 if (radix < MIN_RADIX || radix > MAX_RADIX)
311 radix = 10;
313 char[] buf = new char[65];
314 int charPos = 64;
315 bool negative = (i < 0);
317 if (!negative)
319 i = -i;
322 while (i <= -radix)
324 buf[charPos--] = digits[(int)(-(i % radix))];
325 i = i / radix;
327 buf[charPos] = digits[(int)(-i)];
329 if (negative)
331 buf[--charPos] = '-';
334 return new System.String(buf, charPos, (65 - charPos));
337 /// <summary>
338 /// Parses a number in the specified radix.
339 /// </summary>
340 /// <param name="s">An input System.String.</param>
341 /// <param name="radix">A radix.</param>
342 /// <returns>The parsed number in the specified radix.</returns>
343 public static long Parse(System.String s, int radix)
345 if (s == null)
347 throw new ArgumentException("null");
350 if (radix < MIN_RADIX)
352 throw new NotSupportedException("radix " + radix +
353 " less than Number.MIN_RADIX");
355 if (radix > MAX_RADIX)
357 throw new NotSupportedException("radix " + radix +
358 " greater than Number.MAX_RADIX");
361 long result = 0;
362 long mult = 1;
364 s = s.ToLower();
366 for (int i = s.Length - 1; i >= 0; i--)
368 int weight = digits.IndexOf(s[i]);
369 if (weight == -1)
370 throw new FormatException("Invalid number for the specified radix");
372 result += (weight * mult);
373 mult *= radix;
376 return result;
380 /// <summary>
381 /// Mimics Java's Character class.
382 /// </summary>
383 public class Character
385 private const char charNull= '\0';
386 private const char charZero = '0';
387 private const char charA = 'a';
389 /// <summary>
390 /// </summary>
391 public static int MAX_RADIX
395 return 36;
399 /// <summary>
400 /// </summary>
401 public static int MIN_RADIX
405 return 2;
409 /// <summary>
410 ///
411 /// </summary>
412 /// <param name="digit"></param>
413 /// <param name="radix"></param>
414 /// <returns></returns>
415 public static char ForDigit(int digit, int radix)
417 // if radix or digit is out of range,
418 // return the null character.
419 if (radix < Character.MIN_RADIX)
420 return charNull;
421 if (radix > Character.MAX_RADIX)
422 return charNull;
423 if (digit < 0)
424 return charNull;
425 if (digit >= radix)
426 return charNull;
428 // if digit is less than 10,
429 // return '0' plus digit
430 if (digit < 10)
431 return (char) ( (int) charZero + digit);
433 // otherwise, return 'a' plus digit.
434 return (char) ((int) charA + digit - 10);
438 /// <summary>
439 ///
440 /// </summary>
441 public class Date
443 /// <summary>
444 ///
445 /// </summary>
446 /// <param name="dateTime"></param>
447 /// <returns></returns>
448 static public long GetTime(DateTime dateTime)
450 TimeSpan ts = dateTime.Subtract(new DateTime(1970, 1, 1));
451 ts = ts.Subtract(TimeZone.CurrentTimeZone.GetUtcOffset(dateTime));
452 return ts.Ticks / TimeSpan.TicksPerMillisecond;
456 /// <summary>
457 ///
458 /// </summary>
459 public class Single
461 /// <summary>
462 ///
463 /// </summary>
464 /// <param name="s"></param>
465 /// <param name="style"></param>
466 /// <param name="provider"></param>
467 /// <returns></returns>
468 public static System.Single Parse(System.String s, System.Globalization.NumberStyles style, System.IFormatProvider provider)
472 if (s.EndsWith("f") || s.EndsWith("F"))
473 return System.Single.Parse(s.Substring(0, s.Length - 1), style, provider);
474 else
475 return System.Single.Parse(s, style, provider);
477 catch (System.FormatException fex)
479 throw fex;
483 /// <summary>
484 ///
485 /// </summary>
486 /// <param name="s"></param>
487 /// <param name="provider"></param>
488 /// <returns></returns>
489 public static System.Single Parse(System.String s, System.IFormatProvider provider)
493 if (s.EndsWith("f") || s.EndsWith("F"))
494 return System.Single.Parse(s.Substring(0, s.Length - 1), provider);
495 else
496 return System.Single.Parse(s, provider);
498 catch (System.FormatException fex)
500 throw fex;
504 /// <summary>
505 ///
506 /// </summary>
507 /// <param name="s"></param>
508 /// <param name="style"></param>
509 /// <returns></returns>
510 public static System.Single Parse(System.String s, System.Globalization.NumberStyles style)
514 if (s.EndsWith("f") || s.EndsWith("F"))
515 return System.Single.Parse(s.Substring(0, s.Length - 1), style);
516 else
517 return System.Single.Parse(s, style);
519 catch(System.FormatException fex)
521 throw fex;
525 /// <summary>
526 ///
527 /// </summary>
528 /// <param name="s"></param>
529 /// <returns></returns>
530 public static System.Single Parse(System.String s)
534 if (s.EndsWith("f") || s.EndsWith("F"))
535 return System.Single.Parse(s.Substring(0, s.Length - 1));
536 else
537 return System.Single.Parse(s);
539 catch(System.FormatException fex)
541 throw fex;
546 /// <summary>
547 ///
548 /// </summary>
549 public class AppSettings
551 /// <summary>
552 ///
553 /// </summary>
554 /// <param name="key"></param>
555 /// <param name="defValue"></param>
556 /// <returns></returns>
557 public static int Get(System.String key, int defValue)
559 System.String theValue = System.Configuration.ConfigurationSettings.AppSettings.Get(key);
560 if (theValue == null)
562 return defValue;
564 return System.Convert.ToInt16(theValue.Trim());
567 /// <summary>
568 ///
569 /// </summary>
570 /// <param name="key"></param>
571 /// <param name="defValue"></param>
572 /// <returns></returns>
573 public static long Get(System.String key, long defValue)
575 System.String theValue = System.Configuration.ConfigurationSettings.AppSettings.Get(key);
576 if (theValue == null)
578 return defValue;
580 return System.Convert.ToInt32(theValue.Trim());
583 /// <summary>
584 ///
585 /// </summary>
586 /// <param name="key"></param>
587 /// <param name="defValue"></param>
588 /// <returns></returns>
589 public static System.String Get(System.String key, System.String defValue)
591 System.String theValue = System.Configuration.ConfigurationSettings.AppSettings.Get(key);
592 if (theValue == null)
594 return defValue;
596 return theValue;