ソースを参照

add test code

lixing 3 年 前
コミット
353cb6c853

+ 22 - 4
src/main/java/com/persagy/apm/energy/report/common/utils/DataUtils.java

@@ -1,5 +1,6 @@
 package com.persagy.apm.energy.report.common.utils;
 
+import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.nfunk.jep.JEP;
 import org.springframework.util.CollectionUtils;
@@ -11,6 +12,7 @@ import java.text.Collator;
 import java.util.*;
 import java.util.stream.Collectors;
 
+@Slf4j
 public class DataUtils {
 
     private final static Collator CHINA_COLLATOR = Collator.getInstance(Locale.CHINA);
@@ -46,10 +48,10 @@ public class DataUtils {
     /**
      * 按规则进行计算
      *
-     * @param standardValue  规则
+     * @param standardValue  标准
      * @param qualifyFormula 规则
      * @param data           要进行规则校验的数据
-     * @return
+     * @return 计算结果(如果规则结果是bool值,1.0 -> true, 0.0 -> false)
      */
     public static Double getQualifyResult(Double standardValue, String qualifyFormula, Double data) {
         JEP jep = new JEP();
@@ -63,6 +65,11 @@ public class DataUtils {
         return jep.getValue();
     }
 
+    public static void main(String[] args) {
+        Double qualifyResult = getQualifyResult(4d, "source <= standard_value", 2d);
+        System.out.println(qualifyResult);
+    }
+
 
     /**
      * 将一组对象中类型为double的属性求和
@@ -129,10 +136,12 @@ public class DataUtils {
         // 结果保留的位数
         int scale = 4;
         if (d1 == null || d2 == null) {
-            throw new IllegalArgumentException("两个double做除法,其中一个为空或两个都为空");
+            log.error("两个double做除法,其中一个为空或两个都为空, d1: {}, d2: {}", d1, d2);
+            return null;
         }
         if (d2.equals(0d)) {
-            throw new IllegalArgumentException("double做除法,被除数不能为0");
+            log.error("double做除法,被除数不能为0");
+            return null;
         }
         BigDecimal bigDecimal1 = new BigDecimal(d1);
         BigDecimal bigDecimal2 = new BigDecimal(d2);
@@ -158,6 +167,15 @@ public class DataUtils {
         return bigDecimal1.subtract(bigDecimal2).doubleValue();
     }
 
+    /**
+     * 根据自定义规则对列表排序
+     * 如果存在多个要排序的字段,排序方式同数据库排序方式
+     *
+     * @param list          要排序的列表
+     * @param comparatorMap 要排序的属性 -> 正序/倒序(-1:从高到低,1: 从低到高)
+     * @author lixing
+     * @version V1.0 2021/7/29 9:52 上午
+     */
     public static void sort(List<?> list, final Map<String, Integer> comparatorMap) {
         Comparator<Object> comparator = new Comparator<Object>() {
 

+ 1 - 1
src/main/java/com/persagy/apm/energy/report/environment/service/impl/IEnvironmentWebServiceImpl.java

@@ -42,7 +42,7 @@ public class IEnvironmentWebServiceImpl implements IEnvironmentWebService {
             date_rateMap.put(startDate, getEnvAvgRate(queryPjPlatformParamDTO.getProjectId(), startDate));
             startDate = DateUtils.getMonthOff(startDate, 1);
         }
-        List<Double> monthlySummaries = date_rateMap.values().stream().collect(Collectors.toList());
+        List<Double> monthlySummaries = new ArrayList<>(date_rateMap.values());
         platform.setMonthlySummaries(monthlySummaries);
     }
 

+ 19 - 0
src/test/java/Test.java

@@ -0,0 +1,19 @@
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 类说明
+ *
+ * @author lixing
+ * @version V1.0 2021/7/28 6:51 下午
+ **/
+public class Test {
+    public static void main(String[] args) {
+        Map<String, List<String>> result = new HashMap<>();
+        List<String> tmp = result.computeIfAbsent("a", k -> new ArrayList<>());
+        tmp.add("aa");
+        System.out.println(result);
+    }
+}