de.unibi.techfak.misc
Class IOTools

java.lang.Object
  extended by de.unibi.techfak.misc.IOTools

public class IOTools
extends java.lang.Object

Tools for input and output operations and format conversions.


Field Summary
static java.util.Random rand
          The random number generator used by some methods within the class.
private static long serialVersionUID
          The seriel version number of this class.
 
Constructor Summary
IOTools()
           
 
Method Summary
static java.lang.String combineStrings(java.lang.Object[] array, java.lang.String delim)
          An array of objects is combined into one string ready for output.
static java.lang.String createRandomSequence(int length, char[] chars, double[] distr)
          Creates a sequence of random characters using the given distribution.
static java.lang.String cutString(java.lang.String s, int size)
          This method reduces any given string to be less or equal the given size.
static java.lang.String dbl2str(double nr)
          Converts a double to string with a 2-figures-precision
static java.lang.String dbl2str(double nr, int fig)
          Converts a double to string with the number of figures after the point (rounded).
static java.lang.String formatTime(long msec)
          Short call for formatTime( msec, -1, true ).
static java.lang.String formatTime(long msec, int count, boolean ms)
           Gets the number of milliseconds and builds a time string.
static java.io.BufferedReader openFile(java.lang.String filename)
          Opens an existing file and returns a BufferedReader.
static java.io.BufferedWriter openFile(java.lang.String filename, char how)
          Opens an file to write data.
static boolean test(int flag, int bits)
           Using this method you can test a flag for various settings.
static boolean testA(int flag, int onBits, int offBits)
          Using this method you can test a flag for various settings and off settings.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

rand

public static java.util.Random rand
The random number generator used by some methods within the class. May not be null, otherwise the method using this throw a NullPointerException. This object is made public, so it can be set by any out-sider class. This might be neccessary if the seed has to be known.


serialVersionUID

private static final long serialVersionUID
The seriel version number of this class. Starts with 1000L, every new method increases this number.

See Also:
Constant Field Values
Constructor Detail

IOTools

public IOTools()
Method Detail

openFile

public static java.io.BufferedReader openFile(java.lang.String filename)
Opens an existing file and returns a BufferedReader.

Parameters:
filename - The name of the file to open for reading.
Returns:
The BufferedReader, null in case of error

openFile

public static java.io.BufferedWriter openFile(java.lang.String filename,
                                              char how)
Opens an file to write data. If how is set to 'a', the file must exist and all data to write will be appent. If how is not 'a', the file will always be created.

Parameters:
filename - The name of the file to open for writing.
how - The mode of writing, recognized is 'a' for appending.
Returns:
The BufferedWriter or null in case of error

dbl2str

public static java.lang.String dbl2str(double nr,
                                       int fig)
Converts a double to string with the number of figures after the point (rounded). The sign of the number is always set, either a minus, if nr is less then 0.0, otherwise a blank.

Parameters:
nr - The number to be converted to a String.
fig - The precision of the conversion.
Returns:
A String representing the double number.

dbl2str

public static java.lang.String dbl2str(double nr)
Converts a double to string with a 2-figures-precision

Parameters:
nr - The number to be converted to a String.
Returns:
A String representing the double number.

test

public static boolean test(int flag,
                           int bits)

Using this method you can test a flag for various settings. This method test, whether at least one of the given 'bits' is set, e.g. imagine flag=1 and bits=3, than this method would return true.

If either flag or bits is zero, false is returned.

Parameters:
flag - The flag to be tested.
bits - The bits that must be set in the flag.
Returns:
true, if at least one bit in bits is set in flag, false otherwise.

testA

public static boolean testA(int flag,
                            int onBits,
                            int offBits)
Using this method you can test a flag for various settings and off settings. To return true, two conditions must be met. First, all offBits must be off in flag, second, at least one onBit must be on. If flag=0 false is returned. onBits and offBits might share some bits.

Parameters:
flag - The flag to be tested.
onBits - At least one of these bits must be set in the flag.
offBits - The bits that must be off in the flag.
Returns:
true, if at least one required bit is set in flag AND ALL tested bits are off, false otherwise.

cutString

public static java.lang.String cutString(java.lang.String s,
                                         int size)
This method reduces any given string to be less or equal the given size. Characters are taken from the beginning of the string. This method tries to remove line by line from the string. While doing so, it is possible to undergo the given size.

Parameters:
s - The string which size should be reduced.
size - The size the string is meant to be after cutting.
Returns:
The cutted string. If no cutting was neccessary a reference to the given String is returned.

combineStrings

public static java.lang.String combineStrings(java.lang.Object[] array,
                                              java.lang.String delim)
An array of objects is combined into one string ready for output. For every Object in the array the method toString() is called and the resulting Strings are combined with delim between them.

Parameters:
array - The objects from which the describing strings are got.
delim - The String to seperate two objects description.
Returns:
The descriptions of the Objects.

createRandomSequence

public static java.lang.String createRandomSequence(int length,
                                                    char[] chars,
                                                    double[] distr)
Creates a sequence of random characters using the given distribution. Note, that the length of both arrays must match. The values in the distribution array must sum to one.

Parameters:
length - The lenght of the generated random sequence.
chars - The characters used for the random sequence.
distr - The distribution of the characters.
Returns:
The random sequence or null in case of any error.

formatTime

public static java.lang.String formatTime(long msec,
                                          int count,
                                          boolean ms)

Gets the number of milliseconds and builds a time string. The time string has the format '[[[wwd ]xxh ]yym ]zz.zzzs'. msec is recalcalated to days (ww), hours (xx), minutes (yy), seconds (zz) and milliseconds (.zzz). If leading time values are zero, they are skipped.

Example: for msec=4500, the resulting string is '4.5s', for msec=450,000, it is '7m 30s' and for msec=45,001,803 it is '12h 30m 1.803s'

The parameter count defines how many values should be included in the formated time string. Set count to less than zero to get the format like in the above examples. Give count greater or equal one to include values (from left to right) and exclude the residues. If count is zero, the empty String is returned.

Example: msec may always be 45,001,803, (ms=true for count=0 one gets the same as in the example above, '12h 30m 1.803s'. For count=1 the resulting string contains only the first time value, '12h', all other values are left out. For count=2 the result is '12h 30m', for count=3 it is '12h 30m 1s', the milliseconds are skipped, and for count greater than 3 it is '12h 30m 1.803s'.

Parameters:
msec - The number of milliseconds.
count - The number of time values included in the string.
ms - Should millisecond be printed or not.
Returns:
A time string consisting of days, hours, minutes, seconds and milliseconds.

formatTime

public static java.lang.String formatTime(long msec)
Short call for formatTime( msec, -1, true ).

Parameters:
msec - The number of milliseconds.
Returns:
A time string consisting of days, hours, minutes, seconds and milliseconds.
See Also:
formatTime(long, int, boolean)