Class Utils


  • public class Utils
    extends java.lang.Object
    Various utilities
    • Constructor Summary

      Constructors 
      Constructor Description
      Utils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T extends java.lang.Comparable<T>>
      T
      clamp​(T value, T min, T max)
      Clamps a comparable value in a specified range.
      static <K,​V>
      java.util.HashMap<K,​V>
      deepCopyHashMap​(java.util.HashMap<K,​V> hashMap)
      Creates and returns a deep copy of a hash map.
      static java.lang.StringBuilder trimEnd​(java.lang.StringBuilder text)
      Trims trailing whitespaces off text.
      static boolean truncate​(java.lang.StringBuilder text, int maxLength)
      Truncates text by cutting it off and adding an ellipsis to the end if necessary.
      static java.lang.String truncate​(java.lang.String text, int maxLength)
      Truncates text by cutting it off and adding an ellipsis to the end if necessary.
      • Methods inherited from class java.lang.Object

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

      • Utils

        public Utils()
    • Method Detail

      • deepCopyHashMap

        public static <K,​V> java.util.HashMap<K,​V> deepCopyHashMap​(java.util.HashMap<K,​V> hashMap)
        Creates and returns a deep copy of a hash map.

        All entries' values are copied and placed into a new hashmap that contains the same key/value pairs

        Type Parameters:
        K - The hash map's key type
        V - The hash map's value type
        Parameters:
        hashMap - The map that will be deep copied
        Returns:
        A new deep copy
      • clamp

        public static <T extends java.lang.Comparable<T>> T clamp​(T value,
                                                                  T min,
                                                                  T max)
        Clamps a comparable value in a specified range.
        Type Parameters:
        T - A comparable type
        Parameters:
        value - The value to be clamped
        min - The minimum value in the range
        max - The maximum value in the range
        Returns:
        A clamped value within the specified bounds
      • truncate

        public static java.lang.String truncate​(java.lang.String text,
                                                int maxLength)
        Truncates text by cutting it off and adding an ellipsis to the end if necessary.
        Parameters:
        text - The text to truncate
        maxLength - The maximum amount of characters
        Returns:
        The text, truncated if necessary
      • truncate

        public static boolean truncate​(java.lang.StringBuilder text,
                                       int maxLength)
        Truncates text by cutting it off and adding an ellipsis to the end if necessary.
        Parameters:
        text - The text to truncate
        maxLength - The maximum amount of characters
        Returns:
        True only if the text has been truncated
      • trimEnd

        public static java.lang.StringBuilder trimEnd​(java.lang.StringBuilder text)
        Trims trailing whitespaces off text.
        Parameters:
        text - The text to truncate
        Returns:
        The text, truncated if necessary