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

com.github.leeonky.map.BaseConverter Maven / Gradle / Ivy

There is a newer version: 0.7.5
Show newest version
package com.github.leeonky.map;

import com.github.leeonky.util.BeanClass;
import ma.glasnost.orika.CustomConverter;

import java.util.*;
import java.util.stream.Collectors;

abstract class BaseConverter extends CustomConverter {

    static Map createMap(Class rawType) {
        return rawType.isInterface() ? new LinkedHashMap() : (Map) BeanClass.newInstance(rawType);
    }

    static Collection createCollection(Class rawType) {
        Collection result;
        if (rawType.isInterface()) {
            result = Set.class.isAssignableFrom(rawType) ? new LinkedHashSet() : new ArrayList<>();
        } else
            result = (Collection) BeanClass.newInstance(rawType);
        return result;
    }

    @SuppressWarnings("unchecked")
    static Object getPropertyValue(Object e, String propertyChain) {
        for (String property : propertyChain.split("\\."))
            if (!property.isEmpty())
                e = ((BeanClass) BeanClass.create(e.getClass())).getPropertyValue(e, property);
        return e;
    }

    @SuppressWarnings("unchecked")
    static Iterable wrapperEntry(Map map) {
        return (Iterable) map.entrySet().stream()
                .map(e -> new ViewListConverter.Entry((Map.Entry) e))
                .collect(Collectors.toList());
    }

    public abstract String buildConvertId();

    @Override
    public int hashCode() {
        return buildConvertId().hashCode();
    }

    @Override
    public boolean equals(Object other) {
        return (other instanceof ViewConverter) && buildConvertId().equals(((ViewConverter) other).buildConvertId());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy