Class Checks

java.lang.Object
io.activej.common.Checks

public final class Checks extends Object
This class is used for determining whether Checks should be enabled or not. It is sometimes useful to disable preconditions checks at runtime environment to squeeze out more performance in a tight loop.

The common pattern is having a

 private static final boolean CHECK = Check.isEnabled(MyClass.class);
 
constant in your class. When using Checks just wrap the call in if-statement like so:
 if (CHECK) Preconditions.checkNotNull(value);
 

By default, all checks are disabled (like java asserts). To enable all checks you can run application with system property -Dchk=on You can enable or disable checks for the whole package (with its subpackages) or for individual classes like so: -Dchk:io.activej.eventloop=on -Dchk:io.activej.promise.Promise=off.

  • Constructor Details

    • Checks

      public Checks()
  • Method Details

    • isEnabled

      public static boolean isEnabled(Class<?> cls)
      Indicates whether checks are enabled or disabled for the specified class
      Parameters:
      cls - class to be checked
      Returns:
      true if checks are enabled for the given class, false otherwise
    • isEnabledByDefault

      public static boolean isEnabledByDefault()
      Returns a boolean that indicates whether checks are enabled by default
    • checkNotNull

      @NotNull public static <T> T checkNotNull(@Nullable T reference)
      Checks an object for nullness

      If an object is not null then it is returned as-is. Otherwise, a NullPointerException is thrown

      Type Parameters:
      T - a type of object to be checked
      Parameters:
      reference - an object to be checked for nullness
      Returns:
      a checked object
      Throws:
      NullPointerException - if an object is null
    • checkNotNull

      @NotNull public static <T> T checkNotNull(@Nullable T reference, Object message)
      Checks an object for nullness

      If an object is not null then it is returned as-is. Otherwise, a NullPointerException is thrown with a specified message

      Type Parameters:
      T - a type of object to be checked
      Parameters:
      reference - an object to be checked for nullness
      message - an object that represents a message to be used in thrown NullPointerException
      Returns:
      a checked object
      Throws:
      NullPointerException - if an object is null
    • checkNotNull

      @NotNull public static <T> T checkNotNull(@Nullable T reference, Supplier<String> message)
      Checks an object for nullness

      If an object is not null then it is returned as-is. Otherwise, a NullPointerException is thrown with a supplied message

      Type Parameters:
      T - a type of object to be checked
      Parameters:
      reference - an object to be checked for nullness
      message - a supplier of message to be used in thrown NullPointerException
      Returns:
      a checked object
      Throws:
      NullPointerException - if an object is null
    • checkNotNull

      @NotNull public static <T> T checkNotNull(@Nullable T reference, String template, Object... args)
      Checks an object for nullness

      If an object is not null then it is returned as-is. Otherwise, a NullPointerException is thrown with a templated message

      Type Parameters:
      T - a type of object to be checked
      Parameters:
      reference - an object to be checked for nullness
      template - a template of message to be used in thrown NullPointerException
      args - arguments to an error message template
      Returns:
      a checked object
      Throws:
      NullPointerException - if an object is null
      See Also:
    • checkState

      public static void checkState(boolean expression)
      Checks a validity of a state

      If a given expression is true then a method finishes successfully. Otherwise, an IllegalStateException is thrown

      Parameters:
      expression - a boolean that represents a validity of a state
      Throws:
      IllegalStateException - if a state is not valid
    • checkState

      public static void checkState(boolean expression, Object message)
      Checks a validity of a state

      If a given expression is true then a method finishes successfully. Otherwise, an IllegalStateException is thrown with a specified message

      Parameters:
      expression - a boolean that represents a validity of a state
      message - an object that represents a message to be used in thrown IllegalStateException
      Throws:
      IllegalStateException - if a state is not valid
    • checkState

      public static void checkState(boolean expression, Supplier<String> message)
      Checks a validity of a state

      If a given expression is true then a method finishes successfully. Otherwise, an IllegalStateException is thrown with a supplied message

      Parameters:
      expression - a boolean that represents a validity of a state
      message - a supplier of message to be used in thrown IllegalStateException
      Throws:
      IllegalStateException - if a state is not valid
    • checkState

      public static void checkState(boolean expression, String template, Object... args)
      Checks a validity of a state

      If a given expression is true then a method finishes successfully. Otherwise, an IllegalStateException is thrown with a supplied message

      Parameters:
      expression - a boolean that represents a validity of a state
      template - a template of message to be used in thrown IllegalStateException
      args - arguments to an error message template
      Throws:
      IllegalStateException - if a state is not valid
      See Also:
    • checkArgument

      public static void checkArgument(boolean expression)
      Checks a validity of an argument

      If a given expression is true then a method finishes successfully. Otherwise, an IllegalArgumentException is thrown

      Parameters:
      expression - a boolean that represents a validity of an argument
      Throws:
      IllegalArgumentException - if an argument is not valid
    • checkArgument

      public static void checkArgument(boolean expression, Object message)
      Checks a validity of an argument

      If a given expression is true then a method finishes successfully. Otherwise, an IllegalArgumentException is thrown with a specified message

      Parameters:
      expression - a boolean that represents a validity of an argument
      message - an object that represents a message to be used in thrown IllegalArgumentException
      Throws:
      IllegalArgumentException - if an argument is not valid
    • checkArgument

      public static void checkArgument(boolean expression, Supplier<String> message)
      Checks a validity of an argument

      If a given expression is true then a method finishes successfully. Otherwise, an IllegalArgumentException is thrown with a supplied message

      Parameters:
      expression - a boolean that represents a validity of an argument
      message - a supplier of message to be used in thrown IllegalArgumentException
      Throws:
      IllegalArgumentException - if an argument is not valid
    • checkArgument

      public static void checkArgument(boolean expression, String template, Object... args)
      Checks a validity of an argument

      If a given expression is true then a method finishes successfully. Otherwise, an IllegalArgumentException is thrown with a supplied message

      Parameters:
      expression - a boolean that represents a validity of an argument
      template - a template of message to be used in thrown IllegalArgumentException
      args - arguments to an error message template
      Throws:
      IllegalArgumentException - if an argument is not valid
      See Also:
    • checkArgument

      public static <T> T checkArgument(T argument, Predicate<T> predicate)
      Checks a validity of a given argument

      If an argument is valid then an argument is returned as-is. Otherwise, an IllegalArgumentException is thrown

      Parameters:
      argument - an argument to be checked for validity
      predicate - a predicate that checks an argument for validity
      Throws:
      IllegalArgumentException - if an argument is not valid
    • checkArgument

      public static <T> T checkArgument(T argument, Predicate<T> predicate, Object message)
      Checks a validity of a given argument

      If an argument is valid then an argument is returned as-is. Otherwise, an IllegalArgumentException is thrown with a specified message

      Parameters:
      argument - an argument to be checked for validity
      predicate - a predicate that checks an argument for validity
      message - an object that represents a message to be used in thrown IllegalArgumentException
      Throws:
      IllegalArgumentException - if an argument is not valid
    • checkArgument

      public static <T> T checkArgument(T argument, Predicate<T> predicate, Supplier<String> message)
      Checks a validity of a given argument

      If an argument is valid then an argument is returned as-is. Otherwise, an IllegalArgumentException is thrown with a supplied message

      Parameters:
      argument - an argument to be checked for validity
      predicate - a predicate that checks an argument for validity
      message - a supplier of message to be used in thrown IllegalArgumentException
      Throws:
      IllegalArgumentException - if an argument is not valid
    • checkArgument

      public static <T> T checkArgument(T argument, Predicate<T> predicate, Function<T,String> message)
      Checks a validity of a given argument

      If an argument is valid then an argument is returned as-is. Otherwise, an IllegalArgumentException is thrown with a supplied message

      Parameters:
      argument - an argument to be checked for validity
      predicate - a predicate that checks an argument for validity
      message - a function that transforms an argument into an error message to be used in thrown IllegalArgumentException
      Throws:
      IllegalArgumentException - if an argument is not valid
    • checkArgument

      public static <T> T checkArgument(T argument, Predicate<T> predicate, String template, Object... args)
      Checks a validity of a given argument

      If an argument is valid then an argument is returned as-is. Otherwise, an IllegalArgumentException is thrown with a supplied message

      Parameters:
      argument - an argument to be checked for validity
      predicate - a predicate that checks an argument for validity
      template - a template of message to be used in thrown IllegalArgumentException
      args - arguments to an error message template
      Throws:
      IllegalArgumentException - if an argument is not valid