Class Utils

java.lang.Object
ru.bgcrm.util.Utils

public class Utils extends Object
  • Field Details

    • DEFAULT_DELIM

      public static final String DEFAULT_DELIM
      Default delimiter: ", "
      See Also:
    • HEX

      public static final char[] HEX
    • HEX_LOWERCASE

      public static final char[] HEX_LOWERCASE
  • Constructor Details

    • Utils

      public Utils()
  • Method Details

    • parseInt

      public static int parseInt(String str)
      Converts a string to an int, return 0 in case of error
      Parameters:
      str - the input string
      Returns:
      the number converted from the string, or 0 in case of error
    • parseInt

      public static int parseInt(String str, int defaultValue)
      Converts a string to an int, return a default value in case of error
      Parameters:
      str - the input string
      defaultValue - the default value in case of error
      Returns:
      the number converted from the string, or defaultValue in case of error
    • parseLong

      public static long parseLong(String str)
      Converts a string to a long, return 0 in case of error
      Parameters:
      str - the input string
      Returns:
      the number converted from the string, or 0 in case of error
    • parseLong

      public static long parseLong(String str, long defaultValue)
      Converts a string to a long, return a default value in case of error
      Parameters:
      str - the input string
      defaultValue - the default value in case of error
      Returns:
      the number converted from the string, or defaultValue in case of error
    • parseBigDecimal

      public static BigDecimal parseBigDecimal(Object obj, BigDecimal defaultValue)
      Converts an object's string representation to a decimal object
      Parameters:
      obj - the object
      defaultValue - the default value in case of parsing exception
      Returns:
      decimal object, defaultValue for any unparsable value
    • parseBigDecimal

      public static BigDecimal parseBigDecimal(Object obj)
      Converts an object's string representation to a decimal object
      Parameters:
      obj - the object
      Returns:
      decimal object, null when obj was null, BigDecimal.ZERO for any unparsable value
    • parseBoolean

      public static boolean parseBoolean(String str)
      Converts a string to a boolean, return false in case of error
      Parameters:
      str - the input string
      Returns:
      the boolean value converted from the string, or false in case of error
    • isStringNumber

      public static boolean isStringNumber(String str)
      Checks whether the string contains only digits, i.e. no letters
      Parameters:
      str - the input string
      Returns:
      true if the string doesn't contain letters
    • parseBoolean

      public static Boolean parseBoolean(String str, Boolean defaultValue)
      Converts a string to a boolean
      Parameters:
      str - the string
      defaultValue - the default value
      Returns:
      true - for str "1", "TRUE", "YES", "ON" case insensitive; false - for str "0", "FALSE", "NO" case insensitive; all other cases - defaultValue
    • booleanToStringInt

      public static final String booleanToStringInt(boolean value)
      Converts a boolean value to a string "1" or "0"
      Parameters:
      value - the boolean value
      Returns:
      "1" for true, "0" for false
    • maskNull

      public static final <T> T maskNull(T value, T defaultValue)
      Returns the value, or a default value if the value is null
      Parameters:
      value - the value
      defaultValue - the default value
      Returns:
      value, or defaultValue if value is null
    • maskNull

      public static final String maskNull(String value)
      Converts the input string to an empty one if it is null
      Parameters:
      value - the input string
      Returns:
      value, or an empty string if value is null
    • maskNullDecimal

      public static final BigDecimal maskNullDecimal(BigDecimal value)
      Returns the input value if it is not null, otherwise BigDecimal.ZERO
      Parameters:
      value - the input value
      Returns:
      value, or BigDecimal.ZERO if value is null
    • emptyToNull

      public static final <T, C extends Collection<T>> C emptyToNull(C value)
      Converts to null an empty collection
      Parameters:
      value - the input collection
      Returns:
      the input collection, if it is not empty, or null
    • maskEmpty

      public static final String maskEmpty(String value, String defaultValue)
      Converts the input string to a default value if it is null or empty
      Parameters:
      value - the input string
      defaultValue - the default value
      Returns:
      value, or defaultValue if value is null or empty
    • isEmptyString

      public static final boolean isEmptyString(String value)
      Checks whether the string is empty or null
      Parameters:
      value - the checked string
      Returns:
      true if empty or null
    • isBlankString

      public static final boolean isBlankString(String value)
      Checks whether the string is empty, consists only of whitespace chars, or is null
      Parameters:
      value - the checked string
      Returns:
      true if empty, blank, or null
    • notEmptyString

      public static final boolean notEmptyString(String value)
      Checks whether the string is not empty and not null
      Parameters:
      value - the checked string
      Returns:
      false if empty or null
    • notBlankString

      public static final boolean notBlankString(String value)
      Checks if string value is blank
      Parameters:
      value - checked value
      Returns:
      false when value is null, empty or has only whitespace chars
    • notBlankStrings

      public static final boolean notBlankStrings(String... value)
      Checks all the passed values with notBlankString(String)
      Parameters:
      value - values
      Returns:
      value is not null, all the values aren't blank
    • countLines

      public static final int countLines(String value)
      Counts the number of lines in string
      Parameters:
      value - the string
      Returns:
      the count of \n chars
    • isPositive

      public static final boolean isPositive(Integer value)
      Is the integer value greater than zero. To do not create lambda functions.
      Parameters:
      value - the value
      Returns:
      true if value is greater than zero
    • isValidEmail

      public static final boolean isValidEmail(String value)
      Is the string a valid Email address
      Parameters:
      value - the checked string
      Returns:
      true if the string is a valid email address
    • toString

      public static final String toString(Collection<?> valuesList)
      Converts a collection to a string joined with DEFAULT_DELIM
      Parameters:
      valuesList - the collection
      Returns:
      the resulting string, or an empty string if the collection is empty
    • toString

      public static final String toString(Collection<?> valuesList, String emptyValue, String delim)
      Converts a collection to a string joined with the delimiter
      Parameters:
      valuesList - the collection
      emptyValue - the value to return for an empty list
      delim - the delimiter
      Returns:
      the resulting string
    • toIntegerList

      public static final List<Integer> toIntegerList(String valuesStr)
      Converts a string, delimited by commas or semicolons, to a list of Integer
      Parameters:
      valuesStr - the input string
      Returns:
      the resulting list
    • toIntegerList

      public static final List<Integer> toIntegerList(String valuesStr, String delims)
      Converts a string, delimited by the given chars, to a list of Integer
      Parameters:
      valuesStr - the input string
      delims - the delimiter chars
      Returns:
      the resulting list
    • toIntegerSet

      public static final Set<Integer> toIntegerSet(String valuesStr)
      Converts a string, delimited by commas, to a set of Integer
      Parameters:
      valuesStr - the input string
      Returns:
      the resulting set
    • toSet

      public static final Set<String> toSet(String valuesStr, String delims)
      Converts a string, delimited by the given chars, to a set of strings
      Parameters:
      valuesStr - the input string
      delims - the delimiter chars
      Returns:
      the resulting set
    • toSet

      public static final Set<String> toSet(String valuesStr)
      Converts a string, delimited by commas or semicolons, to a set of strings
      Parameters:
      valuesStr - the input string
      Returns:
      the resulting set
    • toList

      public static final List<String> toList(String valuesStr)
      Converts a string, delimited by commas or semicolons, to a list of strings
      Parameters:
      valuesStr - the input string
      Returns:
      the resulting list
    • toList

      public static final List<String> toList(String value, String delims)
      Converts a string with arbitrary delimiter chars to list of strings
      Parameters:
      value - incoming string
      delims - delimiter chars
      Returns:
      list of tokens, each of that is not empty string
    • toText

      public static final String toText(List<String> config, String separator)
      Joins configuration lines to a single string with a separator
      Parameters:
      config - configuration lines
      separator - separator placed after each line
      Returns:
      joined string
    • getObjectIdsList

      public static final <T extends IdTitle> List<Integer> getObjectIdsList(Collection<T> list)
      Returns a list of object IDs
      Parameters:
      list - the objects
      Returns:
      the resulting list
    • getObjectIdsSet

      public static final <T extends IdTitle> Set<Integer> getObjectIdsSet(Collection<T> list)
      Returns a set of object IDs
      Parameters:
      list - the objects
      Returns:
      the resulting set
    • getObjectIds

      public static final <T extends Id<Integer>> String getObjectIds(Collection<T> values)
      Comma separated object IDs
      Parameters:
      values - the objects
      Returns:
      the resulting string
    • getObjectIds

      public static final <T extends Id<Integer>> String getObjectIds(Collection<T> values, String startValues)
      Comma separated object IDs
      Parameters:
      values - the objects
      startValues - beginning of the resulting string
      Returns:
      the resulting string
    • getObjectIds

      public static <T extends Id<Integer>> String getObjectIds(Collection<T> values, String startValues, String delim)
      Separated object IDs
      Parameters:
      values - the objects
      startValues - beginning of the resulting string
      delim - the separator
      Returns:
      the resulting string
    • getObjectTitles

      public static final <T extends Title> String getObjectTitles(Collection<T> list)
      Concatenates object titles to a comma separated string
      Parameters:
      list - the list of titled objects
      Returns:
      comma separated string
    • getObjectTitles

      public static <T extends IdTitle> String getObjectTitles(List<T> fullList, Set<Integer> selectedIds)
      Concatenates object titles to a comma separated string
      Parameters:
      fullList - the full object list, defines the resulting order
      selectedIds - the selected IDs
      Returns:
      comma separated string
    • getObjectTitles

      public static final <T extends IdTitle> String getObjectTitles(Map<Integer,T> fullMap, List<Integer> selectedIds)
      Concatenates object titles to a comma separated string
      Parameters:
      fullMap - the full object map
      selectedIds - the selected IDs, defines the resulting order
      Returns:
      comma separated string
    • getObjectList

      public static final <T extends IdTitle> List<T> getObjectList(List<T> fullList, Collection<Integer> selectedIds)
      Selects objects sub-list from a given full list with IDs presented in a collection
      Parameters:
      fullList - the full list
      selectedIds - the IDs collection
      Returns:
      the resulting sub-list
    • getObjectList

      public static final <T extends IdTitle> List<T> getObjectList(Map<Integer,T> fullMap, List<Integer> selectedIds)
      Selects objects list from a full map by a given IDs list in the same order
      Parameters:
      fullMap - the full objects map
      selectedIds - the IDs list
      Returns:
      the resulting list
    • parseIdTitleList

      public static final List<IdTitle> parseIdTitleList(String value)
      Parses a list of objects from a string like "id:title; id:title"
      Parameters:
      value - the input string
      Returns:
      the resulting list
    • parseIdTitleList

      public static final List<IdTitle> parseIdTitleList(String value, String noPairValue)
      Parses a list of objects from a string like "id:title; id:title". If the title part of a pair is missing, noPairValue is used instead.
      Parameters:
      value - the input string
      noPairValue - the default title value for entries without one
      Returns:
      the resulting list
    • addSetupPair

      public static final void addSetupPair(StringBuilder data, String prefix, String param, String value)
      Appends a "prefix param=value" line to the data
      Parameters:
      data - the target buffer
      prefix - the line prefix
      param - the parameter name
      value - the parameter value
    • addCommaSeparated

      public static final void addCommaSeparated(StringBuilder result, String value)
      Appends a new value to the end of the string, separating it with a comma and a space if it is not the first one
      Parameters:
      result - the target buffer
      value - the value to append
    • addSeparated

      public static final void addSeparated(StringBuilder result, String separator, String value)
      Appends a new value to the end of the string, separating it with the given separator if it is not the first one
      Parameters:
      result - the target buffer
      separator - the separator
      value - the value to append
    • getDigest

      public static String getDigest(String value)
      Computes the MD5 digest HEX string of the input string, using UTF-8 encoding
      Parameters:
      value - the input string
      Returns:
      digest HEX string or null in case of any error
    • getDigest

      public static String getDigest(String value, String charset)
      Returns the HEX string representation of the MD5 hash of the input string
      Parameters:
      value - the input data
      charset - the encoding
      Returns:
      digest HEX string or null in case of any error
    • getDigest

      public static String getDigest(byte[] value)
      HEX representation of MD5 digest
      Parameters:
      value - digest basic
      Returns:
      digest HEX string or null in case of any error
    • getTmpDir

      public static String getTmpDir()
      System temp directory
      Returns:
      value of system property 'java.io.tmpdir', or '/tmp' if it is missing
    • createDirectoryIfNoExistInWorkDir

      public static final File createDirectoryIfNoExistInWorkDir(String dirName)
      Creates a directory with a given name in working directory if it does not exist
      Parameters:
      dirName - the directory name
      Returns:
      created or existing directory
    • substringAfter

      public static final String substringAfter(String value, String token, int num)
      Returns the remainder of the string after the num-th occurrence of the token
      Parameters:
      value - the input string
      token - the token to search for
      num - the occurrence number
      Returns:
      the remaining substring, or an empty string if not found
    • generateSecret

      public static final String generateSecret()
      Returns:
      generated random string with 32 ASCII chars
    • getFirst

      public static <T> T getFirst(Collection<T> collection)
      Parameters:
      collection - collection of elements
      Returns:
      the first element from collection, or null if collection is null or empty
    • escapeXml

      public static String escapeXml(String value)
      Replaces XML markup symbols with special codes
      Parameters:
      value - the input string
      Returns:
      the input string with replacements
    • htmlEncode

      public static String htmlEncode(String value)
      Replaces only HTML tags with escaped codes
      Parameters:
      value - the input string
      Returns:
      the input string with replacements
    • getOpenId

      public static int getOpenId(String url)
      Extracts entity ID from URL
      Parameters:
      url - URL
      Returns:
      extracted positive ID or 0 if couldn't extract
    • setFileNameHeaders

      public static void setFileNameHeaders(javax.servlet.http.HttpServletResponse response, String fileName)
      Sets HTTP headers for downloaded file
      Parameters:
      response - the HTTP response
      fileName - the file name
    • errorAndExit

      public static void errorAndExit(int code, String message)
      Writes error message and exits the running application
      Parameters:
      code - exit code
      message - message
    • newInstance

      @Deprecated public static Object newInstance(String className, Object... args) throws Exception
      Deprecated.
      The method does special JSP-specific type converting, therefore must not be called from Java code
      Throws:
      Exception
      See Also:
    • hasClass

      public static boolean hasClass(Object o, String... names)
      Checks if object is instance one of classes
      Parameters:
      o - the object to check
      names - class names
      Returns:
      if object o is instance any of names classes
    • getSystemProperty

      public static String getSystemProperty(String key, String defaultValue)
      Retrieves a property value from System.getProperty(String)
      Parameters:
      key - the key is prepended by bgerp.
      defaultValue - the default value if no property found
      Returns:
      the property value, or defaultValue if not found
    • format

      public static String format(BigDecimal value)
      Formats a decimal value to a regular string without trailing zeros
      Parameters:
      value - the decimal value
      Returns:
      empty string for null value, or formatted string