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

shz.PropHelp Maven / Gradle / Ivy

There is a newer version: 2.0
Show newest version
package shz;

import shz.cache.MapCache;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.text.Collator;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

@SuppressWarnings("unchecked")
public final class PropHelp {
    private PropHelp() {
        throw new IllegalStateException();
    }

    public static Properties getProp(InputStream is) {
        Objects.requireNonNull(is);
        Properties prop = new Properties();
        try {
            prop.load(is);
        } catch (IOException e) {
            throw PRException.of(e);
        } finally {
            IOHelp.close(is);
        }
        Properties result = new Properties();
        Enumeration en = prop.propertyNames();
        String key;
        while (en.hasMoreElements()) {
            key = (String) en.nextElement();
            String val = prop.getProperty(key);
            if (Validator.nonBlank(val)) result.setProperty(key, val);
        }
        return result;
    }

    static final class PropSetFieldStrategy extends ToObject.SetFieldStrategy {
        @Override
        public Function, Class, Collection>> supCollection() {
            return t -> (u, r) -> {
                String m = u.apply(t);
                if (Validator.isBlank(m)) return Collections.emptySet();
                return ToSet.collect(keys(t).stream().filter(Objects::nonNull).filter(k -> k.startsWith(m)).map(k -> executor(m, r)).filter(Objects::nonNull), true);
            };
        }

        @Override
        public Function, Class[], Map>> supMap() {
            return t -> (u, r) -> {
                String m = u.apply(t);
                if (Validator.isBlank(m)) return Collections.emptyMap();
                return ToMap.collect(keys(t).stream().filter(Objects::nonNull).filter(k -> k.startsWith(m)), k -> executor(m, r[0]), k -> executor(m, r[1]), true);
            };
        }

        @Override
        public Function, Class, Object>> supObject() {
            return t -> (u, r) -> {
                String m = u.apply(t);
                if (Validator.isBlank(m)) return null;
                return keys(t).stream().filter(Objects::nonNull).anyMatch(k -> k.startsWith(m)) ? executor(m, r) : null;
            };
        }

        Map map;

        @Override
        protected Collection keys(Field field) {
            return map.keySet();
        }

        @Override
        protected Object executor(String key, Class cls) {
            return ToObject.fromMap(cls, map, f -> key + "." + f.getName(), Function.identity(), this);
        }

         T fromMap(Class cls, Map map) {
            this.map = map;
            return ToObject.fromMap(cls, map, Field::getName, Function.identity(), this);
        }

         T fromMap(T t, Map map) {
            this.map = map;
            return ToObject.fromMap(t, map, Field::getName, Function.identity(), this);
        }
    }

    private static final PropSetFieldStrategy STRATEGY = new PropSetFieldStrategy();

    public static  T toObject(Class cls, Map map) {
        return ((PropSetFieldStrategy) STRATEGY.clone()).fromMap(cls, map);
    }

    public static  T toObject(T t, Map map) {
        return ((PropSetFieldStrategy) STRATEGY.clone()).fromMap(t, map);
    }

    public static  T toObject(Class cls, Properties prop) {
        return toObject(cls, propToMap(prop));
    }

    private static Map propToMap(Properties prop) {
        if (prop == null) return Collections.emptyMap();
        Enumeration en = prop.propertyNames();
        String key;
        Map map = new HashMap<>();
        while (en.hasMoreElements()) {
            key = (String) en.nextElement();
            map.put(key, prop.getProperty(key));
        }
        return map;
    }

    public static  T toObject(T t, Properties prop) {
        return toObject(t, propToMap(prop));
    }

    public static  List toObjects(Class cls, List> maps) {
        if (Validator.isEmpty(maps)) return Collections.emptyList();
        PropSetFieldStrategy strategy = (PropSetFieldStrategy) STRATEGY.clone();
        return ToList.explicitCollect(maps.stream().map(map -> strategy.fromMap(cls, map)), maps.size());
    }

    private static final MapCache, Map>> FIELD_CLASS_MAP_CACHE = new MapCache<>(128);

    public static Map> fieldClassMap(Class cls) {
        Map> result = FIELD_CLASS_MAP_CACHE.get(cls);
        if (result == null) {
            result = new HashMap<>();
            fieldClassMap0(result, cls, "");
            if (result.isEmpty()) {
                FIELD_CLASS_MAP_CACHE.put(cls, Collections.emptyMap());
                return Collections.emptyMap();
            }
            result = ToMap.get(result.size()).put(result).build();
            FIELD_CLASS_MAP_CACHE.put(cls, result);
        }
        return result;
    }

