Ver Fonte

代码优化

Woody há 1 semana atrás
pai
commit
8f35916ff2

+ 44 - 12
framework-common/src/main/java/com/chelvc/framework/common/model/Range.java

@@ -1,13 +1,14 @@
 package com.chelvc.framework.common.model;
 
 import java.io.Serializable;
+import java.util.function.Function;
 
+import com.chelvc.framework.common.util.ObjectUtils;
 import com.chelvc.framework.common.util.StringUtils;
-import lombok.AllArgsConstructor;
 import lombok.Builder;
 import lombok.EqualsAndHashCode;
 import lombok.Getter;
-import lombok.NoArgsConstructor;
+import lombok.NonNull;
 
 /**
  * 数字范围对象
@@ -18,18 +19,24 @@ import lombok.NoArgsConstructor;
 @Getter
 @Builder
 @EqualsAndHashCode
-@NoArgsConstructor
-@AllArgsConstructor
 public final class Range implements Serializable {
     /**
      * 最小值
      */
-    private Integer min;
+    private Double min;
 
     /**
      * 最大值
      */
-    private Integer max;
+    private Double max;
+
+    public Range() {
+    }
+
+    public Range(Number min, Number max) {
+        this.min = ObjectUtils.ifNull(min, Number::doubleValue);
+        this.max = ObjectUtils.ifNull(max, Number::doubleValue);
+    }
 
     /**
      * 将字符串转换成数字范围信息
@@ -43,18 +50,43 @@ public final class Range implements Serializable {
                 return new Range(l == null ? r : l, r == null ? l : r);
             }
             return new Range(l, r);
-        }, Integer::parseInt);
+        }, Double::parseDouble);
     }
 
     /**
-     * 判断数字是否属于当前范围
+     * 判断是否包含指定数字
      *
-     * @param value 数字
+     * @param number 数字
      * @return true/false
      */
-    public boolean matches(Integer value) {
-        return value != null && (this.min != null || this.max != null)
-                && (this.min == null || value >= this.min) && (this.max == null || value <= this.max);
+    public boolean contains(Number number) {
+        if (number != null && (this.min != null || this.max != null)) {
+            double value = number.doubleValue();
+            return (this.min == null || value >= this.min) && (this.max == null || value <= this.max);
+        }
+        return false;
+    }
+
+    /**
+     * 获取最小值
+     *
+     * @param function 返回值转换函数
+     * @param <R>      返回值类型
+     * @return 最小值
+     */
+    public <R> R getMin(@NonNull Function<Double, R> function) {
+        return ObjectUtils.ifNull(this.min, function);
+    }
+
+    /**
+     * 获取最大值
+     *
+     * @param function 返回值转换函数
+     * @param <R>      返回值类型
+     * @return 最大值
+     */
+    public <R> R getMax(@NonNull Function<Double, R> function) {
+        return ObjectUtils.ifNull(this.max, function);
     }
 
     @Override