Commit b2a3d6e0 by Jan Hrabal

fix

parent cfc11eb8
package com.jh.boot.web; package com.jh.boot.web;
import com.jh.boot.jpa.AbstractIdEntity; import com.jh.boot.jpa.AbstractIdEntity;
import com.jh.boot.web.list.Page; import com.jh.boot.web.list.Page;
import com.jh.boot.web.list.SortTrend; import com.jh.boot.web.list.SortTrend;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class Converter { public class Converter {
private Converter() {} private Converter() {}
public static <S, D> D convert(S source, Function<S, D> mappingFunction) { public static <S, D> D convert(S source, Function<S, D> mappingFunction) {
if (source == null || mappingFunction == null) { if (source == null || mappingFunction == null) {
return null; return null;
} }
return mappingFunction.apply(source); return mappingFunction.apply(source);
} }
public static <S, D> List<D> convertList(List<S> list, Function<S, D> mappingFunction) { public static <S, D> List<D> convertList(List<S> list, Function<S, D> mappingFunction) {
if (list == null) { if (list == null) {
return null; return null;
} }
return list.stream().map(mappingFunction).collect(Collectors.toList()); return list.stream().map(mappingFunction).collect(Collectors.toList());
} }
public static <S, D> Page<D> convertPage(Page<S> page, Function<S, D> mappingFunction) { public static <S, D> Page<D> convertPage(Page<S> page, Function<S, D> mappingFunction) {
if (page == null || mappingFunction == null) { if (page == null || mappingFunction == null) {
return null; return null;
} }
List<D> content = new ArrayList<>(); List<D> content = new ArrayList<>();
if (page.getContent() != null) { if (page.getContent() != null) {
for (S s : page.getContent()) { for (S s : page.getContent()) {
content.add(mappingFunction.apply(s)); content.add(mappingFunction.apply(s));
} }
} }
return new Page<D>(page.getPage(), page.getPagesCount(), page.getTotalElements(), content, page.getSortBy(), SortTrend.parse(page.getTrend())); 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 * Helper function to safely get id
* *
* @param entity * @param entity
* @return * @return
*/ */
public static Long getId(AbstractIdEntity entity) { public static Long getId(AbstractIdEntity entity) {
return entity == null ? null : entity.getId(); 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