    private static void fieldClassMap0(Map> map, Class cls, String mapField) {
        AccessibleHelp.fields(cls).forEach(f -> {
            if (AccessibleHelp.isCommon(f.getType())) map.put(mapField + f.getName(), cls);
            else fieldClassMap0(map, f.getType(), mapField + f.getName() + ".");
        });
    }

    public static Map fieldValueMap(Object obj, Predicate predicate, String mapField) {
        if (obj == null) return Collections.emptyMap();
        Map map;
        if (Validator.isEmpty(mapField)) {
            Class cls = obj.getClass();
            if (AccessibleHelp.isCommon(cls) || obj instanceof Collection || obj instanceof Object[])
                return Collections.emptyMap();

            map = new HashMap<>();
            if (obj instanceof Map)
                ((Map) obj).forEach((k, v) -> fieldValueMap0(map, v, predicate, k));
            else AccessibleHelp.fields(cls).forEach(f -> {
                if (predicate != null && !predicate.test(f)) return;
                fieldValueMap0(map, AccessibleHelp.getField(f, obj), predicate, f.getName());
            });
        } else {
            map = new HashMap<>();
            fieldValueMap0(map, obj, predicate, mapField);
        }
        return map.isEmpty() ? Collections.emptyMap() : ToMap.get(map.size()).put(map).build();
    }

    private static void fieldValueMap0(Map map, Object obj, Predicate predicate, String mapField) {
        if (obj == null) return;
        Class cls = obj.getClass();
        if (AccessibleHelp.isCommon(cls)) map.put(mapField, obj.toString());
        else if (obj instanceof Map)
            ((Map) obj).forEach((k, v) -> fieldValueMap0(map, v, predicate, mapField + "." + k));
        else if (obj instanceof Collection) {
            Collection collection = (Collection) obj;
            map.put(mapField, joining(ToList.explicitCollect(collection.stream().map(e -> trans(e, predicate)), collection.size())));
        } else if (obj instanceof Object[]) {
            Object[] array = (Object[]) obj;
            map.put(mapField, joining(ToList.explicitCollect(Arrays.stream(array).map(e -> trans(e, predicate)), array.length)));
        } else AccessibleHelp.fields(cls).forEach(f -> {
            if (predicate != null && !predicate.test(f)) return;
            fieldValueMap0(map, AccessibleHelp.getField(f, obj), predicate, mapField + "." + f.getName());
        });
    }

    private static String trans(Object val, Predicate predicate) {
        if (val == null) return "";
        Class cls = val.getClass();
        if (AccessibleHelp.isCommon(cls)) return val.toString();
        if (val instanceof Map) {
            Map map = new HashMap<>();
            ((Map) val).forEach((k, v) -> fieldValueMap0(map, v, predicate, k));
            return joining(map);
        }
        if (val instanceof Collection) {
            Collection collection = (Collection) val;
            return joining(ToList.explicitCollect(collection.stream().map(e -> trans(e, predicate)), collection.size()));
        }
        if (val instanceof Object[]) {
            Object[] array = (Object[]) val;
            return joining(ToList.explicitCollect(Arrays.stream(array).map(e -> trans(e, predicate)), array.length));
        }
        Map map = new HashMap<>();
        AccessibleHelp.fields(cls).forEach(f -> {
            if (predicate != null && !predicate.test(f)) return;
            fieldValueMap0(map, AccessibleHelp.getField(f, val), predicate, f.getName());
        });
        return joining(map);
    }

    private static String joining(Map map) {
        if (map.isEmpty()) return "";
        List list = new ArrayList<>(map.keySet());
        list.sort(Collator.getInstance(Locale.CHINA));
        StringBuilder sb = new StringBuilder();
        String key = list.get(0);
        sb.append("[").append(key).append(":").append(map.get(key));
        for (int i = 1; i < list.size(); ++i) {
            key = list.get(i);
            sb.append(",").append(key).append(":").append(map.get(key));
        }
        sb.append("]");
        return sb.toString();
    }

    private static String joining(List list) {
        if (list.isEmpty()) return "";
        list.sort(Collator.getInstance(Locale.CHINA));
        return list.stream().collect(Collectors.joining(",", "[", "]"));
    }

    public static Map fieldValueMap(Object obj, Predicate predicate) {
        return fieldValueMap(obj, predicate, "");
    }

    public static Map fieldValueMap(Object obj) {
        return fieldValueMap(obj, null, "");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy