org.primordion.xholon.util
Class Misc

java.lang.Object
  extended byorg.primordion.xholon.util.Misc

public class Misc
extends java.lang.Object

This is a mix of static methods that don't have a home elsewhere. The methods all have to do with random numbers, and conversion between ASCII and various numeric formats.

Since:
0.1 (Created on Jul 7, 2005)
Author:
Ken Webb

Field Summary
static int JAVACLASS_array
           
static int JAVACLASS_arrayboolean
           
static int JAVACLASS_arraydouble
           
static int JAVACLASS_arrayfloat
           
static int JAVACLASS_arrayint
           
static int JAVACLASS_arrayString
           
static int JAVACLASS_boolean
           
static int JAVACLASS_double
           
static int JAVACLASS_float
           
static int JAVACLASS_int
           
static int JAVACLASS_long
           
static int JAVACLASS_String
           
static int JAVACLASS_UNKNOWN
           
 
Constructor Summary
Misc()
           
 
Method Summary
static boolean atob(java.lang.String str, int ixStart)
          Convert an ascii string into a boolean.
static double atod(java.lang.String str, int ixStart)
          Convert an ascii string into a double.
static float atof(java.lang.String str, int ixStart)
          Convert an ascii string into a float.
static int atoi(java.lang.String str, int ixStart)
          Convert an ascii string into an int.
static long atol(java.lang.String str, int ixStart)
          Convert an ascii string into a long.
static boolean booleanValue(java.lang.String b)
          Return the boolean equivalent of a String.
static boolean canExecute(java.io.File f)
          Can this existing file be executed? A similar function has been added to Java 6.
static void closeInputFile(java.io.Reader in)
          Close input file.
static void closeOutputFile(java.io.Writer out)
          Close output file.
static int getJavaDataType(java.lang.String s)
          Get the Java type of the input data.
static int getNumericValue(char ch)
          Get the numeric value of a char.
static java.util.Random getRandom()
          Get the singleton instance of Random used by this application.
static double getRandomDouble(double minVal, double maxVal)
          Get random double >= minVal and <= maxVal.
static int getRandomInt(int minVal, int maxVal)
          Get random integer >= minVal and < maxVal.
static boolean isdigit(int digit)
          Is this a digit, a value between 0 and 9.
static boolean isEven(int val)
          Is this an even number?
static boolean isMathSymbol(char symbol)
          Is this a math symbol? This method takes the place of calling if (Character.getType(symbol) == Character.MATH_SYMBOL)
static java.io.Reader openInputFile(java.lang.String fileName)
          Open input file.
static java.io.Writer openOutputFile(java.lang.String fileName)
          Open output file.
static long seedRandomNumberGenerator()
          Seed the random number generator with the current data and time.
static long seedRandomNumberGenerator(long seed)
          Seed the random number generator with a known value.
static boolean setExecutable(java.io.File f, boolean executable)
          Set the executable attribute of an existing file.
static boolean setExecutable(java.io.File f, boolean executable, boolean ownerOnly)
          Set the executable attribute of an existing file.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

JAVACLASS_UNKNOWN

public static final int JAVACLASS_UNKNOWN
See Also:
Constant Field Values

JAVACLASS_int

public static final int JAVACLASS_int
See Also:
Constant Field Values

JAVACLASS_long

public static final int JAVACLASS_long
See Also:
Constant Field Values

JAVACLASS_double

public static final int JAVACLASS_double
See Also:
Constant Field Values

JAVACLASS_float

public static final int JAVACLASS_float
See Also:
Constant Field Values

JAVACLASS_boolean

public static final int JAVACLASS_boolean
See Also:
Constant Field Values

JAVACLASS_String

public static final int JAVACLASS_String
See Also:
Constant Field Values

JAVACLASS_array

public static final int JAVACLASS_array
See Also:
Constant Field Values

JAVACLASS_arrayint

public static final int JAVACLASS_arrayint
See Also:
Constant Field Values

JAVACLASS_arraydouble

public static final int JAVACLASS_arraydouble
See Also:
Constant Field Values

JAVACLASS_arrayfloat

public static final int JAVACLASS_arrayfloat
See Also:
Constant Field Values

JAVACLASS_arrayboolean

public static final int JAVACLASS_arrayboolean
See Also:
Constant Field Values

JAVACLASS_arrayString

public static final int JAVACLASS_arrayString
See Also:
Constant Field Values
Constructor Detail

Misc

public Misc()
Method Detail

getRandom

public static java.util.Random getRandom()
Get the singleton instance of Random used by this application.

Returns:
A singleton instance of Random.

atod

public static double atod(java.lang.String str,
                          int ixStart)
Convert an ascii string into a double.

Parameters:
str - The string that contains the double.
ixStart - The start position within the string (0 indexed).
Returns:
A double.

atof

public static float atof(java.lang.String str,
                         int ixStart)
Convert an ascii string into a float.

Parameters:
str - The string that contains the float.
ixStart - The start position within the string (0 indexed).
Returns:
A float.

atoi

public static int atoi(java.lang.String str,
                       int ixStart)
Convert an ascii string into an int.

Parameters:
str - The string that contains the int.
ixStart - The start position within the string (0 indexed).
Returns:
An int.

atol

public static long atol(java.lang.String str,
                        int ixStart)
Convert an ascii string into a long.

Parameters:
str - The string that contains the long.
ixStart - The start position within the string (0 indexed).
Returns:
A long.

atob

public static boolean atob(java.lang.String str,
                           int ixStart)
Convert an ascii string into a boolean.

Parameters:
str - The string that contains the boolean.
ixStart - The start position within the string (0 indexed).
Returns:
A boolean.

isdigit

public static boolean isdigit(int digit)
Is this a digit, a value between 0 and 9.

Parameters:
digit - The digit being tested.
Returns:
true or false

isMathSymbol

public static boolean isMathSymbol(char symbol)
Is this a math symbol? This method takes the place of calling if (Character.getType(symbol) == Character.MATH_SYMBOL)

Parameters:
symbol - A character to be tested to see if it's a math symbol.
Returns:
true or false

isEven

public static boolean isEven(int val)
Is this an even number?

Parameters:
val - An integer that may be even or odd.
Returns:
Whether or not the intgeger is even.

getNumericValue

public static int getNumericValue(char ch)
Get the numeric value of a char. This method takes the place of calling Character.getNumericValue(char), but only handles ASCII characters between '0' and '9'. It cannot handle unicode characters.

Parameters:
ch - A character.
Returns:
An integer between 0 and 9, or -1 if the character does not have a numeric value.

booleanValue

public static boolean booleanValue(java.lang.String b)
Return the boolean equivalent of a String. If the String does not encode a valid boolean, then return false.

Parameters:
b - A String that must have a value of "true", "false", "TRUE", or "FALSE".
Returns:
true or false

getJavaDataType

public static int getJavaDataType(java.lang.String s)
Get the Java type of the input data. examples: JAVACLASS_boolean: true false JAVACLASS_double : 123.0 2.6e-5 123.0e+5 -123.0 JAVACLASS_float : 123.0f -123.0f JAVACLASS_int : 123 -123 JAVACLASS_long : 9060404178364531554L 543210l (ends with upper or lowercase L) JAVACLASS_String : abc

Parameters:
s - Some data.
Returns:
The Java type.

getRandomInt

public static int getRandomInt(int minVal,
                               int maxVal)
Get random integer >= minVal and < maxVal.

Parameters:
minVal - The result will be >= this value.
maxVal - The result will be < this value.
Returns:
A pseudo-random integer.

getRandomDouble

public static double getRandomDouble(double minVal,
                                     double maxVal)
Get random double >= minVal and <= maxVal. TODO confirm that this works correctly

Parameters:
minVal - The result will be >= this value.
maxVal - The result will be <= this value.
Returns:
A pseudo-random double.

seedRandomNumberGenerator

public static long seedRandomNumberGenerator()
Seed the random number generator with the current data and time.


seedRandomNumberGenerator

public static long seedRandomNumberGenerator(long seed)
Seed the random number generator with a known value.


openInputFile

public static java.io.Reader openInputFile(java.lang.String fileName)
Open input file.

Parameters:
fileName - Name of input file.
Returns:
An instance of Reader.

closeInputFile

public static void closeInputFile(java.io.Reader in)
Close input file.

Parameters:
in - An instance of Reader.

openOutputFile

public static java.io.Writer openOutputFile(java.lang.String fileName)
Open output file.

Parameters:
fileName - Name of the output file.
Returns:
An instance of Writer.

closeOutputFile

public static void closeOutputFile(java.io.Writer out)
Close output file.

Parameters:
out - An instance of Writer.

setExecutable

public static boolean setExecutable(java.io.File f,
                                    boolean executable)
Set the executable attribute of an existing file. This is intended to change permissions on a Linux/Unix system. Calling this function is equivalent to "chmod u+x f" or "chmod u-x f". A similar function has been added to Java 6.

Parameters:
f - An existing file.
executable - If true, sets the access permission to allow execute operations; if false to disallow execute operations.
Returns:
true or false

setExecutable

public static boolean setExecutable(java.io.File f,
                                    boolean executable,
                                    boolean ownerOnly)
Set the executable attribute of an existing file. This is intended to change permissions on a Linux/Unix system. Calling this function is equivalent to "chmod u+x f" or "chmod u-x f" or "chmod ugo+x f" or "chmod ugo-x f". A similar function has been added to Java 6.

Parameters:
f - An existing file.
executable - If true, sets the access permission to allow execute operations; if false to disallow execute operations.
ownerOnly - If true, the execute permission applies only to the owner's execute permission; otherwise, it applies to everybody. If the underlying file system can not distinguish the owner's execute permission from that of others, then the permission will apply to everybody, regardless of this value.
Returns:
true if and only if the operation succeeded. The operation will fail if the user does not have permission to change the access permissions of this abstract pathname. If executable is false and the underlying file system does not implement an execute permission, then the operation will fail.

canExecute

public static boolean canExecute(java.io.File f)
Can this existing file be executed? A similar function has been added to Java 6.

Parameters:
f - An existing file.
Returns:
true or false