Commit b2a3d6e0 by Jan Hrabal

fix

parent cfc11eb8
package com.jh.boot.web;
import com.jh.boot.jpa.AbstractIdEntity;
import com.jh.boot.web.list.Page;
import com.jh.boot.web.list.SortTrend;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
public class Converter {
private Converter() {}
public static <S, D> D convert(S source, Function<S, D> mappingFunction) {
if (source == null || mappingFunction == null) {
return null;
}
return mappingFunction.apply(source);
}
public static <S, D> List<D> convertList(List<S> list, Function<S, D> mappingFunction) {
if (list == null) {
return null;
}
return list.stream().map(mappingFunction).collect(Collectors.toList());
}
public static <S, D> Page<D> convertPage(Page<S> page, Function<S, D> mappingFunction) {
if (page == null || mappingFunction == null) {
return null;
}
List<D> content = new ArrayList<>();
if (page.getContent() != null) {
for (S s : page.getContent()) {
content.add(mappingFunction.apply(s));
}
}
return new Page<D>(page.getPage(), page.getPagesCount(), page.getTotalElements(), content, page.getSortBy(), SortTrend.parse(page.getTrend()));
}
/**
* Helper function to safely get id
*
* @param entity
* @return
*/
public static Long getId(AbstractIdEntity entity) {
return entity == null ? null : entity.getId();
}
}
package com.jh.boot.web;
import com.jh.boot.jpa.AbstractIdEntity;
import com.jh.boot.web.list.Page;
import com.jh.boot.web.list.SortTrend;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
public class Converter {
private Converter() {}
public static <S, D> D convert(S source, Function<S, D> mappingFunction) {
if (source == null || mappingFunction == null) {
return null;
}
return mappingFunction.apply(source);
}
public static <S, D> List<D> convertList(List<S> list, Function<S, D> mappingFunction) {
if (list == null) {
return null;
}
return list.stream().map(mappingFunction).collect(Collectors.toList());
}
public static <S, D> Page<D> convertPage(Page<S> page, Function<S, D> mappingFunction) {
if (page == null || mappingFunction == null) {
return null;
}
List<D> content = new ArrayList<>();
if (page.getContent() != null) {
for (S s : page.getContent()) {
content.add(mappingFunction.apply(s));
}
}
return new Page<D>(page.getPage(), page.getPagesCount(), page.getPageSize(), page.getTotalElements(), content, page.getSortBy(), SortTrend.parse(page.getTrend()));
}
/**
* Helper function to safely get id
*
* @param entity
* @return
*/
public static Long getId(AbstractIdEntity entity) {
return entity == null ? null : entity.getId();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment