Class Utilities

java.lang.Object
edu.uky.cs.nil.tt.Utilities

public class Utilities extends Object
A collection of utility methods used throughout this project.
Author:
Stephen G. Ware
  • Method Summary

    Modifier and Type
    Method
    Description
    static final int
    bits(int count)
    Returns the number of bits needed to assign unique ID numbers to each of a given number of objects.
    static final String
    Capitalizes the first letter of a string.
    static final String
    Randomly generates a valid name.
    static final void
    requireAllNonNull(Object[] array, String description)
    Throws an exception if any index in an array is null.
    static final <T> T
    requireEquals(T object, Object other)
    Returns the first object if both objects are equal; otherwise, throws an exception.
    static final void
    requireExactly(long number, long value, String description)
    Throws an exception if a given number is anything other than a specific value.
    static final void
    requireGreaterThan(long number, long lower, String description)
    Throws an exception if a number is not greater than a given lower bound.
    static final void
    requireLessThanOrEqualTo(long number, long max, String description)
    Throws an exception if a number is not less than or equal to a given upper bound.
    static final void
    Throws an exception if a string does not meet the requirements for a name.
    static final void
    requireNonNegative(long number, String description)
    Throws an exception if a number is negative.
    static final void
    requireNonNull(Object object, String description)
    Throws an exception if the given object is null.
    static final <T> T
    requireType(Object object, Class<T> type, String description)
    If a given object is of the given type, this method casts the object to that type; otherwise, it throws an exception.
    static final int
    toInteger(String string)
    Parses a string into an integer or throws an exception if the string cannot be parsed.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • requireNonNull

      public static final void requireNonNull(Object object, String description)
      Throws an exception if the given object is null.
      Parameters:
      object - the object which should not be null
      description - a short description of the type of object, used in the message of the exception which is thrown if the object is null
      Throws:
      NullPointerException - if the object given is null
    • requireAllNonNull

      public static final void requireAllNonNull(Object[] array, String description)
      Throws an exception if any index in an array is null.
      Parameters:
      array - the array of objects, none of which should be null
      description - a short description of the type of object in the array, used in the message of the exception which is thrown if the object is null
      Throws:
      NullPointerException - if any index in the given array is null
    • requireType

      public static final <T> T requireType(Object object, Class<T> type, String description)
      If a given object is of the given type, this method casts the object to that type; otherwise, it throws an exception.
      Type Parameters:
      T - the type to which the object will be cast, if possible
      Parameters:
      object - the object to be cast
      type - the class to which the object will be cast
      description - a short description of the type of object to be cast, used in the message of the exception which is thrown if the object cannot be cast
      Returns:
      the object, cast to the given type
      Throws:
      IllegalArgumentException - if the given object is null or cannot be cast to the given type
    • requireEquals

      public static final <T> T requireEquals(T object, Object other)
      Returns the first object if both objects are equal; otherwise, throws an exception. This method can accept null arguments. If only one argument is null, the exception will be thrown, but if both arguments are null, no exception will be thrown.
      Type Parameters:
      T - the type of the first object
      Parameters:
      object - the first object
      other - the second object
      Returns:
      the first object, if both objects are equal
      Throws:
      IllegalArgumentException - if the two objects are not equal
    • requireExactly

      public static final void requireExactly(long number, long value, String description)
      Throws an exception if a given number is anything other than a specific value.
      Parameters:
      number - the number which should be a specific value
      value - the value that number should be
      description - a short description of the purpose of the number, used in the message of the exception which is thrown if the number is not the correct value
      Throws:
      IllegalArgumentException - if the number is not the given value
    • requireNonNegative

      public static final void requireNonNegative(long number, String description)
      Throws an exception if a number is negative.
      Parameters:
      number - the number which should not be negative
      description - a short description of the purpose of the number, used in the message of the exception which is thrown if the number is negative
    • requireGreaterThan

      public static final void requireGreaterThan(long number, long lower, String description)
      Throws an exception if a number is not greater than a given lower bound.
      Parameters:
      number - the number which must be greater than the lower bound
      lower - the lower bound
      description - a short description of the purpose of the number, used in the message of the exception which is thrown if the number is not greater than the lower bound
    • requireLessThanOrEqualTo

      public static final void requireLessThanOrEqualTo(long number, long max, String description)
      Throws an exception if a number is not less than or equal to a given upper bound.
      Parameters:
      number - the number which must be less than or equal to the upper bound
      max - the upper bound
      description - a short description of the purpose of the number, used in the message of the exception which is thrown if the number is not less than or equal to the upper bound
      Throws:
      IllegalArgumentException - if the number if not less than or equal to the given upper bound
    • requireName

      public static final void requireName(String string)
      Throws an exception if a string does not meet the requirements for a name. Requirements may include limits on length and the number of characters.
      Parameters:
      string - the string which must be a valid name
      Throws:
      IllegalArgumentException - if the string is not a valid name
    • bits

      public static final int bits(int count)
      Returns the number of bits needed to assign unique ID numbers to each of a given number of objects. If the given number of objects is 0 or 1, then 0 bits are needed. Two objects require 1 bit. Three or four objects require 2 bits, etc.
      Parameters:
      count - the number of objects which must each be assigned a unique ID number
      Returns:
      the minimum number of bits needed to assign a unique ID number to each of the given number of objects
    • capitalize

      public static final String capitalize(String string)
      Capitalizes the first letter of a string.
      Parameters:
      string - the string whose first letter should be a capital letter
      Returns:
      an identical string, except that the first letter has been capitalized
    • toInteger

      public static final int toInteger(String string)
      Parses a string into an integer or throws an exception if the string cannot be parsed.
      Parameters:
      string - the string to be parsed as an integer
      Returns:
      the integer
      Throws:
      NumberFormatException - if the string cannot be parsed as an integer
    • getRandomName

      public static final String getRandomName()
      Randomly generates a valid name.
      Returns:
      a random valid name