
io.polyglotted.common.util.ObjConstructor Maven / Gradle / Ivy
package io.polyglotted.common.util;
import io.polyglotted.common.model.Pair;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import static io.polyglotted.common.util.MapRetriever.asMap;
import static io.polyglotted.common.util.ReflectionUtil.create;
import static io.polyglotted.common.util.ReflectionUtil.fieldValue;
import static io.polyglotted.common.util.ReflectionUtil.getCollTypeArg;
import static io.polyglotted.common.util.ReflectionUtil.getMapTypeArgs;
import static io.polyglotted.common.util.ReflectionUtil.isAssignable;
import static io.polyglotted.common.util.Sanitizer.isSanitizable;
import static io.polyglotted.common.util.Sanitizer.sanitize;
@SuppressWarnings("unchecked")
public abstract class ObjConstructor {
public static T construct(Map map, T builder) {
for (Field field : builder.getClass().getDeclaredFields()) {
Object value = map.get(field.getName());
if (value != null) {
if (value instanceof Collection) {
if (isAssignable(Set.class, field.getType())) {
fieldValue(builder, field, collectInto(field, (List>) value, new LinkedHashSet<>()));
}
else {
fieldValue(builder, field, collectInto(field, (List>) value, new LinkedList<>()));
}
}
else if (value instanceof Map) {
if (isAssignable(Map.class, field.getType())) { fieldValue(builder, field, mapInto(field, asMap(value))); }
else { fieldValue(builder, field, construct((Map) value, create(field.getType()))); }
}
else { fieldValue(builder, field, sanitize(field.getType(), value)); }
}
}
return builder;
}
private static > C collectInto(Field field, List> objects, C result) {
Class> elemClass = getCollTypeArg(field);
for (Object obj : objects) { if(obj != null) { result.add(getElem(elemClass, obj)); } }
return result;
}
private static Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy