Class Joiner


  • public final class Joiner
    extends Object
    Inspired by Googles Joiner.

    It uses internally the String.join(CharSequence, Iterable) implementation of java and provides a guava like wrapper. It focuses on the simplified Joining and omits the Map based variants.

    Usage

     assertEquals("key=value", Joiner.on('=').join("key", "value"));
     assertEquals("key=no value", Joiner.on('=').useForNull("no value").join("key", null));
     assertEquals("key", Joiner.on('=').skipNulls().join("key", null));
     assertEquals("key", Joiner.on('=').skipEmptyStrings().join("key", ""));
     assertEquals("key", Joiner.on('=').skipBlankStrings().join("key", " "));
     

    Migrating from Guava

    In order to migrate for most case you only need to replace the package name on the import.

    Changes to Guavas-Joiner

    In case of content to be joined containing null-values and not set to skip nulls, skipNulls() it does not throw an NullPointerException but writes "null" for each null element. You can define a different String by calling useForNull(String)

    In addition to skipEmptyStrings() it provides a variant skipBlankStrings()

    Author:
    Oliver Wolff
    • Method Detail

      • on

        public static Joiner on​(String separator)
        Returns a Joiner that uses the given fixed string as a separator. For example, Joiner.on("-").join("foo", "bar") returns a String "foo-bar"
        Parameters:
        separator - the literal, nonempty string to recognize as a separator
        Returns:
        a Joiner, with default settings, that uses that separator
      • on

        public static Joiner on​(char separator)
        Returns a Joiner that uses the given fixed string as a separator. For example, Joiner.on('-').join("foo", "bar") returns a String "foo-bar"
        Parameters:
        separator - the literal, nonempty string to recognize as a separator
        Returns:
        a Joiner, with default settings, that uses that separator
      • useForNull

        public Joiner useForNull​(String nullText)
        Parameters:
        nullText - to be used as substitution for null elements
        Returns:
        a joiner with the same behavior as this one, except automatically substituting nullText for any provided null elements.
      • skipNulls

        public Joiner skipNulls()
        Returns:
        a joiner with the same behavior as this one, except automatically skipping null-values
      • skipEmptyStrings

        public Joiner skipEmptyStrings()
        Returns:
        a joiner with the same behavior as this one, except automatically skipping String-values that evaluate to an empty String
      • join

        public String join​(Iterable<?> parts)
        Parameters:
        parts - to be joined
        Returns:
        a string containing the string representation of each of parts, using the previously configured separator between each.
      • join

        public String join​(Iterator<?> parts)
        Parameters:
        parts - to be joined
        Returns:
        a string containing the string representation of each of parts, using the previously configured separator between each.
      • join

        public String join​(Object... parts)
        Parameters:
        parts - to be joined
        Returns:
        a string containing the string representation of each of parts, using the previously configured separator between each.