Class Util


  • public final class Util
    extends java.lang.Object
    A collection of assorted utility classes.
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static void assertBackgroundThread()
      Throws an IllegalArgumentException if called on the main thread.
      static void assertMainThread()
      Throws an IllegalArgumentException if called on a thread other than the main thread.
      static boolean bothModelsNullEquivalentOrEquals​(java.lang.Object a, java.lang.Object b)  
      static boolean bothNullOrEqual​(java.lang.Object a, java.lang.Object b)
      Null-safe equivalent of a.equals(b).
      static <T> java.util.Queue<T> createQueue​(int size)
      Creates a Queue of the given size using Glide's preferred implementation.
      static int getBitmapByteSize​(int width, int height, android.graphics.Bitmap.Config config)
      Returns the in memory size of Bitmap with the given width, height, and Bitmap.Config.
      static int getBitmapByteSize​(android.graphics.Bitmap bitmap)
      Returns the in memory size of the given Bitmap in bytes.
      static int getBytesPerPixel​(android.graphics.Bitmap.Config config)
      Returns the number of bytes required to store each pixel of a Bitmap with the given config.
      static int getSize​(android.graphics.Bitmap bitmap)
      static <T> java.util.List<T> getSnapshot​(java.util.Collection<T> other)
      Returns a copy of the given list that is safe to iterate over and perform actions that may modify the original list.
      static int hashCode​(boolean value)  
      static int hashCode​(boolean value, int accumulator)  
      static int hashCode​(float value)  
      static int hashCode​(float value, int accumulator)  
      static int hashCode​(int value)  
      static int hashCode​(int value, int accumulator)  
      static int hashCode​(java.lang.Object object, int accumulator)  
      static boolean isOnBackgroundThread()
      Returns true if called on a background thread, false otherwise.
      static boolean isOnMainThread()
      Returns true if called on the main thread, false otherwise.
      static boolean isValidDimension​(int dimen)  
      static boolean isValidDimensions​(int width, int height)
      Returns true if width and height are both > 0 and/or equal to Target.SIZE_ORIGINAL.
      static void postOnUiThread​(java.lang.Runnable runnable)
      Posts the given runnable to the UI thread using a shared Handler.
      static void removeCallbacksOnUiThread​(java.lang.Runnable runnable)
      Removes the given runnable from the UI threads queue if it is still queued.
      static java.lang.String sha256BytesToHex​(byte[] bytes)
      Returns the hex string of the given byte array representing a SHA256 hash.
      • Methods inherited from class java.lang.Object

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

      • sha256BytesToHex

        @NonNull
        public static java.lang.String sha256BytesToHex​(@NonNull
                                                        byte[] bytes)
        Returns the hex string of the given byte array representing a SHA256 hash.
      • getBitmapByteSize

        public static int getBitmapByteSize​(@NonNull
                                            android.graphics.Bitmap bitmap)
        Returns the in memory size of the given Bitmap in bytes.
      • getBitmapByteSize

        public static int getBitmapByteSize​(int width,
                                            int height,
                                            @Nullable
                                            android.graphics.Bitmap.Config config)
        Returns the in memory size of Bitmap with the given width, height, and Bitmap.Config.
      • getBytesPerPixel

        public static int getBytesPerPixel​(@Nullable
                                           android.graphics.Bitmap.Config config)
        Returns the number of bytes required to store each pixel of a Bitmap with the given config.

        Defaults to Bitmap.Config.ARGB_8888 if config is null.

      • isValidDimensions

        public static boolean isValidDimensions​(int width,
                                                int height)
        Returns true if width and height are both > 0 and/or equal to Target.SIZE_ORIGINAL.
      • isValidDimension

        public static boolean isValidDimension​(int dimen)
      • postOnUiThread

        public static void postOnUiThread​(java.lang.Runnable runnable)
        Posts the given runnable to the UI thread using a shared Handler.
      • removeCallbacksOnUiThread

        public static void removeCallbacksOnUiThread​(java.lang.Runnable runnable)
        Removes the given runnable from the UI threads queue if it is still queued.
      • assertMainThread

        public static void assertMainThread()
        Throws an IllegalArgumentException if called on a thread other than the main thread.
      • assertBackgroundThread

        public static void assertBackgroundThread()
        Throws an IllegalArgumentException if called on the main thread.
      • isOnMainThread

        public static boolean isOnMainThread()
        Returns true if called on the main thread, false otherwise.
      • isOnBackgroundThread

        public static boolean isOnBackgroundThread()
        Returns true if called on a background thread, false otherwise.
      • createQueue

        @NonNull
        public static <T> java.util.Queue<T> createQueue​(int size)
        Creates a Queue of the given size using Glide's preferred implementation.
      • getSnapshot

        @NonNull
        public static <T> java.util.List<T> getSnapshot​(@NonNull
                                                        java.util.Collection<T> other)
        Returns a copy of the given list that is safe to iterate over and perform actions that may modify the original list.

        See #303, #375, #322, #2262.

      • bothNullOrEqual

        public static boolean bothNullOrEqual​(@Nullable
                                              java.lang.Object a,
                                              @Nullable
                                              java.lang.Object b)
        Null-safe equivalent of a.equals(b).
        See Also:
        Objects.equals(java.lang.Object, java.lang.Object)
      • bothModelsNullEquivalentOrEquals

        public static boolean bothModelsNullEquivalentOrEquals​(@Nullable
                                                               java.lang.Object a,
                                                               @Nullable
                                                               java.lang.Object b)
      • hashCode

        public static int hashCode​(int value)
      • hashCode

        public static int hashCode​(int value,
                                   int accumulator)
      • hashCode

        public static int hashCode​(float value)
      • hashCode

        public static int hashCode​(float value,
                                   int accumulator)
      • hashCode

        public static int hashCode​(@Nullable
                                   java.lang.Object object,
                                   int accumulator)
      • hashCode

        public static int hashCode​(boolean value,
                                   int accumulator)
      • hashCode

        public static int hashCode​(boolean value)