/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections;
///
/// This interface should be implemented by any class whose instances are intended
/// to be executed by a thread.
///
public interface IThreadRunnable
{
///
/// 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.
///
void Run();
}
///
/// Contains conversion support elements such as classes, interfaces and static methods.
///
public class SupportClass
{
public interface Checksum
{
void reset();
void update(int b);
void update(byte[] b);
void update(byte[] b, int offset, int length);
Int64 getValue();
}
public class CRC32 : Checksum
{
private static readonly UInt32[] crcTable = InitializeCRCTable();
private static UInt32[] InitializeCRCTable()
{
UInt32[] crcTable = new UInt32[256];
for (UInt32 n = 0; n < 256; n++)
{
UInt32 c = n;
for (int k = 8; --k >= 0; )
{
if ((c & 1) != 0)
c = 0xedb88320 ^ (c >> 1);
else
c = c >> 1;
}
crcTable[n] = c;
}
return crcTable;
}
private UInt32 crc = 0;
public Int64 getValue()
{
return (Int64)crc & 0xffffffffL;
}
public void reset()
{
crc = 0;
}
public void update(int bval)
{
UInt32 c = ~crc;
c = crcTable[(c ^ bval) & 0xff] ^ (c >> 8);
crc = ~c;
}
public void update(byte[] buf, int off, int len)
{
UInt32 c = ~crc;
while (--len >= 0)
c = crcTable[(c ^ buf[off++]) & 0xff] ^ (c >> 8);
crc = ~c;
}
public void update(byte[] buf)
{
update(buf, 0, buf.Length);
}
}
public class TextSupport
{
///
/// Copies an array of chars obtained from a String into a specified array of chars
///
/// The String to get the chars from
/// Position of the String to start getting the chars
/// Position of the String to end getting the chars
/// Array to return the chars
/// Position of the destination array of chars to start storing the chars
/// An array of chars
public static void GetCharsFromString(string sourceString, int sourceStart, int sourceEnd, char[] destinationArray, int destinationStart)
{
int sourceCounter;
int destinationCounter;
sourceCounter = sourceStart;
destinationCounter = destinationStart;
while (sourceCounter < sourceEnd)
{
destinationArray[destinationCounter] = (char)sourceString[sourceCounter];
sourceCounter++;
destinationCounter++;
}
}
}
public class CollectionsSupport
{
public class BitSet
{
private System.Collections.BitArray bitArray = null;
public BitSet()
: this(0)
{
}
public BitSet(int size)
{
bitArray = new System.Collections.BitArray(size, false);
}
public void Set(int index)
{
if (index >= bitArray.Count)
GrowBitArray(index + 1);
bitArray.Set(index, true);
}
public int Cardinality()
{
int cardinality = 0;
for (int i = 0; i < bitArray.Length; i++)
if (bitArray.Get(i))
cardinality++;
return cardinality;
}
///
/// Returns the next set bit at or index, or -1 if no such bit exists.
///
/// the index of the bit at which to start checking
/// the next set bit or -1
public int NextSetBit(int index)
{
while (index < bitArray.Count)
{
// if index bit is set, return it; otherwise check next index bit
if (bitArray.Get(index))
return index;
else
index++;
}
// if no bits are set at or after index, return -1
return -1;
}
private void GrowBitArray(int size)
{
//TODO:
// might be able to change this to:
bitArray.Length = size;
//System.Collections.BitArray tmp = new System.Collections.BitArray(size, false);
//for (int i = 0; i < bitArray.Count; i++)
// tmp.Set(i, bitArray.Get(i));
//bitArray = tmp;
}
}
public static void ArrayFill(object[] array, object fillValue)
{
ArrayFill(array, 0, array.Length, fillValue);
}
public static void ArrayFill(object[] array, int from, int to, object fillValue)
{
for (int i = from; i < to; i++)
array[i] = fillValue;
}
public static void ArrayFill(byte[] array, byte fillValue)
{
ArrayFill(array, 0, array.Length, fillValue);
}
public static void ArrayFill(byte[] array, int from, int to, byte fillValue)
{
for (int i = from; i < to; i++)
array[i] = fillValue;
}
public static void ArrayFill(char[] array, char fillValue)
{
ArrayFill(array, 0, array.Length, fillValue);
}
public static void ArrayFill(char[] array, int from, int to, char fillValue)
{
for (int i = from; i < to; i++)
array[i] = fillValue;
}
public static void ArrayFill(int[] array, int fillValue)
{
ArrayFill(array, 0, array.Length, fillValue);
}
public static void ArrayFill(int[] array, int from, int to, int fillValue)
{
for (int i = from; i < to; i++)
array[i] = fillValue;
}
public static void ArrayFill(long[] array, long fillValue)
{
ArrayFill(array, 0, array.Length, fillValue);
}
public static void ArrayFill(long[] array, int from, int to, long fillValue)
{
for (int i = from; i < to; i++)
array[i] = fillValue;
}
public static void AddAll(System.Collections.Generic.ICollection source, System.Collections.Generic.ICollection destination)
{
System.Collections.Generic.IEnumerator enumerator = source.GetEnumerator();
while (enumerator.MoveNext())
destination.Add(enumerator.Current);
}
public static void AddAll(System.Collections.Generic.ICollection source, System.Collections.Generic.ICollection destination)
{
System.Collections.Generic.IEnumerator enumerator = source.GetEnumerator();
while (enumerator.MoveNext())
destination.Add(enumerator.Current);
}
public static void AddAll(System.Collections.Generic.IList source, System.Collections.Generic.IList destination)
{
System.Collections.Generic.IEnumerator enumerator = source.GetEnumerator();
while (enumerator.MoveNext())
destination.Add(enumerator.Current);
}
public static System.Collections.Generic.SortedDictionary TailMap(System.Collections.Generic.SortedDictionary map, string fromKey)
{
System.Collections.Generic.SortedDictionary tailMap;
if (map.Comparer != null)
tailMap = new System.Collections.Generic.SortedDictionary(map.Comparer);
else
tailMap = new System.Collections.Generic.SortedDictionary();
if (map != null && map.Count > 0)
{
System.Collections.Generic.IEnumerator> e = map.GetEnumerator();
if (map.Comparer != null)
while (e.MoveNext())
{
if (map.Comparer.Compare(fromKey, e.Current.Key) <= 0)
tailMap[e.Current.Key] = e.Current.Value;
}
else
while (e.MoveNext())
{
if (string.CompareOrdinal(fromKey, e.Current.Key) <= 0)
tailMap[e.Current.Key] = e.Current.Value;
}
}
return tailMap;
}
public static void PutAll(System.Collections.IDictionary source, System.Collections.IDictionary destination)
{
// using destination[key] = source[key] avoids exceptions on duplicate, and
// preserves the most recent duplicate key, which is semantically equivalent
// to the java.util.Map functionality
System.Collections.IEnumerator enumerator = source.Keys.GetEnumerator();
while (enumerator.MoveNext())
destination[enumerator.Current] = source[enumerator.Current];
}
public static void PutAll(System.Collections.Generic.IDictionary source, System.Collections.Generic.IDictionary destination)
{
// using destination[key] = source[key] avoids exceptions on duplicate, and
// preserves the most recent duplicate key, which is semantically equivalent
// to the java.util.Map functionality
System.Collections.Generic.IEnumerator enumerator = source.Keys.GetEnumerator();
while (enumerator.MoveNext())
destination[enumerator.Current] = source[enumerator.Current];
}
}
///
/// Support class used to handle threads
///
public class ThreadClass : IThreadRunnable
{
///
/// The instance of System.Threading.Thread
///
private System.Threading.Thread threadField;
///
/// Initializes a new instance of the ThreadClass class
///
public ThreadClass()
{
threadField = new System.Threading.Thread(new System.Threading.ThreadStart(Run));
This = this;
}
///
/// Initializes a new instance of the Thread class.
///
/// The name of the thread
public ThreadClass(System.String Name)
{
threadField = new System.Threading.Thread(new System.Threading.ThreadStart(Run));
this.Name = Name;
This = this;
}
///
/// Initializes a new instance of the Thread class.
///
/// A ThreadStart delegate that references the methods to be invoked when this thread begins executing
public ThreadClass(System.Threading.ThreadStart Start)
{
threadField = new System.Threading.Thread(Start);
This = this;
}
///
/// Initializes a new instance of the Thread class.
///
/// A ThreadStart delegate that references the methods to be invoked when this thread begins executing
/// The name of the thread
public ThreadClass(System.Threading.ThreadStart Start, System.String Name)
{
threadField = new System.Threading.Thread(Start);
this.Name = Name;
This = this;
}
///
/// This method has no functionality unless the method is overridden
///
public virtual void Run()
{
}
///
/// Causes the operating system to change the state of the current thread instance to ThreadState.Running
///
public virtual void Start()
{
threadField.Start();
if (This == null)
{
This = this;
This.Instance = threadField;
}
}
///
/// Interrupts a thread that is in the WaitSleepJoin thread state
///
public virtual void Interrupt()
{
threadField.Interrupt();
}
///
/// Gets the current thread instance
///
public System.Threading.Thread Instance
{
get
{
return threadField;
}
set
{
threadField = value;
}
}
///
/// Gets or sets the name of the thread
///
public System.String Name
{
get
{
return threadField.Name;
}
set
{
if (threadField.Name == null)
threadField.Name = value;
}
}
public void SetDaemon(bool isDaemon)
{
threadField.IsBackground = isDaemon;
}
///
/// Gets or sets a value indicating the scheduling priority of a thread
///
public System.Threading.ThreadPriority Priority
{
get
{
try
{
return threadField.Priority;
}
catch
{
return System.Threading.ThreadPriority.Normal;
}
}
set
{
try
{
threadField.Priority = value;
}
catch{}
}
}
///
/// Gets a value indicating the execution status of the current thread
///
public bool IsAlive
{
get
{
return threadField.IsAlive;
}
}
///
/// Gets or sets a value indicating whether or not a thread is a background thread.
///
public bool IsBackground
{
get
{
return threadField.IsBackground;
}
set
{
threadField.IsBackground = value;
}
}
///
/// Blocks the calling thread until a thread terminates
///
public void Join()
{
lock (this)
{
threadField.Join();
}
}
///
/// Blocks the calling thread until a thread terminates or the specified time elapses
///
/// Time of wait in milliseconds
public void Join(long MiliSeconds)
{
lock (this)
{
threadField.Join(new System.TimeSpan(MiliSeconds * 10000));
}
}
///
/// Blocks the calling thread until a thread terminates or the specified time elapses
///
/// Time of wait in milliseconds
/// Time of wait in nanoseconds
public void Join(long MiliSeconds, int NanoSeconds)
{
lock (this)
{
threadField.Join(new System.TimeSpan(MiliSeconds * 10000 + NanoSeconds * 100));
}
}
///
/// Resumes a thread that has been suspended
///
public void Resume()
{
System.Threading.Monitor.PulseAll(threadField);
}
///
/// 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
///
public void Abort()
{
threadField.Abort();
}
///
/// 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.
///
/// An object that contains application-specific information, such as state, which can be used by the thread being aborted
public void Abort(object stateInfo)
{
lock (this)
{
threadField.Abort(stateInfo);
}
}
///
/// Suspends the thread, if the thread is already suspended it has no effect
///
public void Suspend()
{
System.Threading.Monitor.Wait(threadField);
}
///
/// Obtain a String that represents the current object
///
/// A String that represents the current object
public override System.String ToString()
{
return "Thread[" + Name + "," + Priority.ToString() + "," + "" + "]";
}
[ThreadStatic]
static ThreadClass This = null;
// named as the Java version
public static ThreadClass CurrentThread()
{
return Current();
}
public static void Sleep(long ms)
{
// casting long ms to int ms could lose resolution, however unlikely
// that someone would want to sleep for that long...
System.Threading.Thread.Sleep((int)ms);
}
///
/// Gets the currently running thread
///
/// The currently running thread
public static ThreadClass Current()
{
if (This == null)
{
This = new ThreadClass();
This.Instance = System.Threading.Thread.CurrentThread;
}
return This;
}
}
///
/// Represents the methods to support some operations over files.
///
public class FileSupport
{
///
/// Returns an array of abstract pathnames representing the files and directories of the specified path.
///
/// The abstract pathname to list it childs.
/// An array of abstract pathnames childs of the path specified or null if the path is not a directory
public static System.IO.FileInfo[] GetFiles(System.IO.FileInfo path)
{
if ((path.Attributes & System.IO.FileAttributes.Directory) > 0)
{
String[] fullpathnames = System.IO.Directory.GetFileSystemEntries(path.FullName);
System.IO.FileInfo[] result = new System.IO.FileInfo[fullpathnames.Length];
for (int i = 0; i < result.Length ; i++)
result[i] = new System.IO.FileInfo(fullpathnames[i]);
return result;
}
else
return null;
}
///
/// Returns a list of files in a give directory.
///
/// The full path name to the directory.
///
/// An array containing the files.
public static System.String[] GetLuceneIndexFiles(System.String fullName,
Lucene.Net.Index.IndexFileNameFilter indexFileNameFilter)
{
System.IO.DirectoryInfo dInfo = new System.IO.DirectoryInfo(fullName);
System.Collections.ArrayList list = new System.Collections.ArrayList();
foreach (System.IO.FileInfo fInfo in dInfo.GetFiles())
{
if (indexFileNameFilter.Accept(fInfo, fInfo.Name) == true)
{
list.Add(fInfo.Name);
}
}
System.String[] retFiles = new System.String[list.Count];
list.CopyTo(retFiles);
return retFiles;
}
}
///
/// A simple class for number conversions.
///
public class Number
{
///
/// Min radix value.
///
public const int MIN_RADIX = 2;
///
/// Max radix value.
///
public const int MAX_RADIX = 36;
private const System.String digits = "0123456789abcdefghijklmnopqrstuvwxyz";
///
/// Converts a number to System.String.
///
///
///
public static System.String ToString(long number)
{
System.Text.StringBuilder s = new System.Text.StringBuilder();
if (number == 0)
{
s.Append("0");
}
else
{
if (number < 0)
{
s.Append("-");
number = -number;
}
while (number > 0)
{
char c = digits[(int)number % 36];
s.Insert(0, c);
number = number / 36;
}
}
return s.ToString();
}
///
/// Converts a number to System.String.
///
///
///
public static System.String ToString(float f)
{
if (((float)(int)f) == f)
{
return ((int)f).ToString() + ".0";
}
else
{
return f.ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
}
}
///
/// Converts a number to System.String in the specified radix.
///
/// A number to be converted.
/// A radix.
/// A System.String representation of the number in the specified redix.
public static System.String ToString(long i, int radix)
{
if (radix < MIN_RADIX || radix > MAX_RADIX)
radix = 10;
char[] buf = new char[65];
int charPos = 64;
bool negative = (i < 0);
if (!negative)
{
i = -i;
}
while (i <= -radix)
{
buf[charPos--] = digits[(int)(-(i % radix))];
i = i / radix;
}
buf[charPos] = digits[(int)(-i)];
if (negative)
{
buf[--charPos] = '-';
}
return new System.String(buf, charPos, (65 - charPos));
}
///
/// Parses a number in the specified radix.
///
/// An input System.String.
/// A radix.
/// The parsed number in the specified radix.
public static long Parse(System.String s, int radix)
{
if (s == null)
{
throw new ArgumentException("null");
}
if (radix < MIN_RADIX)
{
throw new NotSupportedException("radix " + radix +
" less than Number.MIN_RADIX");
}
if (radix > MAX_RADIX)
{
throw new NotSupportedException("radix " + radix +
" greater than Number.MAX_RADIX");
}
long result = 0;
long mult = 1;
s = s.ToLower();
for (int i = s.Length - 1; i >= 0; i--)
{
int weight = digits.IndexOf(s[i]);
if (weight == -1)
throw new FormatException("Invalid number for the specified radix");
result += (weight * mult);
mult *= radix;
}
return result;
}
///
/// Performs an unsigned bitwise right shift with the specified number
///
/// Number to operate on
/// Ammount of bits to shift
/// The resulting number from the shift operation
public static int URShift(int number, int bits)
{
if (number >= 0)
return number >> bits;
else
return (number >> bits) + (2 << ~bits);
}
///
/// Performs an unsigned bitwise right shift with the specified number
///
/// Number to operate on
/// Ammount of bits to shift
/// The resulting number from the shift operation
public static long URShift(long number, int bits)
{
if (number >= 0)
return number >> bits;
else
return (number >> bits) + (2 << ~bits);
}
///
/// Returns the index of the first bit that is set to true that occurs
/// on or after the specified starting index. If no such bit exists
/// then -1 is returned.
///
/// The BitArray object.
/// The index to start checking from (inclusive).
/// The index of the next set bit.
public static int NextSetBit(System.Collections.BitArray bits, int fromIndex)
{
for (int i = fromIndex; i < bits.Length; i++)
{
if (bits[i] == true)
{
return i;
}
}
return -1;
}
///
/// Returns the number of bits set to true in this BitSet.
///
/// The BitArray object.
/// The number of bits set to true in this BitSet.
public static int Cardinality(System.Collections.BitArray bits)
{
int count = 0;
for (int i = 0; i < bits.Count; i++)
{
if (bits[i])
count++;
}
return count;
}
///
/// Converts a System.String number to long.
///
///
///
public static long ToInt64(System.String s)
{
long number = 0;
int factor;
// handle negative number
if (s.StartsWith("-"))
{
s = s.Substring(1);
factor = -1;
}
else
{
factor = 1;
}
// generate number
for (int i = s.Length - 1; i > -1; i--)
{
int n = digits.IndexOf(s[i]);
// not supporting fractional or scientific notations
if (n < 0)
throw new System.ArgumentException("Invalid or unsupported character in number: " + s[i]);
number += (n * factor);
factor *= 36;
}
return number;
}
}
///
/// Mimics Java's Character class.
///
public class Character
{
private const char charNull= '\0';
private const char charZero = '0';
private const char charA = 'a';
///
///
public static int MAX_RADIX
{
get
{
return 36;
}
}
///
///
public static int MIN_RADIX
{
get
{
return 2;
}
}
///
///
///
///
///
///
public static char ForDigit(int digit, int radix)
{
// if radix or digit is out of range,
// return the null character.
if (radix < Character.MIN_RADIX)
return charNull;
if (radix > Character.MAX_RADIX)
return charNull;
if (digit < 0)
return charNull;
if (digit >= radix)
return charNull;
// if digit is less than 10,
// return '0' plus digit
if (digit < 10)
return (char) ( (int) charZero + digit);
// otherwise, return 'a' plus digit.
return (char) ((int) charA + digit - 10);
}
}
///
///
///
public class Date
{
///
///
///
///
///
static public long GetTime(DateTime dateTime)
{
TimeSpan ts = dateTime.Subtract(new DateTime(1970, 1, 1));
ts = ts.Subtract(TimeZone.CurrentTimeZone.GetUtcOffset(dateTime));
return ts.Ticks / TimeSpan.TicksPerMillisecond;
}
}
///
///
///
public class Single
{
///
///
///
///
///
///
///
public static System.Single Parse(System.String s, System.Globalization.NumberStyles style, System.IFormatProvider provider)
{
try
{
if (s.EndsWith("f") || s.EndsWith("F"))
return System.Single.Parse(s.Substring(0, s.Length - 1), style, provider);
else
return System.Single.Parse(s, style, provider);
}
catch (System.FormatException fex)
{
throw fex;
}
}
///
///
///
///
///
///
public static System.Single Parse(System.String s, System.IFormatProvider provider)
{
try
{
if (s.EndsWith("f") || s.EndsWith("F"))
return System.Single.Parse(s.Substring(0, s.Length - 1), provider);
else
return System.Single.Parse(s, provider);
}
catch (System.FormatException fex)
{
throw fex;
}
}
///
///
///
///
///
///
public static System.Single Parse(System.String s, System.Globalization.NumberStyles style)
{
try
{
if (s.EndsWith("f") || s.EndsWith("F"))
return System.Single.Parse(s.Substring(0, s.Length - 1), style);
else
return System.Single.Parse(s, style);
}
catch(System.FormatException fex)
{
throw fex;
}
}
///
///
///
///
///
public static System.Single Parse(System.String s)
{
try
{
if (s.EndsWith("f") || s.EndsWith("F"))
return System.Single.Parse(s.Substring(0, s.Length - 1).Replace(".", System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator));
else
return System.Single.Parse(s.Replace(".", System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator));
}
catch(System.FormatException fex)
{
throw fex;
}
}
public static bool TryParse(System.String s, out float f)
{
bool ok = false;
if (s.EndsWith("f") || s.EndsWith("F"))
ok = System.Single.TryParse(s.Substring(0, s.Length - 1).Replace(".", System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator), out f);
else
ok = System.Single.TryParse(s.Replace(".", System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator), out f);
return ok;
}
///
///
///
///
///
public static string ToString(float f)
{
return f.ToString().Replace(System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator, ".");
}
///
///
///
///
///
///
public static string ToString(float f, string format)
{
return f.ToString(format).Replace(System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator, ".");
}
}
///
///
///
public class AppSettings
{
static System.Collections.Specialized.ListDictionary settings = new System.Collections.Specialized.ListDictionary();
///
///
///
///
///
public static void Set(System.String key, int defValue)
{
settings[key] = defValue;
//System.Configuration.ConfigurationManager.AppSettings.Set(key, defValue.ToString()); // {{Aroush-2.3.1}} try this instead
}
///
///
///
///
///
public static void Set(System.String key, long defValue)
{
settings[key] = defValue;
//System.Configuration.ConfigurationManager.AppSettings.Set(key, defValue.ToString()); // {{Aroush-2.3.1}} try this instead
}
///
///
///
///
///
public static void Set(System.String key, System.String defValue)
{
settings[key] = defValue;
//System.Configuration.ConfigurationManager.AppSettings.Set(key, defValue); // {{Aroush-2.3.1}} try this instead
}
///
///
///
///
///
public static void Set(System.String key, bool defValue)
{
settings[key] = defValue;
//System.Configuration.ConfigurationManager.AppSettings.Set(key, defValue); // {{Aroush-2.3.1}} try this instead
}
///
///
///
///
///
///
public static int Get(System.String key, int defValue)
{
if (settings[key] != null)
{
return (int) settings[key];
}
System.String theValue = System.Configuration.ConfigurationManager.AppSettings.Get(key);
if (theValue == null)
{
return defValue;
}
int retValue = System.Convert.ToInt32(theValue.Trim());
settings[key] = retValue;
return retValue;
}
///
///
///
///
///
///
public static long Get(System.String key, long defValue)
{
if (settings[key] != null)
{
return (long) settings[key];
}
System.String theValue = System.Configuration.ConfigurationManager.AppSettings.Get(key);
if (theValue == null)
{
return defValue;
}
long retValue = System.Convert.ToInt64(theValue.Trim());
settings[key] = retValue;
return retValue;
}
///
///
///
///
///
///
public static System.String Get(System.String key, System.String defValue)
{
if (settings[key] != null)
{
return (System.String) settings[key];
}
System.String theValue = System.Configuration.ConfigurationManager.AppSettings.Get(key);
if (theValue == null)
{
return defValue;
}
settings[key] = theValue;
return theValue;
}
public static bool Get(System.String key, bool defValue)
{
if (settings[key] != null)
{
return (bool)settings[key];
}
System.String theValue = System.Configuration.ConfigurationManager.AppSettings.Get(key);
if (theValue == null)
{
return defValue;
}
bool retValue = System.Convert.ToBoolean(theValue.Trim());
settings[key] = retValue;
return retValue;
}
}
///
/// This class provides supporting methods of java.util.BitSet
/// that are not present in System.Collections.BitArray.
///
public class BitSetSupport
{
///
/// Returns the next set bit at or after docId, or -1 if no such bit exists.
///
///
/// the index of bit array at which to start checking
/// the next set bit or -1
public static int NextSetBit(System.Collections.BitArray bitArray, int docId)
{
while (docId < bitArray.Length)
{
// if docId bit is set, return it
// otherwise check next docId bit
if (bitArray.Get(docId))
return docId;
else
docId++;
}
// if no bits are set at or after docId, return -1
return -1;
}
private BitSetSupport() { }
}
///
/// Summary description for TestSupportClass.
///
public class Compare
{
///
/// Compares two Term arrays for equality.
///
/// First Term array to compare
/// Second Term array to compare
/// true if the Terms are equal in both arrays, false otherwise
public static bool CompareTermArrays(Lucene.Net.Index.Term[] t1, Lucene.Net.Index.Term[] t2)
{
if (t1.Length != t2.Length)
return false;
for (int i = 0; i < t1.Length; i++)
{
if (t1[i].CompareTo(t2[i]) == 0)
{
return true;
}
}
return false;
}
///
/// Compares two string arrays for equality.
///
/// First string array list to compare
/// Second string array list to compare
/// true if the strings are equal in both arrays, false otherwise
public static bool CompareStringArrays(System.String[] l1, System.String[] l2)
{
if (l1.Length != l2.Length)
return false;
for (int i = 0; i < l1.Length; i++)
{
if (l1[i] != l2[i])
return false;
}
return true;
}
}
///
/// Use for .NET 1.1 Framework only.
///
public class CompressionSupport
{
public interface ICompressionAdapter
{
byte[] Compress(byte[] input, int offset, int length);
byte[] Uncompress(byte[] input);
}
#if SHARP_ZIP_LIB
private static ICompressionAdapter compressionAdapter = new Lucene.Net.Index.Compression.SharpZipLibAdapter();
#else
private static ICompressionAdapter compressionAdapter;
#endif
public static byte[] Uncompress(byte[] input)
{
CheckCompressionSupport();
return compressionAdapter.Uncompress(input);
}
public static byte[] Compress(byte[] input, int offset, int length)
{
CheckCompressionSupport();
return compressionAdapter.Compress(input, offset, length);
}
private static void CheckCompressionSupport()
{
if (compressionAdapter == null)
{
System.String compressionLibClassName = SupportClass.AppSettings.Get("Lucene.Net.CompressionLib.class", null);
if (compressionLibClassName == null)
throw new System.SystemException("Compression support not configured");
Type compressionLibClass = Type.GetType(compressionLibClassName, true);
object compressionAdapterObj = Activator.CreateInstance(compressionLibClass);
compressionAdapter = compressionAdapterObj as ICompressionAdapter;
if (compressionAdapter == null)
throw new System.SystemException("Compression adapter does not support the ICompressionAdapter interface");
}
}
}
#region WEAKHASHTABLE
///
/// A Hashtable which holds weak references to its keys so they
/// can be collected during GC.
///
[System.Diagnostics.DebuggerDisplay("Count = {Values.Count}")]
public class WeakHashTable : Hashtable, IEnumerable
{
///
/// A weak referene wrapper for the hashtable keys. Whenever a key\value pair
/// is added to the hashtable, the key is wrapped using a WeakKey. WeakKey saves the
/// value of the original object hashcode for fast comparison.
///
class WeakKey : WeakReference
{
int hashCode;
public WeakKey(object key)
: base(key)
{
if (key == null)
throw new ArgumentNullException("key");
hashCode = key.GetHashCode();
}
public override int GetHashCode()
{
return hashCode;
}
}
///
/// A Dictionary enumerator which wraps the original hashtable enumerator
/// and performs 2 tasks: Extract the real key from a WeakKey and skip keys
/// that were already collected.
///
class WeakDictionaryEnumerator : IDictionaryEnumerator
{
IDictionaryEnumerator baseEnumerator;
object currentKey;
object currentValue;
public WeakDictionaryEnumerator(IDictionaryEnumerator baseEnumerator)
{
this.baseEnumerator = baseEnumerator;
}
public DictionaryEntry Entry
{
get
{
return new DictionaryEntry(this.currentKey, this.currentValue);
}
}
public object Key
{
get
{
return this.currentKey;
}
}
public object Value
{
get
{
return this.currentValue;
}
}
public object Current
{
get
{
return Entry;
}
}
public bool MoveNext()
{
while (baseEnumerator.MoveNext())
{
object key = ((WeakKey)baseEnumerator.Key).Target;
if (key != null)
{
this.currentKey = key;
this.currentValue = baseEnumerator.Value;
return true;
}
}
return false;
}
public void Reset()
{
baseEnumerator.Reset();
this.currentKey = null;
this.currentValue = null;
}
}
///
/// Serves as a simple "GC Monitor" that indicates whether cleanup is needed.
/// If collectableObject.IsAlive is false, GC has occurred and we should perform cleanup
///
WeakReference collectableObject = new WeakReference(new Object());
///
/// Customize the hashtable lookup process by overriding KeyEquals. KeyEquals
/// will compare both WeakKey to WeakKey and WeakKey to real keys
///
protected override bool KeyEquals(object x, object y)
{
if (x == y)
return true;
if (x is WeakKey)
{
x = ((WeakKey)x).Target;
if (x == null)
return false;
}
if (y is WeakKey)
{
y = ((WeakKey)y).Target;
if (y == null)
return false;
}
return x.Equals(y);
}
protected override int GetHash(object key)
{
return key.GetHashCode();
}
///
/// Perform cleanup if GC occurred
///
private void CleanIfNeeded()
{
if (collectableObject.Target == null)
{
Clean();
collectableObject = new WeakReference(new Object());
}
}
///
/// Iterate over all keys and remove keys that were collected
///
private void Clean()
{
ArrayList keysToDelete = new ArrayList();
foreach (WeakKey wtk in base.Keys)
{
if (!wtk.IsAlive)
{
keysToDelete.Add(wtk);
}
}
foreach (WeakKey wtk in keysToDelete)
Remove(wtk);
}
///
/// Wrap each key with a WeakKey and add it to the hashtable
///
public override void Add(object key, object value)
{
CleanIfNeeded();
base.Add(new WeakKey(key), value);
}
public override IDictionaryEnumerator GetEnumerator()
{
return new WeakDictionaryEnumerator(base.GetEnumerator());
}
///
/// Create a temporary copy of the real keys and return that
///
public override ICollection Keys
{
get
{
ArrayList keys = new ArrayList(Count);
foreach (WeakKey key in base.Keys)
{
object realKey = key.Target;
if (realKey != null)
keys.Add(realKey);
}
return keys;
}
}
public override object this[object key]
{
get
{
return base[key];
}
set
{
CleanIfNeeded();
base[new WeakKey(key)] = value;
}
}
public override void CopyTo(Array array, int index)
{
int arrayIndex = index;
foreach (DictionaryEntry de in this)
{
array.SetValue(de, arrayIndex++);
}
}
public override int Count
{
get
{
CleanIfNeeded();
return base.Count;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
#endregion
public class Cryptography
{
static public bool FIPSCompliant = false;
static public System.Security.Cryptography.HashAlgorithm GetHashAlgorithm()
{
if (FIPSCompliant)
{
//LUCENENET-175
//No Assumptions should be made on the HashAlgorithm. It may change in time.
//SHA256 SHA384 SHA512 etc.
return System.Security.Cryptography.SHA1.Create();
}
return System.Security.Cryptography.MD5.Create();
}
}
public class FileStream
{
//[System.Runtime.InteropServices.DllImport("kernel32")]
//public static extern int FlushFileBuffers(Microsoft.Win32.SafeHandles.SafeFileHandle SafeFileHandle);
//public static void Sync(System.IO.FileStream fs)
//{
// if (FlushFileBuffers(fs.SafeFileHandle) == 0) { throw new SyncFailedException(); }
//}
//public class SyncFailedException : Exception
//{
//}
public static void Sync(System.IO.FileStream fs)
{
fs.Flush();
}
}
}