筆者平時時間有限,直接貼代碼,關於幾個接口的差別,可以查看這兩篇文章
感受lambda之美,推薦收藏,需要時查閱
https://juejin.im/post/5ce66801e51d455d850d3a4a
Java8 函數式編程讀書總結
https://juejin.im/entry/5912bbe0a0bb9f0058b485d6
<T> List<T> readList(Table table, OID[] oids, String[] fieldNames, Class clazz, List<T> defaultValue, Predicate<? super T> filter, Consumer<? super List<T>> consumer, UnaryOperator<? super T> operator);
@Override public <T> List<T> readList(Table table, OID[] oids, String[] fieldNames, Class clazz, List<T> defaultValue, Predicate<? super T> filter, Consumer<? super List<T>> consumer, UnaryOperator<? super T> operator) { if (table == null || table.isEmpty()) { return defaultValue; } Class[] declaredFieldType = getDeclaredFieldType(fieldNames, clazz); if(ArrayUtils.isEmpty(fieldNames)||ArrayUtils.isEmpty(declaredFieldType)) { log.error("字段名長度和字段數據類型長度不能為null"); throw new IllegalArgumentException("字段名長度和字段數據類型長度不能為null"); } if(!ArrayUtils.isSameLength(fieldNames, declaredFieldType)) { throw new IllegalArgumentException("字段名個數與字段數據類型數組長度不一致"); } Map<OID, Table.Row> rowMapping = table.getMap(); if(MapUtils.isEmpty(rowMapping)) { return defaultValue; } log.debug("待查詢oid名稱:{}",String.join(",", fieldNames)); String [] oidInDottedString = Arrays.asList(oids).stream().map(OID::toDottedString).toArray(size->new String[size]); List<T> list = new LinkedList<T>(); for(Table.Row row : rowMapping.values()) { Set<OID> keys = row.getMap().keySet(); try { T instance = (T)clazz.newInstance(); for (OID key : keys) { String dotString = key.toDottedString(); for (int i = 0; i < oidInDottedString.length; i++) { String oidString = oidInDottedString[i]; if (dotString.contains(oidString)) { String fieldName = fieldNames[i]; Variable value = row.getVariable(key); Class declaredType = declaredFieldType[i]; setProperty(instance,declaredType,fieldName,value); } } } list.add(instance); }catch (IllegalAccessException|InstantiationException e) { log.error("提取屬性失敗:{}",e); } } if(operator != null && CollectionUtils.isNotEmpty(list)) { list.stream().forEach(instance->{operator.apply(instance);}); } if(filter != null && CollectionUtils.isNotEmpty(list)) { list = list.stream().filter(filter).collect(Collectors.toList()); } if(consumer != null && CollectionUtils.isNotEmpty(list)) { consumer.accept(list); } return list; }
<T> T readObject(Table table, String[] fieldNames, Class clazz, T defaultValue,final Predicate<? super T> filter,final Consumer<? super T> consumer,final UnaryOperator<? super T> operator); @Override public <T> T readObject(Table table, String[] fieldNames, Class clazz, T defaultValue, Predicate<? super T> filter, Consumer<? super T> consumer, UnaryOperator<? super T> operator) { if (table == null || table.isEmpty()) { return defaultValue; } Optional<T> result = readList(table, fieldNames, clazz, Arrays.asList(defaultValue)/*, null, null, null*/).stream().findFirst(); return handleSingleObject(result,filter,consumer,operator);
用法:(方法簽名會有出入,沒有的簽名方法,通過重載實現了,在此不貼出來代碼,看官可自行實現,有疑問可跟帖留意交流)
//讀取ip地址、子網掩碼 Table ipAddrTable = snmpHelper.getTable(MIB.IpAddrTable); List<SnmpIfCard> ifCards = agentService.readList(ipAddrTable, agentService.translateOID(SnmpObjectNotation.ipAddrEntry), SnmpObjectNotation.ipFieldNames, SnmpIfCard.class, null, (SnmpIfCard ifCard) -> { if(ifCard!=null) { //過濾掉回環地址 String ifIpAddr = ifCard.getIfIpAddr(); return !"127.0.0.1".equals(ifIpAddr); } return false; },(SnmpIfCard ifCard)->{ if(ifCard != null) { //設置nodeId ifCard.setNodeId(nodeId); return ifCard; } return null; }); //網口流量 Table ifTable = snmpHelper.getTable(MIB.IfTable); agentService.readList(ifTable,SnmpObjectNotation.trafficFieldNames,SnmpIfCardTraffic.class, null, (SnmpIfCardTraffic traffic)->{ if(traffic != null) { Integer ifIndex = traffic.getIfIndex(); //如果網口索引里沒有這個IfIndex,則不采集(通常是過濾IfIndex =1的本地回環網卡 lo) List<Integer> allIfIndex = ifCards.stream().map(SnmpIfCard::getIfIndex).collect(Collectors.toList()); return allIfIndex.contains(ifIndex); } return false; }, captured::setTraffics, (SnmpIfCardTraffic traffic)->{ if(traffic != null) { //通過網口索引匹配,將對應網口索引的子網掩碼和ip地址拷貝到網口流量信息實體上面 Optional<SnmpIfCard> matched = matchIfCard(ifCards, traffic); if (matched.isPresent()) { traffic.setNodeId(nodeId); traffic.setIfIpNetMask(matched.get().getIfIpNetMask()); traffic.setIfIpAddr(matched.get().getIfIpAddr()); } return traffic; } return null; }); //文件系統使用情況 Table hrStorageEntry = snmpHelper.getTable(MIB.hrStorageEntry); agentService.readList(hrStorageEntry, SnmpObjectNotation.hrStorageEntryFields, SnmpFileSystemUsage.class,null, captured::setFsUsage, (SnmpFileSystemUsage usage)->{ if(usage != null) { usage.setNodeId(nodeId); return usage; } return null; });
//內存使用率 Table memory = snmpHelper.getTable(MIB.memory); agentService.readObject(memory, SnmpObjectNotation.memoryFieldNames, SnmpMemoryUsage.class, (SnmpMemoryUsage) null, captured::setMemoryUsage, (usage) -> { if(usage !=null) { usage.setNodeId(nodeId); return usage; } return null; }); //系統運行狀態,從這里獲取cpu使用率信息 Table sysStats = snmpHelper.getTable(MIB.systemStats); agentService.readObject(sysStats, SnmpObjectNotation.systemStatFields, SnmpCPUUsage.class, (SnmpCPUUsage)null, captured::setCpuUsage, (usage) -> { if(usage !=null) { usage.setNodeId(nodeId); return usage; } return null; });
Function的應用
<R,T> List<R> readTable(Table table, String[] fieldNames, Class clazz, Class[] declaredFieldType, List<T> defaultValue, Predicate<? super T> filter, Consumer<? super List<T>> consumer, Function<? super T, ? extends R> mapper); @Override public <R,T> List<R> readTable(Table table, String[] fieldNames, Class clazz, Class[] declaredFieldType, List<T> defaultValue, Predicate<? super T> filter, Consumer<? super List<T>> consumer, Function<? super T, ? extends R> mapper) { List<T> list = readList(table, fieldNames, clazz, declaredFieldType, defaultValue, filter, consumer); return list.stream().map(item -> { R target = mapper.apply(item); return target; }).collect(Collectors.toList()); } @Override public <T> List<T> readList(Table table, String[] fieldNames, Class clazz, Class[] declaredFieldType, List<T> defaultValue, Predicate<? super T> filter, Consumer<? super List<T>> consumer) { return readList(table,fieldNames,clazz,declaredFieldType,defaultValue,filter,consumer,null); }