Class Utils


  • public final class Utils
    extends java.lang.Object
    General internal utility methods.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Utils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void checkArgument​(boolean isValid, java.lang.Object errorMessage)
      Throws an IllegalArgumentException if the argument is false.
      static void checkArgument​(boolean expression, java.lang.String errorMessageTemplate, java.lang.Object... errorMessageArgs)
      Throws an IllegalArgumentException if the argument is false.
      static void checkIndex​(int index, int size)
      Validates an index in an array or other container.
      static <T> void checkListElementNotNull​(java.util.List<T> list, java.lang.Object errorMessage)
      Throws a NullPointerException if any of the list elements is null.
      static <K,​V>
      void
      checkMapElementNotNull​(java.util.Map<K,​V> map, java.lang.Object errorMessage)
      Throws a NullPointerException if any of the map elements is null.
      static <T> T checkNotNull​(T arg, java.lang.Object errorMessage)
      Throws a NullPointerException if the argument is null.
      static void checkState​(boolean isValid, java.lang.Object errorMessage)
      Throws an IllegalStateException if the argument is false.
      static boolean equalsObjects​(java.lang.Object x, java.lang.Object y)
      Compares two Objects for equality.
      private static java.lang.String format​(java.lang.String template, java.lang.Object... args)
      Substitutes each %s in template with an argument.
      • Methods inherited from class java.lang.Object

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

      • Utils

        private Utils()
    • Method Detail

      • checkArgument

        public static void checkArgument​(boolean isValid,
                                         @Nullable
                                         java.lang.Object errorMessage)
        Throws an IllegalArgumentException if the argument is false. This method is similar to Preconditions.checkArgument(boolean, Object) from Guava.
        Parameters:
        isValid - whether the argument check passed.
        errorMessage - the message to use for the exception. Will be converted to a string using String.valueOf(Object).
      • checkArgument

        public static void checkArgument​(boolean expression,
                                         java.lang.String errorMessageTemplate,
                                         @Nullable
                                         java.lang.Object... errorMessageArgs)
        Throws an IllegalArgumentException if the argument is false. This method is similar to Preconditions.checkArgument(boolean, Object) from Guava.
        Parameters:
        expression - a boolean expression
        errorMessageTemplate - a template for the exception message should the check fail. The message is formed by replacing each %s placeholder in the template with an argument. These are matched by position - the first %s gets errorMessageArgs[0], etc. Unmatched arguments will be appended to the formatted message in square braces. Unmatched placeholders will be left as-is.
        errorMessageArgs - the arguments to be substituted into the message template. Arguments are converted to strings using String.valueOf(Object).
        Throws:
        java.lang.IllegalArgumentException - if expression is false
        java.lang.NullPointerException - if the check fails and either errorMessageTemplate or errorMessageArgs is null (don't let this happen)
      • checkState

        public static void checkState​(boolean isValid,
                                      @Nullable
                                      java.lang.Object errorMessage)
        Throws an IllegalStateException if the argument is false. This method is similar to Preconditions.checkState(boolean, Object) from Guava.
        Parameters:
        isValid - whether the state check passed.
        errorMessage - the message to use for the exception. Will be converted to a string using String.valueOf(Object).
      • checkIndex

        public static void checkIndex​(int index,
                                      int size)
        Validates an index in an array or other container. This method throws an IllegalArgumentException if the size is negative and throws an IndexOutOfBoundsException if the index is negative or greater than or equal to the size. This method is similar to Preconditions.checkElementIndex(int, int) from Guava.
        Parameters:
        index - the index to validate.
        size - the size of the array or container.
      • checkNotNull

        public static <T> T checkNotNull​(T arg,
                                         @Nullable
                                         java.lang.Object errorMessage)
        Throws a NullPointerException if the argument is null. This method is similar to Preconditions.checkNotNull(Object, Object) from Guava.
        Parameters:
        arg - the argument to check for null.
        errorMessage - the message to use for the exception. Will be converted to a string using String.valueOf(Object).
        Returns:
        the argument, if it passes the null check.
      • checkListElementNotNull

        public static <T> void checkListElementNotNull​(java.util.List<T> list,
                                                       @Nullable
                                                       java.lang.Object errorMessage)
        Throws a NullPointerException if any of the list elements is null.
        Parameters:
        list - the argument list to check for null.
        errorMessage - the message to use for the exception. Will be converted to a string using String.valueOf(Object).
      • checkMapElementNotNull

        public static <K,​V> void checkMapElementNotNull​(java.util.Map<K,​V> map,
                                                              @Nullable
                                                              java.lang.Object errorMessage)
        Throws a NullPointerException if any of the map elements is null.
        Parameters:
        map - the argument map to check for null.
        errorMessage - the message to use for the exception. Will be converted to a string using String.valueOf(Object).
      • equalsObjects

        public static boolean equalsObjects​(@Nullable
                                            java.lang.Object x,
                                            @Nullable
                                            java.lang.Object y)
        Compares two Objects for equality. This functionality is provided by Objects.equal(Object, Object) in Java 7.
      • format

        private static java.lang.String format​(java.lang.String template,
                                               @Nullable
                                               java.lang.Object... args)
        Substitutes each %s in template with an argument. These are matched by position: the first %s gets args[0], etc. If there are more arguments than placeholders, the unmatched arguments will be appended to the end of the formatted message in square braces.

        Copied from Preconditions.format(String, Object...) from Guava

        Parameters:
        template - a non-null string containing 0 or more %s placeholders.
        args - the arguments to be substituted into the message template. Arguments are converted to strings using String.valueOf(Object). Arguments can be null.