Class Exceptions


  • public final class Exceptions
    extends java.lang.Object
    Helper methods that perform various checks and throw exceptions if certain conditions are met.
    • Constructor Summary

      Constructors 
      Constructor Description
      Exceptions()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void checkArgument​(boolean validCondition, java.lang.String argName, java.lang.String message, java.lang.Object... args)
      Throws an IllegalArgumentException if the validCondition argument is false.
      static void checkArrayRange​(long startIndex, int length, long arrayLength, java.lang.String startIndexArgName, java.lang.String lengthArgName)
      Throws an appropriate exception if the given range is not included in the given array interval.
      static void checkNotClosed​(boolean closed, java.lang.Object targetObject)
      Throws an ObjectClosedException if the closed argument is true.
      static java.lang.String checkNotNullOrEmpty​(java.lang.String arg, java.lang.String argName)
      Throws a NullPointerException if the arg argument is null.
      static <K,​V>
      java.util.Map<K,​V>
      checkNotNullOrEmpty​(java.util.Map<K,​V> arg, java.lang.String argName)
      Throws a NullPointerException if the arg argument is null.
      static <T,​V extends java.util.Collection<T>>
      V
      checkNotNullOrEmpty​(V arg, java.lang.String argName)
      Throws a NullPointerException if the arg argument is null.
      static <ExceptionT extends java.lang.Exception>
      void
      handleInterrupted​(Exceptions.InterruptibleRun<ExceptionT> run)
      Eliminates boilerplate code of catching and re-interrupting the thread.
      static <ExceptionT extends java.lang.Exception,​ResultT>
      ResultT
      handleInterruptedCall​(Exceptions.InterruptibleCall<ExceptionT,​ResultT> call)
      Eliminates boilerplate code of catching and re-interrupting the thread.
      static boolean mustRethrow​(java.lang.Throwable ex)
      Determines if the given Throwable represents a fatal exception and cannot be handled.
      static boolean shouldUnwrap​(java.lang.Class<? extends java.lang.Exception> c)
      Returns true if the provided class is CompletionException or ExecutionException which need to be unwrapped.
      static java.lang.RuntimeException sneakyThrow​(java.lang.Throwable t)
      Throws any throwable 'sneakily' - you don't need to catch it, nor declare that you throw it onwards.
      static java.lang.Throwable unwrap​(java.lang.Throwable ex)
      If the provided exception is a CompletionException or ExecutionException which need be unwrapped.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Exceptions

        public Exceptions()
    • Method Detail

      • sneakyThrow

        public static java.lang.RuntimeException sneakyThrow​(java.lang.Throwable t)
        Throws any throwable 'sneakily' - you don't need to catch it, nor declare that you throw it onwards. The exception is still thrown - javac will just stop whining about it.

        Example usage:

        public void run() {
             throw sneakyThrow(new IOException("You don't need to catch me!"));
         }

        NB: The exception is not wrapped, ignored, swallowed, or redefined. The JVM actually does not know or care about the concept of a 'checked exception'. All this method does is hide the act of throwing a checked exception from the java compiler.

        Note that this method has a return type of RuntimeException; it is advised you always call this method as argument to the throw statement to avoid compiler errors regarding no return statement and similar problems. This method won't of course return an actual RuntimeException - it never returns, it always throws the provided exception.

        Parameters:
        t - The throwable to throw without requiring you to catch its type.
        Returns:
        A dummy RuntimeException; this method never returns normally, it always throws an exception!
      • mustRethrow

        public static boolean mustRethrow​(java.lang.Throwable ex)
        Determines if the given Throwable represents a fatal exception and cannot be handled.
        Parameters:
        ex - The Throwable to inspect.
        Returns:
        True if a fatal error which must be rethrown, false otherwise (it can be handled in a catch block).
      • unwrap

        public static java.lang.Throwable unwrap​(java.lang.Throwable ex)
        If the provided exception is a CompletionException or ExecutionException which need be unwrapped.
        Parameters:
        ex - The exception to be unwrapped.
        Returns:
        The cause or the exception provided.
      • shouldUnwrap

        public static boolean shouldUnwrap​(java.lang.Class<? extends java.lang.Exception> c)
        Returns true if the provided class is CompletionException or ExecutionException which need to be unwrapped.
        Parameters:
        c - The class to be tested
        Returns:
        True if unwrap(Throwable) should be called on exceptions of this type
      • handleInterrupted

        public static <ExceptionT extends java.lang.Exception> void handleInterrupted​(Exceptions.InterruptibleRun<ExceptionT> run)
                                                                               throws ExceptionT extends java.lang.Exception
        Eliminates boilerplate code of catching and re-interrupting the thread.

        NOTE: This method currently has the limitation that it can only handle functions that throw up to one additional exception besides InterruptedException. This is a limitation of the Compiler.

        Type Parameters:
        ExceptionT - The type of exception.
        Parameters:
        run - A method that should be run handling interrupts automatically
        Throws:
        ExceptionT - If thrown by run.
        ExceptionT extends java.lang.Exception
      • handleInterruptedCall

        public static <ExceptionT extends java.lang.Exception,​ResultT> ResultT handleInterruptedCall​(Exceptions.InterruptibleCall<ExceptionT,​ResultT> call)
                                                                                                    throws ExceptionT extends java.lang.Exception
        Eliminates boilerplate code of catching and re-interrupting the thread.

        NOTE: This method currently has the limitation that it can only handle functions that throw up to one additional exception besides InterruptedException. This is a limitation of the Compiler.

        Type Parameters:
        ExceptionT - The type of exception.
        ResultT - The type of the result.
        Parameters:
        call - A method that should be run handling interrupts automatically
        Returns:
        The result of the call.
        Throws:
        ExceptionT - If thrown by call.
        ExceptionT extends java.lang.Exception
      • checkNotNullOrEmpty

        public static java.lang.String checkNotNullOrEmpty​(java.lang.String arg,
                                                           java.lang.String argName)
                                                    throws java.lang.NullPointerException,
                                                           java.lang.IllegalArgumentException
        Throws a NullPointerException if the arg argument is null. Throws an IllegalArgumentException if the String arg argument has a length of zero.
        Parameters:
        arg - The argument to check.
        argName - The name of the argument (to be included in the exception message).
        Returns:
        The arg.
        Throws:
        java.lang.NullPointerException - If arg is null.
        java.lang.IllegalArgumentException - If arg is not null, but has a length of zero.
      • checkNotNullOrEmpty

        public static <T,​V extends java.util.Collection<T>> V checkNotNullOrEmpty​(V arg,
                                                                                        java.lang.String argName)
                                                                                 throws java.lang.NullPointerException,
                                                                                        java.lang.IllegalArgumentException
        Throws a NullPointerException if the arg argument is null. Throws an IllegalArgumentException if the Collections arg argument has a size of zero.
        Type Parameters:
        T - The type of elements in the provided collection.
        V - The actual type of the collection.
        Parameters:
        arg - The argument to check.
        argName - The name of the argument (to be included in the exception message).
        Returns:
        The arg.
        Throws:
        java.lang.NullPointerException - If arg is null.
        java.lang.IllegalArgumentException - If arg is not null, but has a length of zero.
      • checkNotNullOrEmpty

        public static <K,​V> java.util.Map<K,​V> checkNotNullOrEmpty​(java.util.Map<K,​V> arg,
                                                                               java.lang.String argName)
                                                                        throws java.lang.NullPointerException,
                                                                               java.lang.IllegalArgumentException
        Throws a NullPointerException if the arg argument is null. Throws an IllegalArgumentException if the Map arg argument has a size of zero.
        Type Parameters:
        K - The type of keys in the provided map.
        V - The type of keys in the provided map.
        Parameters:
        arg - The argument to check.
        argName - The name of the argument (to be included in the exception message).
        Returns:
        The arg.
        Throws:
        java.lang.NullPointerException - If arg is null.
        java.lang.IllegalArgumentException - If arg is not null, but has a length of zero.
      • checkArgument

        public static void checkArgument​(boolean validCondition,
                                         java.lang.String argName,
                                         java.lang.String message,
                                         java.lang.Object... args)
                                  throws java.lang.IllegalArgumentException
        Throws an IllegalArgumentException if the validCondition argument is false.
        Parameters:
        validCondition - The result of the condition to validate.
        argName - The name of the argument (to be included in the exception message).
        message - The message to include in the exception. This should not include the name of the argument, as that is already prefixed.
        args - Format args for message. These must correspond to String.format() args.
        Throws:
        java.lang.IllegalArgumentException - If validCondition is false.
      • checkArrayRange

        public static void checkArrayRange​(long startIndex,
                                           int length,
                                           long arrayLength,
                                           java.lang.String startIndexArgName,
                                           java.lang.String lengthArgName)
                                    throws java.lang.ArrayIndexOutOfBoundsException,
                                           java.lang.IllegalArgumentException
        Throws an appropriate exception if the given range is not included in the given array interval.
        Parameters:
        startIndex - The First index in the range.
        length - The number of items in the range.
        arrayLength - The length of the array.
        startIndexArgName - The name of the start index argument.
        lengthArgName - The name of the length argument.
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - If startIndex is less than lowBoundInclusive or if startIndex+length is greater than upBoundExclusive.
        java.lang.IllegalArgumentException - If length is a negative number.
      • checkNotClosed

        public static void checkNotClosed​(boolean closed,
                                          java.lang.Object targetObject)
                                   throws ObjectClosedException
        Throws an ObjectClosedException if the closed argument is true.
        Parameters:
        closed - The result of the condition to check. True if object is closed, false otherwise.
        targetObject - The object itself.
        Throws:
        ObjectClosedException - If closed is true.