aim4.util
Class Util

java.lang.Object
  extended by aim4.util.Util

public class Util
extends Object

This class provides helper methods that are used throughout the code.


Field Summary
static Random random
          The global random number generator
static long randSeed
          The random seed for all random number generators in the simulation
 
Method Summary
static String concatenate(List<String> strings, String sep)
          Concatenate a list of strings.
static String concatenate(String[] strings, String sep)
          Concatenate an array of strings.
static double constrain(double inputValue, double minValue, double maxValue)
          Constrain a double value between a lower and upper bound.
static boolean isDoubleEqual(double a, double b)
          Whether two floating-point numbers (doubles) are equal.
static boolean isDoubleEqual(double a, double b, double precision)
          Whether two floating-point numbers (doubles) are equal.
static boolean isDoubleEqualOrGreater(double a, double b)
          Whether two floating-point numbers (doubles) are greater than or equal to another.
static boolean isDoubleEqualOrLess(double a, double b)
          Whether two floating-point numbers (doubles) are less than or equal to another.
static boolean isDoubleNotEqual(double a, double b)
          Whether two floating-point numbers (doubles) are not equal.
static boolean isDoubleNotEqual(double a, double b, double precision)
          Whether two floating-point numbers (doubles) are not equal.
static boolean isDoubleNotZero(double a)
          Whether a floating-point numbers (doubles) is not zero.
static boolean isDoubleZero(double a)
          Whether a floating-point numbers (doubles) is zero.
static boolean nearlyEqual(double a, double b, double epsilon)
          Check whether two double values are "nearly" equal.
static int randomIndex(double[] distribution)
          Choose a number according to a finite probability distribution.
static int randomIndex(List<Double> distribution)
          Choose a number according to a finite probability distribution.
static List<String> readFileToStrArray(String inFileName)
          Read the content of a file into a list of strings.
static double recenter(double inputValue, double minValue, double maxValue)
          Recenter a double value between a lower and upper bound.
static double sum(double[] as)
          The sum of an array of floating-point numbers
static double sum(Iterable<Double> as)
          The sum of a sequence of floating-point numbers
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

randSeed

public static long randSeed
The random seed for all random number generators in the simulation


random

public static final Random random
The global random number generator

Method Detail

nearlyEqual

public static boolean nearlyEqual(double a,
                                  double b,
                                  double epsilon)
Check whether two double values are "nearly" equal.

Parameters:
a - the first double value
b - the second double value
epsilon - the precision
Returns:
whether the two values are nearly equal.

isDoubleZero

public static boolean isDoubleZero(double a)
Whether a floating-point numbers (doubles) is zero.

Parameters:
a - a double value
Returns:
whether the floating-point number is zero.

isDoubleNotZero

public static boolean isDoubleNotZero(double a)
Whether a floating-point numbers (doubles) is not zero.

Parameters:
a - a double value
Returns:
whether the floating-point number is not zero.

isDoubleEqual

public static boolean isDoubleEqual(double a,
                                    double b)
Whether two floating-point numbers (doubles) are equal.

Parameters:
a - a double value
b - a double value
Returns:
whether the two floating-point numbers are equal.

isDoubleNotEqual

public static boolean isDoubleNotEqual(double a,
                                       double b)
Whether two floating-point numbers (doubles) are not equal.

Parameters:
a - a double value
b - a double value
Returns:
whether the two floating-point numbers are not equal.

isDoubleEqual

public static boolean isDoubleEqual(double a,
                                    double b,
                                    double precision)
Whether two floating-point numbers (doubles) are equal.

Parameters:
a - a double value
b - a double value
precision - the bound on of the difference between a and b that is considered equal.
Returns:
whether the two floating-point numbers are equal.

isDoubleNotEqual

public static boolean isDoubleNotEqual(double a,
                                       double b,
                                       double precision)
Whether two floating-point numbers (doubles) are not equal.

Parameters:
a - a double value
b - a double value
precision - the bound on of the difference between a and b that is considered equal.
Returns:
whether the two floating-point numbers are not equal.

isDoubleEqualOrGreater

public static boolean isDoubleEqualOrGreater(double a,
                                             double b)
Whether two floating-point numbers (doubles) are greater than or equal to another.

Parameters:
a - a double value
b - a double value
Returns:
whether a >= b.

isDoubleEqualOrLess

public static boolean isDoubleEqualOrLess(double a,
                                          double b)
Whether two floating-point numbers (doubles) are less than or equal to another.

Parameters:
a - a double value
b - a double value
Returns:
whether a <= b.

constrain

public static double constrain(double inputValue,
                               double minValue,
                               double maxValue)
Constrain a double value between a lower and upper bound. If it is below the minimum value, return the minimum value. If it is above the maximum value, return the maximum value. Otherwise, return the original value.

Parameters:
inputValue - the value to constrain
minValue - the lower bound
maxValue - the upper bound
Returns:
a value between the lower and upper bounds

recenter

public static double recenter(double inputValue,
                              double minValue,
                              double maxValue)
Recenter a double value between a lower and upper bound. This means that if a number is between the two bounds, it is returned unchanged, but if it falls out of bounds, it is adjusted by the size of the interval until it fits. For example, it can be used for for recentering angles between 0 and 2π or 0 and 180.

Parameters:
inputValue - the value to recenter
minValue - the lower bound
maxValue - the upper bound
Returns:
a value between the lower and upper bounds

sum

public static double sum(Iterable<Double> as)
The sum of a sequence of floating-point numbers

Parameters:
as - an iterable of double values
Returns:
the sum of the double values

sum

public static double sum(double[] as)
The sum of an array of floating-point numbers

Parameters:
as - an array of double values
Returns:
the sum of the double values

randomIndex

public static int randomIndex(double[] distribution)
Choose a number according to a finite probability distribution.

Parameters:
distribution - the probability distribution
Returns:
an index of the distribution that is randomly chosen according to the distribution

randomIndex

public static int randomIndex(List<Double> distribution)
Choose a number according to a finite probability distribution.

Parameters:
distribution - the probability distribution
Returns:
an index of the distribution that is randomly chosen according to the distribution

concatenate

public static String concatenate(List<String> strings,
                                 String sep)
Concatenate a list of strings.

Parameters:
strings - a list of string
sep - the separator
Returns:
the concatenation of the list of string.

concatenate

public static String concatenate(String[] strings,
                                 String sep)
Concatenate an array of strings.

Parameters:
strings - an array of string
sep - the separator
Returns:
the concatenation of the list of string.

readFileToStrArray

public static List<String> readFileToStrArray(String inFileName)
                                       throws IOException
Read the content of a file into a list of strings.

Parameters:
inFileName - the name of the file
Returns:
the list of strings
Throws:
IOException


Copyright © 2011. All Rights Reserved.