Package edu.uky.ai.util
Class Utilities
java.lang.Object
edu.uky.ai.util.Utilities
public class Utilities
extends java.lang.Object
Assorted helpful methods.
- Author:
- Stephen G. Ware
-
Constructor Summary
Constructors Constructor Description Utilities()
-
Method Summary
Modifier and Type Method Description static java.lang.Number
add(java.lang.Object n1, java.lang.Object n2)
Adds two number objects together.static java.lang.Number
divide(java.lang.Object n1, java.lang.Object n2)
Divides the first number object by the second.static java.lang.String
getFileName(java.io.File file)
Returns the name of a file (with no preceding URL information and no trailing file extensions).static <T> T
loadFromJARFile(java.lang.Class<T> type, java.io.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 java.lang.Number
multiply(java.lang.Object n1, java.lang.Object n2)
Multiplies two number objects together.static java.lang.String
percent(double value)
Converts a decimal number into a string expressing percentage.static java.lang.Number
subtract(java.lang.Object n1, java.lang.Object n2)
Subtracts the second number object from the first.static java.lang.String
time(long time)
Converts a duration in milliseconds into a formatted string expressing showing minutes, seconds, and milliseconds.static <T> T[]
toArray(java.lang.Iterable<? extends T> iterable, java.lang.Class<T> type)
Efficiently converts anyIterable
into an array of its elements.
-
Constructor Details
-
Utilities
public Utilities()
-
-
Method Details
-
time
public static final java.lang.String time(long 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
public static final java.lang.String percent(double value)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
public static final java.lang.Number add(java.lang.Object n1, java.lang.Object n2)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
public static final java.lang.Number subtract(java.lang.Object n1, java.lang.Object n2)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
public static final java.lang.Number multiply(java.lang.Object n1, java.lang.Object n2)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
public static final java.lang.Number divide(java.lang.Object n1, java.lang.Object n2)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
public static final <T> T[] toArray(java.lang.Iterable<? extends T> iterable, java.lang.Class<T> type)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
public static final java.lang.String getFileName(java.io.File file)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(java.lang.Class<T> type, java.io.File jarFile) throws java.io.IOException, java.lang.ClassNotFoundException, java.lang.NoSuchMethodException, java.lang.SecurityException, java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.IllegalArgumentException, java.lang.reflect.InvocationTargetExceptionSearches 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:
java.io.IOException
- if a problem occurs while reading the JAR filejava.lang.ClassNotFoundException
- if no such classes or more than one such classes are foundjava.lang.NoSuchMethodException
- if the class does not define a public constructor that takes no argumentsjava.lang.SecurityException
- if this process violates a security policyjava.lang.InstantiationException
- if invoking the constructor causes an InstantiationExceptionjava.lang.IllegalAccessException
- if invoking the constructor causes an IllegalAccessExceptionjava.lang.IllegalArgumentException
- if invoking the constructor causes an IllegalArgumentExceptionjava.lang.reflect.InvocationTargetException
- if invoking the constructor causes an InvocationTargetException
-