Class Utils


  • public final class Utils
    extends java.lang.Object
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String HTTP  
      static java.lang.String HTTPS  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void checkUrl​(java.lang.String pattern, java.lang.String url)
      Check if the url matches the pattern.
      static java.lang.String decodeUrlUtf8​(java.lang.String url)
      Decodes a URL using the UTF-8 character set.
      static java.lang.String encodeUrlUtf8​(java.lang.String string)
      Encodes a string to URL format using the UTF-8 character set.
      static java.lang.String followGoogleRedirectIfNeeded​(java.lang.String url)
      If the provided url is a Google search redirect, then the actual url is extracted from the url= query value and returned, otherwise the original url is returned.
      static java.lang.String getBaseUrl​(java.lang.String url)  
      static java.lang.String getQueryValue​(java.net.URL url, java.lang.String parameterName)
      Get the value of a URL-query by name.
      static java.lang.String getStringResultFromRegexArray​(java.lang.String input, java.lang.String[] regexes)
      Find the result of an array of string regular expressions inside an input on the first group (0).
      static java.lang.String getStringResultFromRegexArray​(java.lang.String input, java.lang.String[] regexes, int group)
      Find the result of an array of string regular expressions inside an input on a specific group.
      static java.lang.String getStringResultFromRegexArray​(java.lang.String input, java.util.regex.Pattern[] regexes)
      Find the result of an array of Patterns inside an input on the first group (0).
      static java.lang.String getStringResultFromRegexArray​(java.lang.String input, java.util.regex.Pattern[] regexes, int group)
      Find the result of an array of Patterns inside an input on a specific group.
      static boolean isBlank​(java.lang.String string)  
      static boolean isHTTP​(java.net.URL url)  
      static boolean isNullOrEmpty​(java.lang.String str)  
      static boolean isNullOrEmpty​(java.util.Collection<?> collection)
      Checks if a collection is null or empty.
      static <K,​V>
      boolean
      isNullOrEmpty​(java.util.Map<K,​V> map)
      Checks if a map is null or empty.
      static java.lang.String join​(java.lang.String delimiter, java.lang.String mapJoin, java.util.Map<? extends java.lang.CharSequence,​? extends java.lang.CharSequence> elements)  
      static long mixedNumberWordToLong​(java.lang.String numberWord)
      Convert a mixed number word to a long.
      static java.lang.String nonEmptyAndNullJoin​(java.lang.CharSequence delimiter, java.lang.String... elements)
      Concatenate all non-null, non-empty and strings which are not equal to "null".
      static java.lang.String removeMAndWWWFromUrl​(java.lang.String url)  
      static java.lang.String removeNonDigitCharacters​(java.lang.String toRemove)
      Remove all non-digit characters from a string.
      static java.lang.String removeUTF8BOM​(java.lang.String s)  
      static java.lang.String replaceHttpWithHttps​(java.lang.String url)  
      static java.net.URL stringToURL​(java.lang.String url)
      Convert a string to a URL object.
      • Methods inherited from class java.lang.Object

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

      • encodeUrlUtf8

        public static java.lang.String encodeUrlUtf8​(java.lang.String string)
        Encodes a string to URL format using the UTF-8 character set.
        Parameters:
        string - The string to be encoded.
        Returns:
        The encoded URL.
      • decodeUrlUtf8

        public static java.lang.String decodeUrlUtf8​(java.lang.String url)
        Decodes a URL using the UTF-8 character set.
        Parameters:
        url - The URL to be decoded.
        Returns:
        The decoded URL.
      • removeNonDigitCharacters

        @Nonnull
        public static java.lang.String removeNonDigitCharacters​(@Nonnull
                                                                java.lang.String toRemove)
        Remove all non-digit characters from a string.

        Examples:

        • 1 234 567 views -> 1234567
        • $31,133.124 -> 31133124
        Parameters:
        toRemove - string to remove non-digit chars
        Returns:
        a string that contains only digits
      • mixedNumberWordToLong

        public static long mixedNumberWordToLong​(java.lang.String numberWord)
                                          throws java.lang.NumberFormatException,
                                                 ParsingException
        Convert a mixed number word to a long.

        Examples:

        • 123 -> 123
        • 1.23K -> 1230
        • 1.23M -> 1230000
        Parameters:
        numberWord - string to be converted to a long
        Returns:
        a long
        Throws:
        java.lang.NumberFormatException
        ParsingException
      • checkUrl

        public static void checkUrl​(java.lang.String pattern,
                                    java.lang.String url)
                             throws ParsingException
        Check if the url matches the pattern.
        Parameters:
        pattern - the pattern that will be used to check the url
        url - the url to be tested
        Throws:
        ParsingException
      • replaceHttpWithHttps

        public static java.lang.String replaceHttpWithHttps​(java.lang.String url)
      • getQueryValue

        @Nullable
        public static java.lang.String getQueryValue​(@Nonnull
                                                     java.net.URL url,
                                                     java.lang.String parameterName)
        Get the value of a URL-query by name.

        If an url-query is give multiple times, only the value of the first query is returned.

        Parameters:
        url - the url to be used
        parameterName - the pattern that will be used to check the url
        Returns:
        a string that contains the value of the query parameter or null if nothing was found
      • stringToURL

        @Nonnull
        public static java.net.URL stringToURL​(java.lang.String url)
                                        throws java.net.MalformedURLException
        Convert a string to a URL object.

        Defaults to HTTP if no protocol is given.

        Parameters:
        url - the string to be converted to a URL-Object
        Returns:
        a URL object containing the url
        Throws:
        java.net.MalformedURLException
      • isHTTP

        public static boolean isHTTP​(@Nonnull
                                     java.net.URL url)
      • removeMAndWWWFromUrl

        public static java.lang.String removeMAndWWWFromUrl​(java.lang.String url)
      • removeUTF8BOM

        @Nonnull
        public static java.lang.String removeUTF8BOM​(@Nonnull
                                                     java.lang.String s)
      • followGoogleRedirectIfNeeded

        public static java.lang.String followGoogleRedirectIfNeeded​(java.lang.String url)
        If the provided url is a Google search redirect, then the actual url is extracted from the url= query value and returned, otherwise the original url is returned.
        Parameters:
        url - the url which can possibly be a Google search redirect
        Returns:
        an url with no Google search redirects
      • isNullOrEmpty

        public static boolean isNullOrEmpty​(java.lang.String str)
      • isNullOrEmpty

        public static boolean isNullOrEmpty​(java.util.Collection<?> collection)
        Checks if a collection is null or empty.

        This method can be also used for JsonArrays.

        Parameters:
        collection - the collection on which check if it's null or empty
        Returns:
        whether the collection is null or empty
      • isNullOrEmpty

        public static <K,​V> boolean isNullOrEmpty​(java.util.Map<K,​V> map)
        Checks if a map is null or empty.

        This method can be also used for JsonObjects.

        Parameters:
        map - the map on which check if it's null or empty
        Returns:
        whether the map is null or empty
      • isBlank

        public static boolean isBlank​(java.lang.String string)
      • join

        @Nonnull
        public static java.lang.String join​(java.lang.String delimiter,
                                            java.lang.String mapJoin,
                                            @Nonnull
                                            java.util.Map<? extends java.lang.CharSequence,​? extends java.lang.CharSequence> elements)
      • nonEmptyAndNullJoin

        @Nonnull
        public static java.lang.String nonEmptyAndNullJoin​(java.lang.CharSequence delimiter,
                                                           java.lang.String... elements)
        Concatenate all non-null, non-empty and strings which are not equal to "null".
      • getStringResultFromRegexArray

        @Nonnull
        public static java.lang.String getStringResultFromRegexArray​(@Nonnull
                                                                     java.lang.String input,
                                                                     @Nonnull
                                                                     java.lang.String[] regexes)
                                                              throws Parser.RegexException
        Find the result of an array of string regular expressions inside an input on the first group (0).
        Parameters:
        input - the input on which using the regular expressions
        regexes - the string array of regular expressions
        Returns:
        the result
        Throws:
        Parser.RegexException - if none of the patterns match the input
      • getStringResultFromRegexArray

        @Nonnull
        public static java.lang.String getStringResultFromRegexArray​(@Nonnull
                                                                     java.lang.String input,
                                                                     @Nonnull
                                                                     java.util.regex.Pattern[] regexes)
                                                              throws Parser.RegexException
        Find the result of an array of Patterns inside an input on the first group (0).
        Parameters:
        input - the input on which using the regular expressions
        regexes - the Pattern array
        Returns:
        the result
        Throws:
        Parser.RegexException - if none of the patterns match the input
      • getStringResultFromRegexArray

        @Nonnull
        public static java.lang.String getStringResultFromRegexArray​(@Nonnull
                                                                     java.lang.String input,
                                                                     @Nonnull
                                                                     java.lang.String[] regexes,
                                                                     int group)
                                                              throws Parser.RegexException
        Find the result of an array of string regular expressions inside an input on a specific group.
        Parameters:
        input - the input on which using the regular expressions
        regexes - the string array of regular expressions
        group - the group to match
        Returns:
        the result
        Throws:
        Parser.RegexException - if none of the patterns match the input, or at least in the specified group
      • getStringResultFromRegexArray

        @Nonnull
        public static java.lang.String getStringResultFromRegexArray​(@Nonnull
                                                                     java.lang.String input,
                                                                     @Nonnull
                                                                     java.util.regex.Pattern[] regexes,
                                                                     int group)
                                                              throws Parser.RegexException
        Find the result of an array of Patterns inside an input on a specific group.
        Parameters:
        input - the input on which using the regular expressions
        regexes - the Pattern array
        group - the group to match
        Returns:
        the result
        Throws:
        Parser.RegexException - if none of the patterns match the input, or at least in the specified group