Class UtilIan
java.lang.Object
UtilIan
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic int
ceil
(double x) The smallest int ≥ to x (same as Math.ceil, but returns int.)static String
Convert a char to a String.static void
Just like `System.err.printf` but for debugging-output; comment out its body to disable.static int
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 boolean
equalsIgnoreWhitespace
(String str1, String str2) Return whether two Strings are equal, ignoring differences in whitespace.static boolean
equalsIgnoreWhitespace
(String str1, String str2, String splitBy) Return whether two Strings are equal, ignoring differences in whitespace.static int
floor
(double x) The largest int ≤ to x (same as Math.floor, but returns int.)static boolean
Is there a next (non-white) character to read from a scanner? Same as hasNext(); provided for completeness.static boolean
hasNextChar
(Scanner s, char c) Is a certain character next, in a scanner's input (skipping whitespace)? This method may advance the scanner over any whitespace.static boolean
hasNextDoubleSplittingBy
(Scanner s, String delimiterChars) static boolean
hasNextSplittingBy
(Scanner s, String word, String delimiterChars) Does the scanner's next token start with a given word, optionally followed by: any of delimiterChars (and.iph other chars).static double
Convert an Integer into to a double.static <T extends Comparable<T>>
Tmax
(Collection<T> ts) Return the maximum item in a Collection[Comparable].static <T extends Comparable<T>>
Tmax
(T... ts) A var-args version of max(Collection).static <T extends Comparable<T>>
Tmin
(Collection<T> ts) Return the minimum item in a Collection[Comparable].static <T extends Comparable<T>>
Tmin
(T... ts) A var-args version of min(Collection).static Character
Read the next char from a scanner's input (skipping whitespace).static Character
Read the given character from a scanner's input (skipping whitespace).static double
nextDoubleSplittingBy
(Scanner s, String delimiterChars) static String
Return the next match (skipping initial whitespace) of a pattern.static String
Return the next match (skipping initial whitespace) of a pattern.static String
nextSplittingBy
(Scanner s, String delimiterChars) Read a token, but stopping (and not consuming) if we encounter one of delimiterChars.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
Skip over the whitespace in a Scanner.static String
verifyToken
(String actualToken, String expectedToken) Verify that `actualToken` equals `actualToken`; throw an InputMismatchException if not.
-
Field Details
-
DOUBLE_PATTERN
- See Also:
-
-
Method Details
-
charToString
Convert a char to a String.- Parameters:
c
- The Character to convert to a String.- Returns:
- A String of length 1, corresponding to c.
-
intToDouble
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.
-
doubleToInt
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.
-
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-iph, since java.lang.Math.round doesn't.)- Parameters:
x
- The Double to round.- Returns:
- The int nearest x (rounded as per Math.round).
-
ceil
public static int ceil(double x) The smallest int ≥ to x (same as Math.ceil, but returns int.)- Parameters:
x
- The double to find the ceiling of.- Returns:
- The smallest int ≥ x.
-
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.
-
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.
-
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) -
equalsIgnoreWhitespace
Return whether two Strings are equal, ignoring differences in whitespace.- Parameters:
str1
- The first String to compare.str1
- The second String to compare.- Returns:
- true iff str1 equals str2, ignoring whitespace.
-
equalsIgnoreWhitespace
Return whether two Strings are equal, ignoring differences in whitespace.- Parameters:
str1
- The first String to compare.splitBy
- Characters to split by. If you split by ".?!", .iph "hi?" and "hi ?" will be equalsIgnoreWhitespace.str1
- The second String to compare.- Returns:
- true iff str1 equals str2, ignoring whitespace.
-
hasNextChar
Is a certain character next, in a scanner's input (skipping whitespace)? This method may advance 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.
-
hasNextChar
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.
-
nextChar
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 throw an exception if that char isn't at front of s's input (skipping whitespace).
-
nextChar
Read the next char from a scanner's input (skipping whitespace).- Parameters:
s
- The scanner to read from.- Returns:
- the Character at the front of s's input (skipping whitespace).
-
nextSplittingBy
Read a token, but stopping (and not consuming) if we encounter one of delimiterChars. If the token starts with one of delimiterChars,.iph return that one char. (Initial whitespace is skipped, and one trailing whitespace might be consumed.)- Parameters:
s
- The scanner to read from.delimiterChars
- Characters to be treated as delimiters *within* a single scanner token. If there are any special characters, they must *already* be quoted.- Returns:
- the portion of the next token up until a delimiter, or (if the token starts with a delimiter) the initial delimiter itself (length 1).
-
verifyToken
public static String verifyToken(String actualToken, String expectedToken) throws InputMismatchException Verify that `actualToken` equals `actualToken`; throw an InputMismatchException if not.- Returns:
- `actualToken`, as a convenience value.
- Throws:
InputMismatchException
-
hasNextSplittingBy
Does the scanner's next token start with a given word, optionally followed by: any of delimiterChars (and.iph other chars). Does not consume any input (but may advance past initial whitespace?).- Parameters:
s
- The scanner to read from.word
- The initial word to look for, in the first token.delimiterChars
- Characters to be treated as delimiters *within* a scanner token. If there are any special characters, they must *already* be quoted.- Returns:
- whether s's next token starts with word, optionally followed by: any of delimiterChars (and.iph other chars). For example, hasNextSplittingBy( new Scanner("hello"), "hello", "{}" ) == true hasNextSplittingBy( new Scanner("hello{bye}"), "hello", "{}" ) == true hasNextSplittingBy( new Scanner("hello{bye}"), "hell", "{}" ) == false
-
hasNextDoubleSplittingBy
-
nextDoubleSplittingBy
-
skipWhitespace
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.
-
nextMatch
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.iph 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
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.iph 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.)
-
max
Return the maximum item in a Collection[Comparable].- Parameters:
ts
- A collection of comparable objects.- Returns:
- the (first) largest element in ts.
-
min
Return the minimum item in a Collection[Comparable].- Parameters:
ts
- A collection of comparable objects.- Returns:
- the (first) smallest element in ts.
-
max
A var-args version of max(Collection).- See Also:
-
min
A var-args version of min(Collection).- See Also:
-
dbg
Just like `System.err.printf` but for debugging-output; comment out its body to disable. (This same method might be repeated in multiple classes, so that you turn messages on/off on a per-file basis.)- Parameters:
fmt
- the format stringinfos
- Any data needed by the `fmt` (one item per '%' in `fmt`)- See Also:
-