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

com.spring.boxes.dollar.JoiningUtils Maven / Gradle / Ivy

package com.spring.boxes.dollar;


import com.google.common.collect.Lists;
import org.apache.commons.collections4.ListUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

public class JoiningUtils {

    public static  String joinArray(E... es) {
        return joinArray(StringUtils.COMMA, es);
    }

    public static  String joinArray(String split, E... es) {
        Objects.requireNonNull(es, "args es is null.");
        List list = Arrays.stream(es).collect(Collectors.toList());
        return joinList(list, split);
    }

    public static  String joinList(List coll) {
        return joinList(coll, StringUtils.COMMA);
    }

    public static  String joinList(List coll, String split) {
        return ListUtils.emptyIfNull(coll).stream().map(String::valueOf).collect(Collectors.joining(split));
    }

    public static List asLongList(String source) {
        return asLongList(source, null);
    }

    public static List asLongList(String source, String split) {
        return asList(source, getSplit(split), Long::parseLong);
    }

    public static List asShortList(String source) {
        return asShortList(source, null);
    }

    public static List asShortList(String source, String split) {
        return asList(source, getSplit(split), Short::parseShort);
    }

    public static List asIntegerList(String source) {
        return asIntegerList(source, null);
    }

    public static List asIntegerList(String source, String split) {
        return asList(source, getSplit(split), Integer::parseInt);
    }

    public static List asFloatList(String source) {
        return asFloatList(source, null);
    }

    public static List asFloatList(String source, String split) {
        return asList(source, getSplit(split), Float::parseFloat);
    }

    public static List asDoubleList(String source) {
        return asDoubleList(source, null);
    }

    public static List asDoubleList(String source, String split) {
        return asList(source, getSplit(split), Double::parseDouble);
    }

    public static List asStringList(String source) {
        return asStringList(source, null);
    }

    public static List asStringList(String source, String split) {
        return asList(source, getSplit(split), String::toString);
    }

    public static String getSplit(String split) {
        return StringUtils.isBlank(split) ? StringUtils.COMMA : split;
    }

    public static  List asList(String source, String split, Function mapper) {
        if (StringUtils.isBlank(source)) {
            return Lists.newArrayList();
        }
        return Arrays.stream(source.split(split)).filter(StringUtils::isNotBlank).peek(StringUtils::trim).map(mapper).distinct().collect(Collectors.toList());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy