用泛型方法Java从实体中提取属性值,以及在泛型方法中的使用
public <T> T getFieldValue(Object target, String fieldName, Class<T> typeName) { try { Object fieldValue = FieldUtils.readField(target, fieldName, true); return (T)fieldValue; } catch (IllegalAccessException e) { log.error("出错:实体类{}没有{}类型的{}属性字段!",target.getClass(),typeName.getSimpleName(),fieldName); throw new RuntimeException(e); } }
用法1:
public Long getLongValue(Object target, String fieldName) { return getFieldValue(target,fieldName,Long.class); }
以此类推,你也可以写出
public LocalDateTime getLocalDateTimeValue(Object target, String fieldName) { return getFieldValue(target,fieldName,LocalDateTime.class); } public String getStringValue(Object target, String fieldName) { return getFieldValue(target,fieldName,String.class); }
笔者的一个用法是在泛型方法中提取实体的属性值,做进一步计算
<R,T> 你的返回类型 processData(String label, String snapshotKey, Class<T> targetClass, Predicate<? super T> filter, final Function<? super T, ? extends R> mapper) { if(filter == null) { //如果没有指定过滤表达式,给一个默认值 filter = (T entity)->{ LocalDateTime createTime = cacheService.getFieldValue(entity, "createTime", LocalDateTime.class); return createTime.getMinute() % 10 == 0 &&createTime.getSecond() ==0; }; } Map<String,Object> resultMap = new HashMap<>(); Optional<SnmpNode> node1 = nodeMapping.values().stream().findFirst(); List<T> list = null; if(node1.isPresent()) { String ipAddr1 = node1.get().getAddress(); list = cacheService.getCachedList(snapshotKey, ipAddr1, targetClass); //服务器ip resultMap.put("legend", nodeMapping.values().stream().map(SnmpNode::getAddress).collect(Collectors.toList())); //批量格式时间MM-dd HH:mm:ss并封送到List List<String> xAxis = list.stream() .map(entity->cacheService.getFieldValue(entity,"createTime", LocalDateTime.class)) .filter( localDateTime -> localDateTime.getMinute()%10==0 && localDateTime.getSecond() == 0 ).map(createTime -> createTime.format(DateTimeFormatter.ofPattern("MM-dd HH:mm"))).collect(Collectors.toList()); //筛选后的样本大小 int filteredSize = xAxis.size(); //由于图表不能显示太多的数据,太多的就会被隐藏,因此只显示最近的20条数据 xAxis = xAxis.stream().skip(filteredSize>=0?filteredSize-20:filteredSize).collect(Collectors.toList()); resultMap.put("xAxis",xAxis); List<EChartSeries> series = new LinkedList<>(); for(Map.Entry<Long,SnmpNode> entry: nodeMapping.entrySet()) { SnmpNode node = entry.getValue(); String ipAddr = node.getAddress(); List<T> traffics = cacheService.getCachedList(snapshotKey, ipAddr, targetClass); List<R> data = traffics.stream() .filter(filter) .skip(filteredSize>=0?filteredSize-20:filteredSize) .map(mapper).collect(Collectors.toList()); EChartSeries chartSeries = new EChartSeries.Builder() .withName(ipAddr) .withStack(label) .withType("line") .withData((LinkedList<String>) new LinkedList<R>(data)) .build(); if(!CollectionUtils.isEmpty(data)) { series.add(chartSeries); } } resultMap.put("series",series); } return 你的返回类型; }
import lombok.Data; import lombok.NoArgsConstructor; import java.util.LinkedList; /*** * // name:'邮件营销', * // type:'line', * // stack: '内存使用率', * // data:[120, 132, 101, 134, 90, 230, 210] */ @Data @NoArgsConstructor public class EChartSeries { private String name; private String type; private String stack; private LinkedList<String> data; private EChartSeries(Builder builder) { setName(builder.name); setType(builder.type); setStack(builder.stack); setData(builder.data); } public static final class Builder { private String name; private String type; private String stack; private LinkedList<String> data; public Builder() { } public Builder(EChartSeries copy) { this.name = copy.getName(); this.type = copy.getType(); this.stack = copy.getStack(); this.data = copy.getData(); } public Builder withName(String name) { this.name = name; return this; } public Builder withType(String type) { this.type = type; return this; } public Builder withStack(String stack) { this.stack = stack; return this; } public Builder withData(LinkedList<String> data) { this.data = data; return this; } public EChartSeries build() { return new EChartSeries(this); } } }