Class IgniteUtils

java.lang.Object
org.apache.ignite.internal.util.IgniteUtils

public abstract class IgniteUtils extends Object
Collection of utility methods used throughout the system.
  • Field Details

  • Constructor Details

    • IgniteUtils

      public IgniteUtils()
  • Method Details

    • cast

      public static IgniteCheckedException cast(Throwable t)
      Casts the passed Throwable t to IgniteCheckedException.
      If t is a GridClosureException, it is unwrapped and then cast to IgniteCheckedException. If t is an IgniteCheckedException, it is returned. If t is not a IgniteCheckedException, a new IgniteCheckedException caused by t is returned.
      Parameters:
      t - Throwable to cast.
      Returns:
      t cast to IgniteCheckedException.
    • currentTimeMillis

      public static long currentTimeMillis()
    • compact

      public static String compact(String s)
      Replaces all occurrences of org.apache.ignite. with o.a.i., org.apache.ignite.internal. with o.a.i.i., org.apache.ignite.internal.visor. with o.a.i.i.v. and org.apache.ignite.scalar. with o.a.i.s..
      Parameters:
      s - String to replace in.
      Returns:
      Replaces string.
    • copy

      public static int copy(InputStream in, OutputStream out) throws IOException
      Copies input byte stream to output byte stream.
      Parameters:
      in - Input byte stream.
      out - Output byte stream.
      Returns:
      Number of the copied bytes.
      Throws:
      IOException - Thrown if an I/O error occurs.
    • delete

      public static boolean delete(Path path)
      Deletes file or directory with all sub-directories and files.
      Parameters:
      path - File or directory to delete.
      Returns:
      true if and only if the file or directory is successfully deleted, false otherwise
    • error

      public static void error(IgniteLogger log, Object shortMsg, Throwable e)
      Parameters:
      log - Optional logger.
      shortMsg - Message to log using quiet logger.
      e - Optional exception.
    • error

      public static void error(IgniteLogger log, Object longMsg, Object shortMsg, Throwable e)
      Depending on whether or not log is provided and quiet mode is enabled logs given messages as quiet message or normal log ERROR message. If log is null or in QUIET mode it will add (err) prefix to the message.
      Parameters:
      log - Optional logger to use when QUIET mode is not enabled.
      longMsg - Message to log using normal logger.
      shortMsg - Message to log using quiet logger.
      e - Optional exception.
    • forRange

      public static GridIntIterator forRange(int start, int cnt)
      Returns GridIntIterator for range of primitive integers.
      Parameters:
      start - Start.
      cnt - Count.
    • hash

      public static int hash(int h)
      Applies a supplemental hash function to a given hashCode, which defends against poor quality hash functions. This is critical because ConcurrentHashMap uses power-of-two length hash tables, that otherwise encounter collisions for hashCodes that do not differ in lower or upper bits.

      This function has been taken from Java 8 ConcurrentHashMap with slightly modifications.

      Parameters:
      h - Value to hash.
      Returns:
      Hash value.
    • hash

      public static int hash(Object key)
      Applies a supplemental hash function to a given hashCode, which defends against poor quality hash functions. This is critical because ConcurrentHashMap uses power-of-two length hash tables, that otherwise encounter collisions for hashCodes that do not differ in lower or upper bits.

      This function has been taken from Java 8 ConcurrentHashMap with slightly modifications.

      Parameters:
      key - Value to hash.
      Returns:
      Hash value.
    • hash

      public static int hash(long key)
      A primitive override of hash(Object) to avoid unnecessary boxing.
      Parameters:
      key - Value to hash.
      Returns:
      Hash value.
    • hashCode

      public static int hashCode(Object obj)
      Calculate a hashCode for an array.
      Parameters:
      obj - Object.
    • hexLong

      public static String hexLong(long val)
      Gets a hex string representation of the given long value.
      Parameters:
      val - Value to convert to string.
      Returns:
      Hex string.
    • interrupt

      public static void interrupt(Thread t)
      Convenience method that interrupts a given thread if it's not null.
      Parameters:
      t - Thread to interrupt.
    • interrupt

      public static void interrupt(Iterable<? extends Thread> workers)
      Convenience method that interrupts a given thread if it's not null.
      Parameters:
      workers - Threads to interrupt.
    • isPow2

      public static boolean isPow2(int i)
      Parameters:
      i - Value.
      Returns:
      true If the given value is power of 2 (0 is not power of 2).
    • join

      public static void join(Thread t) throws org.apache.ignite.internal.IgniteInterruptedCheckedException
      Joins thread.
      Parameters:
      t - Thread.
      Throws:
      org.apache.ignite.internal.IgniteInterruptedCheckedException - Wrapped InterruptedException.
    • join

      public static boolean join(Thread t, IgniteLogger log)
      Waits for completion of a given thread. If thread is null then this method returns immediately returning true
      Parameters:
      t - Thread to join.
      log - Logger for logging errors.
      Returns:
      true if thread has finished, false otherwise.
    • join

      public static boolean join(Thread t, IgniteLogger log, long timeout)
      Waits for completion of a given thread. If thread is null then this method returns immediately returning true
      Parameters:
      t - Thread to join.
      log - Logger for logging errors.
      timeout - Join timeout.
      Returns:
      true if thread has finished, false otherwise.
    • joinThreads

      public static boolean joinThreads(Iterable<? extends Thread> workers, IgniteLogger log)
      Waits for completion of a given threads. If thread is null then this method returns immediately returning true
      Parameters:
      workers - Thread to join.
      log - Logger for logging errors.
      Returns:
      true if thread has finished, false otherwise.
    • jdkVersion

      public static String jdkVersion()
      Gets JDK version.
      Returns:
      JDK version.
    • error

      public static void error(IgniteLogger log, Object msg)
      Depending on whether or not log is provided and quiet mode is enabled logs given messages as quiet message or normal log ERROR message. If log is null or in QUIET mode it will add (err) prefix to the message.
      Parameters:
      log - Optional logger to use when QUIET mode is not enabled.
      msg - Message to log.
    • nextPowerOf2

      public static int nextPowerOf2(int v)
      Round up the argument to the next highest power of 2;
      Parameters:
      v - Value to round up.
      Returns:
      Next closest power of 2.
    • majorJavaVersion

      public static int majorJavaVersion(String verStr)
      Get major Java version from string.
      Parameters:
      verStr - Version string.
      Returns:
      Major version or zero if failed to resolve.
    • nl

      public static String nl()
      Returns:
      line.separator system property.
    • readableSize

      public static String readableSize(long bytes, boolean si)
      Parameters:
      bytes - Number of bytes to display.
      si - If true, then unit base is 1000, otherwise unit base is 1024.
      Returns:
      Formatted size.
    • safeAbs

      public static int safeAbs(int i)
      Gets absolute value for integer. If integer is Integer.MIN_VALUE, then 0 is returned.
      Parameters:
      i - Integer.
      Returns:
      Absolute value.
    • safeAbs

      public static long safeAbs(long i)
      Gets absolute value for long. If argument is Long.MIN_VALUE, then 0 is returned.
      Parameters:
      i - Argument.
      Returns:
      Absolute value.
    • sleep

      public static void sleep(long ms) throws org.apache.ignite.internal.IgniteInterruptedCheckedException
      Sleeps for given number of milliseconds.
      Parameters:
      ms - Time to sleep.
      Throws:
      org.apache.ignite.internal.IgniteInterruptedCheckedException - Wrapped InterruptedException.
    • toHexString

      public static String toHexString(long addr, int len)
      Parameters:
      addr - pointer in memory
      len - how much byte to read (should divide 8)
      Returns:
      hex representation of memory region
    • unwrap

      public static Exception unwrap(Throwable t)
      Unwraps closure exceptions.
      Parameters:
      t - Exception.
      Returns:
      Unwrapped exception.
    • warn

      public static void warn(IgniteLogger log, Object msg)
      Depending on whether or not log is provided and quiet mode is enabled logs given messages as quiet message or normal log WARN message. If log is null or in QUIET mode it will add (wrn) prefix to the message.
      Parameters:
      log - Optional logger to use when QUIET mode is not enabled.
      msg - Message to log.
    • warn

      public static void warn(IgniteLogger log, Object msg, Throwable e)
      Depending on whether or not log is provided and quiet mode is enabled logs given messages as quiet message or normal log WARN message. If log is null or in QUIET mode it will add (wrn) prefix to the message.
      Parameters:
      log - Optional logger to use when QUIET mode is not enabled.
      msg - Message to log using normal logger.
      e - Optional exception.
    • writeByteArray

      public static void writeByteArray(DataOutput out, byte[] arr) throws IOException
      Writes byte array to output stream accounting for null values.
      Parameters:
      out - Output stream to write to.
      arr - Array to write, possibly null.
      Throws:
      IOException - If write failed.
    • readByteArray

      public static byte[] readByteArray(DataInput in) throws IOException
      Reads byte array from input stream accounting for null values.
      Parameters:
      in - Stream to read from.
      Returns:
      Read byte array, possibly null.
      Throws:
      IOException - If read failed.