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 any Iterable into an array of its elements.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • 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 of Number or null (which is treated as 0).
      Parameters:
      n1 - the first number
      n2 - 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 of Number or null (which is treated as 0).
      Parameters:
      n1 - the first number
      n2 - 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 of Number or null (which is treated as 0).
      Parameters:
      n1 - the first number
      n2 - 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 of Number or null (which is treated as 0).
      Parameters:
      n1 - the first number
      n2 - 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 any Iterable 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 array
      type - 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.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 for
      jarFile - 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 file
      java.lang.ClassNotFoundException - if no such classes or more than one such classes are found
      java.lang.NoSuchMethodException - if the class does not define a public constructor that takes no arguments
      java.lang.SecurityException - if this process violates a security policy
      java.lang.InstantiationException - if invoking the constructor causes an InstantiationException
      java.lang.IllegalAccessException - if invoking the constructor causes an IllegalAccessException
      java.lang.IllegalArgumentException - if invoking the constructor causes an IllegalArgumentException
      java.lang.reflect.InvocationTargetException - if invoking the constructor causes an InvocationTargetException