Package edu.uky.ai.util
Class Utilities
java.lang.Object
edu.uky.ai.util.Utilities
Assorted helpful methods.
- Author:
- Stephen G. Ware
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic final Number
Adds two number objects together.static final Number
Divides the first number object by the second.static final String
getFileName
(File file) Returns the name of a file (with no preceding URL information and no trailing file extensions).static final <T> T
loadFromJARFile
(Class<T> type, File jarFile) Searches a given JAR file for the definition of a non-abstract class that extends the given class and returns a new instance of that class.static final double
log2
(double x) Calculates the base 2 logarithm of a number.static final Number
Multiplies two number objects together.static final String
percent
(double value) Converts a decimal number into a string expressing percentage.static final Number
Subtracts the second number object from the first.static final String
time
(long time) Converts a duration in milliseconds into a formatted string expressing showing minutes, seconds, and milliseconds.static final <T> T[]
Efficiently converts anyIterable
into an array of its elements.
-
Constructor Details
-
Utilities
public Utilities()
-
-
Method Details
-
log2
public static final double log2(double x) Calculates the base 2 logarithm of a number.- Parameters:
x
- the number- Returns:
- the base 2 logarithm of x
-
time
Converts a duration in milliseconds into a formatted string expressing showing minutes, seconds, and milliseconds.- Parameters:
time
- the duration in milliseconds- Returns:
- a string in the format "minutes:seconds:milliseconds"
-
percent
Converts a decimal number into a string expressing percentage.- Parameters:
value
- a double between 0 and 1 (inclusive)- Returns:
- a string representing a percentage
-
add
Adds two number objects together. The objects must be instances ofNumber
or null (which is treated as 0).- Parameters:
n1
- the first numbern2
- the second number- Returns:
- the sum of the numbers
-
subtract
Subtracts the second number object from the first. The objects must be instances ofNumber
or null (which is treated as 0).- Parameters:
n1
- the first numbern2
- the second number- Returns:
- the difference of the numbers
-
multiply
Multiplies two number objects together. The objects must be instances ofNumber
or null (which is treated as 0).- Parameters:
n1
- the first numbern2
- the second number- Returns:
- the product of the numbers
-
divide
Divides the first number object by the second. The objects must be instances ofNumber
or null (which is treated as 0).- Parameters:
n1
- the first numbern2
- the second number- Returns:
- the quotient of the numbers
-
toArray
Efficiently converts anyIterable
into an array of its elements.- Type Parameters:
T
- the component type of the array to be returned- Parameters:
iterable
- the group of objects to convert to an arraytype
- the component type of the array to be returned- Returns:
- an array of the given component type containing the objects
-
getFileName
Returns the name of a file (with no preceding URL information and no trailing file extensions).- Parameters:
file
- the file whose name is needed- Returns:
- the file's name
-
loadFromJARFile
public static final <T> T loadFromJARFile(Class<T> type, File jarFile) throws IOException, ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException Searches a given JAR file for the definition of a non-abstract class that extends the given class and returns a new instance of that class. If no such class or more than one such class is found, an exception is thrown. The class must define a public constructor which taken no arguments.- Type Parameters:
T
- an ancestor type of the object being searched for- Parameters:
type
- an ancestor class of the object being searched forjarFile
- the JAR file to search- Returns:
- an instance of the class which extends the given parent class
- Throws:
IOException
- if a problem occurs while reading the JAR fileClassNotFoundException
- if no such classes or more than one such classes are foundNoSuchMethodException
- if the class does not define a public constructor that takes no argumentsSecurityException
- if this process violates a security policyInstantiationException
- if invoking the constructor causes an InstantiationExceptionIllegalAccessException
- if invoking the constructor causes an IllegalAccessExceptionIllegalArgumentException
- if invoking the constructor causes an IllegalArgumentExceptionInvocationTargetException
- if invoking the constructor causes an InvocationTargetException
-