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

io.vilya.common.function.Converter Maven / Gradle / Ivy

package io.vilya.common.function;

import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author iamaprin
 * @since 2018年6月28日 下午9:31:43
 * @see converter
 */
public class Converter {

    private final Function fromLeft;

    private final Function fromRight;

    public Converter(final Function fromLeft, final Function fromRight) {
        this.fromLeft = fromLeft;
        this.fromRight = fromRight;
    }

    public final U convertFromLeft(final T dto) {
        return fromLeft.apply(dto);
    }

    public final T convertFromRight(final U entity) {
        return fromRight.apply(entity);
    }

    public final List createFromLeft(final Collection dtos) {
        return dtos.stream().map(this::convertFromLeft).collect(Collectors.toList());
    }

    public final List createFromERight(final Collection entities) {
        return entities.stream().map(this::convertFromRight).collect(Collectors.toList());
    }

}