com.zhaofujun.automapper.mapping.ClassMappingManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of automapper Show documentation
Show all versions of automapper Show documentation
easy to mapping jave entity
The newest version!
package com.zhaofujun.automapper.mapping;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* 映射关系管理器,用于管理源对象、目标对象的关联关系。
*/
public class ClassMappingManager {
List classMappings = new CopyOnWriteArrayList<>();
public void registerClassMapping(ClassMapping classMapping) {
this.classMappings.add(classMapping);
}
//按源类型和目标类型获取字段列表,如果没有获取到则按
public List getFieldMappingList(Class sourceClass, Class targetClass ,boolean allowNoSetter) {
ClassMapping classMapping = getClassMapping(sourceClass, targetClass);
return classMapping.getEffectiveFieldMappingList(allowNoSetter);
}
public ClassMapping getClassMapping(Class sourceClass,Class targetClass) {
ClassMapping classMapping = classMappings.stream()
.filter(p -> p.getSourceClass().isAssignableFrom(sourceClass) && p.getTargetClass().isAssignableFrom(targetClass))
.findFirst()
.orElse(null);
if (classMapping == null) {
classMapping = new ClassMapping(sourceClass, targetClass);
classMappings.add(classMapping);
}
return classMapping;
}
}