Class UtilIan

java.lang.Object
  extended by UtilIan

public class UtilIan
extends java.lang.Object


Method Summary
static int ceil(double x)
          The smallest int ≥ to x (same as Math.ceil, but returns int.)
static java.lang.String charToString(java.lang.Character c)
          Convert a char to a String.
static int doubleToInt(java.lang.Double d)
          Convert a Double to an int.
static boolean equalsApprox(double d1, double d2)
          Return whether two doubles are equal (approximately).
static boolean equalsApprox(double d1, double d2, double relativeTolerance)
           
static int floor(double x)
          The largest int ≤ to x (same as Math.floor, but returns int.)
static boolean hasNextChar(java.util.Scanner s)
          Is there a next (non-white) character to read from a scanner? Same as hasNext(); provided for completeness.
static boolean hasNextChar(java.util.Scanner s, char c)
          Is the next character from a scanner's input (skipping whitespace)? This method may advances the scanner over any whitespace.
static double intToDouble(java.lang.Integer i)
          Convert an Integer into to a double.
static
<T extends java.lang.Comparable<T>>
T
max(java.util.Collection<T> ts)
          Return the maximum item in a Collection.
static
<T extends java.lang.Comparable<T>>
T
max(T... ts)
          A var-args version of max(Collection).
static
<T extends java.lang.Comparable<T>>
T
min(java.util.Collection<T> ts)
          Return the minimum item in a Collection.
static
<T extends java.lang.Comparable<T>>
T
min(T... ts)
          A var-args version of min(Collection).
static java.lang.Character nextChar(java.util.Scanner s)
          Read the next char from a scanner's input (skipping whitespace).
static java.lang.Character nextChar(java.util.Scanner s, char c)
          Read the given character from a scanner's input (skipping whitespace).
static java.lang.String nextMatch(java.util.Scanner s, java.util.regex.Pattern pat)
          Return the next match (skipping initial whitespace) of a pattern.
static java.lang.String nextMatch(java.util.Scanner s, java.lang.String pat)
          Return the next match (skipping initial whitespace) of a pattern.
static double roundTo(double x, int places)
          Round a number to a certain number of decimal places.
static int roundToInt(double x)
          Round a Double to the nearest int.
static void skipWhitespace(java.util.Scanner s)
          Skip over the whitespace in a Scanner.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

ceil

public static int ceil(double x)
The smallest int ≥ to x (same as Math.ceil, but returns int.)

Parameters:
d - The double to find the ceiling of.
Returns:
The smallest int ≥ x.

charToString

public static java.lang.String charToString(java.lang.Character c)
Convert a char to a String.

Parameters:
c - The Character to conver to a String.
Returns:
A String of length 1, corresponding to c.

doubleToInt

public static int doubleToInt(java.lang.Double d)
Convert a Double to an int. Same as casting (but w/o new syntax.)

Parameters:
d - The Double to convert to an int.
Returns:
The int corresponding to d.

equalsApprox

public static boolean equalsApprox(double d1,
                                   double d2)
Return whether two doubles are equal (approximately). This function is symmetric. TODO: detail what happens if d1 or d2 is zero.

Parameters:
d1 - A double to compare.
d2 - A double to compare.
Returns:
true iff d1 is close to d2 (within a factor of {UtilIan.TOLERANCE}).

equalsApprox

public static boolean equalsApprox(double d1,
                                   double d2,
                                   double relativeTolerance)

floor

public static int floor(double x)
The largest int ≤ to x (same as Math.floor, but returns int.)

Parameters:
x - The double to find the floor of.
Returns:
The larest int ≤ x.

hasNextChar

public static boolean hasNextChar(java.util.Scanner s)
Is there a next (non-white) character to read from a scanner? Same as hasNext(); provided for completeness.

Parameters:
s - The scanner to read from.
Returns:
Whether s has any (non-white) input to read.

hasNextChar

public static boolean hasNextChar(java.util.Scanner s,
                                  char c)
Is the next character from a scanner's input (skipping whitespace)? This method may advances the scanner over any whitespace.

Parameters:
s - The scanner to read from.
c - The char to read.
Returns:
Whether c is the next (non-white) char at the front of s.

intToDouble

public static double intToDouble(java.lang.Integer i)
Convert an Integer into to a double. Same as casting (but w/o new syntax.)

Parameters:
i - The Integer to convert to a double.
Returns:
The double corresponding to i.

max

public static <T extends java.lang.Comparable<T>> T max(java.util.Collection<T> ts)
Return the maximum item in a Collection.

Parameters:
ts - A collection of comparable objects.
Returns:
the (first) largest element in ts.

max

public static <T extends java.lang.Comparable<T>> T max(T... ts)
A var-args version of max(Collection).

See Also:
UtilIan.max(Collection).

min

public static <T extends java.lang.Comparable<T>> T min(java.util.Collection<T> ts)
Return the minimum item in a Collection.

Parameters:
ts - A collection of comparable objects.
Returns:
the (first) smallest element in ts.

min

public static <T extends java.lang.Comparable<T>> T min(T... ts)
A var-args version of min(Collection).

See Also:
UtilIan.min(Collection).

nextChar

public static java.lang.Character nextChar(java.util.Scanner s)
Read the next char from a scanner's input (skipping whitespace).

Parameters:
s - The scanner to read from.
Returns:
true iff c is at the front of s's input (skipping whitespace).

nextChar

public static java.lang.Character nextChar(java.util.Scanner s,
                                           char c)
Read the given character from a scanner's input (skipping whitespace).

Parameters:
s - The scanner to read from.
c - The char to read.
Returns:
new Character(c), or null if c is not at the front of s's input (skipping whitespace).

nextMatch

public static java.lang.String nextMatch(java.util.Scanner s,
                                         java.util.regex.Pattern pat)
Return the next match (skipping initial whitespace) of a pattern. Note: there is no corresponding 'hasNextMatch' method; this method either returns (and consumes) the matched String, or null.

Parameters:
s - The scanner to read from.
pat - The pattern to look for.
Returns:
The String matching the given pattern; if the front of the input doesn't match the pattern then null is returned and the scanner is does not consume any input. (N.B. A *large* amount of input might be buffered, depending on the pattern.)

nextMatch

public static java.lang.String nextMatch(java.util.Scanner s,
                                         java.lang.String pat)
Return the next match (skipping initial whitespace) of a pattern. Note: there is no corresponding 'hasNextMatch' method; this method either returns the matched String, or null.

Parameters:
s - The scanner to read from.
pat - The pattern to look for.
Returns:
The String matching the given pattern; if the front of the input doesn't match the pattern then null is returned and the scanner is does not consume any input. (N.B. A *large* amount of input might be buffered, depending on the pattern.)

roundTo

public static double roundTo(double x,
                             int places)
Round a number to a certain number of decimal places.

Parameters:
x - The number to round.
places - The number of decimal places to round to. Can be negative.
Returns:
The double corresponding to i.

roundToInt

public static int roundToInt(double x)
Round a Double to the nearest int. Same as Math.round, but returns an int. (Does *not* round-to-even, since java.lang.Math.round doesn't.)

Parameters:
x - The Double to round.
Returns:
The int nearest x (rounded as per Math.round).

skipWhitespace

public static void skipWhitespace(java.util.Scanner s)
Skip over the whitespace in a Scanner. Not helpful unless you use methods which ignore delimiters (such as Scanner.findWithinHorizon).

Parameters:
s - The scanner to skip over whitespace.