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

io.github.linpeilie.Converter Maven / Gradle / Ivy

There is a newer version: 1.4.4
Show newest version
package io.github.linpeilie;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class Converter {

    private final ConverterFactory converterFactory;

    public Converter() {
        this.converterFactory = new DefaultConverterFactory();
    }

    public Converter(final ConverterFactory converterFactory) {
        this.converterFactory = converterFactory;
    }

    @SuppressWarnings("unchecked")
    public  T convert(S source, Class targetType) {
        if (source == null) {
            return null;
        }
        BaseMapper mapper = (BaseMapper) converterFactory.getMapper(source.getClass(), targetType);
        if (mapper != null) {
            return mapper.convert(source);
        }
        throw new ConvertException(
            "cannot find converter from " + source.getClass().getSimpleName() + " to " + targetType.getSimpleName());
    }

    @SuppressWarnings("unchecked")
    public  T convert(S source, T target) {
        if (source == null) {
            return null;
        }
        if (target == null) {
            return null;
        }
        BaseMapper mapper = (BaseMapper) converterFactory.getMapper(source.getClass(), target.getClass());
        if (mapper != null) {
            return mapper.convert(source, target);
        }
        throw new ConvertException("cannot find converter from " + source.getClass().getSimpleName() + " to " +
                                   target.getClass().getSimpleName());
    }

    public  List convert(List source, Class targetType) {
        if (source == null || source.isEmpty()) {
            return new ArrayList<>();
        }
        return source.stream().map(item -> convert(item, targetType)).collect(Collectors.toList());
    }


    @SuppressWarnings("unchecked")
    public  T convert(S source, Class target, CycleAvoidingMappingContext context) {
        if (source == null) {
            return null;
        }
        BaseCycleAvoidingMapper mapper =
            (BaseCycleAvoidingMapper) converterFactory.getCycleAvoidingMapper(source.getClass(), target);
        if (mapper != null) {
            return mapper.convert(source, context);
        }
        throw new ConvertException("cannot find converter from " + source.getClass().getSimpleName() + " to " +
                                   target.getSimpleName());
    }

    public  List convert(List source, Class targetType, CycleAvoidingMappingContext context) {
        if (source == null || source.isEmpty()) {
            return new ArrayList<>();
        }
        return source.stream().map(item -> convert(item, targetType, context)).collect(Collectors.toList());
    }



    public  T convert(Map map, Class target) {
        if (map == null || map.isEmpty()) {
            return null;
        }
        if (map.values().stream().allMatch(Objects::isNull)) {
            return null;
        }
        final BaseMapMapper mapper = converterFactory.getMapMapper(target);
        if (mapper != null) {
            return mapper.convert(map);
        }
        throw new ConvertException("cannot find converter from " + map.getClass().getName() + " to " +
                                   target.getSimpleName());
    }

}