Class Utilities

java.lang.Object
edu.uky.ai.util.Utilities

public class Utilities extends Object
Assorted helpful methods.
Author:
Stephen G. Ware
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static final Number
    add(Object n1, Object n2)
    Adds two number objects together.
    static final Number
    divide(Object n1, Object n2)
    Divides the first number object by the second.
    static final String
    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[]
    toArray(Iterable<? extends T> iterable, Class<T> type)
    Efficiently converts any Iterable into an array of its elements.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      public static final 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 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 Number add(Object n1, 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 Number subtract(Object n1, 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 Number multiply(Object n1, 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 Number divide(Object n1, 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(Iterable<? extends T> iterable, 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 String getFileName(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

      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:
      IOException - if a problem occurs while reading the JAR file
      ClassNotFoundException - if no such classes or more than one such classes are found
      NoSuchMethodException - if the class does not define a public constructor that takes no arguments
      SecurityException - if this process violates a security policy
      InstantiationException - if invoking the constructor causes an InstantiationException
      IllegalAccessException - if invoking the constructor causes an IllegalAccessException
      IllegalArgumentException - if invoking the constructor causes an IllegalArgumentException
      InvocationTargetException - if invoking the constructor causes an InvocationTargetException