All Downloads are FREE. Search and download functionalities are using the official Maven repository.

solid.collectors.ToJoinedString Maven / Gradle / Ivy

package solid.collectors;

import android.text.TextUtils;

import solid.functions.Func1;

/**
 * This class is a syntax enhancement around {@link TextUtils#join(CharSequence, Iterable)} method
 * to chain it with {@link solid.stream.Stream} streams.
 */
public class ToJoinedString {

    private static Func1, String> TO_JOINED_STRING = toJoinedString("");

    /**
     * Returns a method that can be used with {@link solid.stream.Stream#collect(Func1)}
     * to convert an iterable stream of {@link String} type into a joined string.
     *
     * @return a method that converts an iterable stream of {@link String} type into a joined string.
     */
    public static Func1, String> toJoinedString() {
        return TO_JOINED_STRING;
    }

    /**
     * Returns a method that can be used with {@link solid.stream.Stream#collect(Func1)}
     * to convert an iterable stream of {@link String} type into a joined string.
     *
     * @param delimiter a delimiter
     * @return a method that converts an iterable stream of {@link String} type into a joined string.
     */
    public static Func1, String> toJoinedString(final String delimiter) {
        return new Func1, String>() {
            @Override
            public String call(Iterable iterable) {
                StringBuilder builder = new StringBuilder();
                boolean first = true;
                for (String string : iterable) {
                    if (first)
                        first = false;
                    else
                        builder.append(delimiter);
                    builder.append(string);
                }
                return builder.toString();
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy