فهرست منبع

修复boolean无法对比问题

zhou-hao 5 سال پیش
والد
کامیت
ab5c11be52

+ 18 - 2
hsweb-core/src/main/java/org/hswebframework/web/bean/CompareUtils.java

@@ -21,6 +21,10 @@ public abstract class CompareUtils {
         if (source.equals(target)) {
         if (source.equals(target)) {
             return true;
             return true;
         }
         }
+
+        if (source instanceof Boolean) {
+            return compare(((Boolean) source), target);
+        }
         if (source instanceof Number) {
         if (source instanceof Number) {
             return compare(((Number) source), target);
             return compare(((Number) source), target);
         }
         }
@@ -178,9 +182,9 @@ public abstract class CompareUtils {
                 DateFormatter dateFormatter = DateFormatter.getFormatter(stringValue);
                 DateFormatter dateFormatter = DateFormatter.getFormatter(stringValue);
                 return (dateFormatter.toString(new Date(number.longValue())).equals(stringValue));
                 return (dateFormatter.toString(new Date(number.longValue())).equals(stringValue));
             }
             }
-            try{
+            try {
                 return new BigDecimal(stringValue).doubleValue() == number.doubleValue();
                 return new BigDecimal(stringValue).doubleValue() == number.doubleValue();
-            }catch (NumberFormatException e){
+            } catch (NumberFormatException e) {
                 return false;
                 return false;
             }
             }
         }
         }
@@ -235,6 +239,18 @@ public abstract class CompareUtils {
         return false;
         return false;
     }
     }
 
 
+    public static boolean compare(Boolean bool, Object target) {
+        if (bool.equals(target)) {
+            return true;
+        }
+
+        if (bool && "true".equals(target)) {
+            return true;
+        }
+
+        return false;
+    }
+
     public static boolean compare(Date date, Object target) {
     public static boolean compare(Date date, Object target) {
         if (date == target) {
         if (date == target) {
             return true;
             return true;

+ 2 - 0
hsweb-core/src/test/java/org/hswebframework/web/bean/DiffTest.java

@@ -15,11 +15,13 @@ public class DiffTest {
         Map<String, Object> before = new HashMap<>();
         Map<String, Object> before = new HashMap<>();
         before.put("name", "name");
         before.put("name", "name");
         before.put("age",21);
         before.put("age",21);
+        before.put("bool",true);
         before.put("birthday", DateFormatter.fromString("19910101"));
         before.put("birthday", DateFormatter.fromString("19910101"));
 
 
         Map<String, Object> after = new HashMap<>();
         Map<String, Object> after = new HashMap<>();
         after.put("name", "name");
         after.put("name", "name");
         after.put("age", "21");
         after.put("age", "21");
+        after.put("bool", "true");
         after.put("birthday", "1991-01-01");
         after.put("birthday", "1991-01-01");