Class Utilities
java.lang.Object
edu.uky.cs.nil.tt.Utilities
A collection of utility methods used throughout this project.
- Author:
- Stephen G. Ware
-
Method Summary
Modifier and TypeMethodDescriptionstatic final intbits(int count) Returns the number of bits needed to assign unique ID numbers to each of a given number of objects.static final Stringcapitalize(String string) Capitalizes the first letter of a string.static final StringRandomly generates a validname.static final voidrequireAllNonNull(Object[] array, String description) Throws an exception if any index in an array is null.static final <T> TrequireEquals(T object, Object other) Returns the first object if both objects areequal; otherwise, throws an exception.static final voidrequireExactly(long number, long value, String description) Throws an exception if a given number is anything other than a specific value.static final voidrequireGreaterThan(long number, long lower, String description) Throws an exception if a number is not greater than a given lower bound.static final voidrequireLessThanOrEqualTo(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 voidrequireName(String string) Throws an exception if a string does not meet the requirements for aname.static final voidrequireNonNegative(long number, String description) Throws an exception if a number is negative.static final voidrequireNonNull(Object object, String description) Throws an exception if the given object is null.static final <T> TrequireType(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 intParsesa string into an integer or throws an exception if the string cannot be parsed.
-
Method Details
-
requireNonNull
Throws an exception if the given object is null.- Parameters:
object- the object which should not be nulldescription- 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
Throws an exception if any index in an array is null.- Parameters:
array- the array of objects, none of which should be nulldescription- 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
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 casttype- the class to which the object will be castdescription- 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
Returns the first object if both objects areequal; 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 objectother- the second object- Returns:
- the first object, if both objects are equal
- Throws:
IllegalArgumentException- if the two objects are not equal
-
requireExactly
Throws an exception if a given number is anything other than a specific value.- Parameters:
number- the number which should be a specific valuevalue- the value that number should bedescription- 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
Throws an exception if a number is negative.- Parameters:
number- the number which should not be negativedescription- 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
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 boundlower- the lower bounddescription- 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
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 boundmax- the upper bounddescription- 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
Throws an exception if a string does not meet the requirements for aname. 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
-
toInteger
Parsesa 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
-