
org.swiftboot.web.result.BasePopulateListResult Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of swiftboot-web Show documentation
Show all versions of swiftboot-web Show documentation
Basic module for enterprise web applications
package org.swiftboot.web.result;
import org.swiftboot.data.model.entity.IdPersistable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
/**
* 列表查询结果抽象类
*
* @param 集合中元素的类型
* @param 元素对应的实体类类型
* @author swiftech
*/
public abstract class BasePopulateListResult, E extends IdPersistable> extends BaseListableResult {
/**
* 从实体类集合创建相对应的返回对象集合
*
* @param entities
* @return
*/
public BasePopulateListResult populateByEntities(Iterable entities) {
// TODO call populateByEntities(Iterable entities, PopulateHandler populateHandler) instead
Type genericSuperclass = getClass().getGenericSuperclass();
if (genericSuperclass == null) {
throw new RuntimeException("反射错误");
}
Class itemClass = (Class) ((ParameterizedType) genericSuperclass).getActualTypeArguments()[0];
if (itemClass == null) {
throw new RuntimeException("类定义缺少元素的类型");
}
List list = new ArrayList<>();
for (E entity : entities) {
T item = BasePopulateResult.createResult(itemClass, entity);
list.add(item);
}
this.setItems(list);
return this;
}
/**
* 从实体类集合创建相对应的返回对象集合
*
* @param entities
* @param populateHandler Handle after each element populated
* @return
* @since 1.1
*/
public BasePopulateListResult populateByEntities(Iterable entities, PopulateHandler populateHandler) {
Type genericSuperclass = getClass().getGenericSuperclass();
if (genericSuperclass == null) {
throw new RuntimeException("反射错误");
}
Class itemClass = (Class) ((ParameterizedType) genericSuperclass).getActualTypeArguments()[0];
if (itemClass == null) {
throw new RuntimeException("类定义缺少元素的类型");
}
List list = new ArrayList<>();
for (E entity : entities) {
T item = BasePopulateResult.createResult(itemClass, entity);
if (populateHandler != null) {
populateHandler.onPopulated(item, entity);
}
list.add(item);
}
this.setItems(list);
return this;
}
/**
* 处理集合填充的回调
*
* @param
* @param
*/
@FunctionalInterface
public interface PopulateHandler, E extends IdPersistable> {
/**
* 一个继承自 BasePopulateResult 的类被填充完成之后执行
*
* @param result
* @param entity
*/
void onPopulated(T result, E entity);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy