This is an automated email from the ASF dual-hosted git repository.

doebele pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/empire-db.git


The following commit(s) were added to refs/heads/master by this push:
     new 465a0b4d EMPIREDB-395 allow non-integer min/max values
465a0b4d is described below

commit 465a0b4d23b9bf06ad901d7c80c37f4415342c44
Author: Rainer Döbele <[email protected]>
AuthorDate: Mon Sep 12 13:09:44 2022 +0200

    EMPIREDB-395 allow non-integer min/max values
---
 .../org/apache/empire/commons/ObjectUtils.java     | 50 ++++++++++++++++++++++
 .../main/java/org/apache/empire/db/DBDatabase.java | 29 ++++---------
 2 files changed, 59 insertions(+), 20 deletions(-)

diff --git a/empire-db/src/main/java/org/apache/empire/commons/ObjectUtils.java 
b/empire-db/src/main/java/org/apache/empire/commons/ObjectUtils.java
index 93df3679..ab5c4a7d 100644
--- a/empire-db/src/main/java/org/apache/empire/commons/ObjectUtils.java
+++ b/empire-db/src/main/java/org/apache/empire/commons/ObjectUtils.java
@@ -291,6 +291,56 @@ public final class ObjectUtils
         return expr.equals(other);
     }
     
+    /**
+     * Compares two objects for equality 
+     * 
+     * @param o1 the first object
+     * @param o2 the second object
+     * 
+     * @return true if both objects are equal or false otherwise
+     */
+    @SuppressWarnings("unchecked")
+    public static int compare(Object o1, Object o2)
+    {
+        // simple case
+        if (o1==o2)
+            return 0;
+        // Check for Empty Values
+        if (isEmpty(o1))
+            return isEmpty(o2) ? 0 : -1;
+        if (isEmpty(o2))
+            return isEmpty(o1) ? 0 :  1;
+        // Check classes
+        if (o1.getClass().equals(o2.getClass()))
+        {   // Check if object implements comparable
+            if (o1 instanceof Comparable)
+                return ((Comparable<Object>)o1).compareTo(o2);
+            if (o2 instanceof Comparable)
+                return ((Comparable<Object>)o2).compareTo(o1);
+        }    
+        // Use equal check first
+        if (o1.equals(o2) || o2.equals(o1))
+            return 0;
+        // Compare Numbers
+        if (o1 instanceof Number && o2 instanceof Number)
+        {   // boolean test = obj1.equals(obj2);
+            double d1 = ((Number)o1).doubleValue();
+            double d2 = ((Number)o2).doubleValue();
+            return ((d1<d2) ? -1 : ((d1>d2) ? 1 : 0));
+        }
+        // Compare Date with LocalDate / LocalDateTime
+        if (o1 instanceof Temporal && o2 instanceof Date)
+        {   // swap
+            Object tmp = o2; o2 = o1; o1 = tmp; 
+        }
+        if (o1 instanceof Date && o2 instanceof LocalDate)
+            return compare(o1, DateUtils.toDate((LocalDate)o2));
+        if (o1 instanceof Date && o2 instanceof LocalDateTime)
+            return compare(o1, DateUtils.toDate((LocalDateTime)o2));
+        // Compare Strings
+        return o1.toString().compareTo(o2.toString());
+    }
+    
     /**
      * Checks whether a preferred value is valid and returns an alternative 
value if not.
      * @param <T> the type of the values
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java 
b/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java
index adcbf6dd..be2810df 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java
@@ -1126,30 +1126,19 @@ public abstract class DBDatabase extends DBObject
         // Check Range
         Object min = column.getAttribute(Column.COLATTR_MINVALUE);
         Object max = column.getAttribute(Column.COLATTR_MAXVALUE);
-        if (min!=null && max!=null)
-        {   // Check Range
-            long minVal = ObjectUtils.getLong(min);
-            long maxVal = ObjectUtils.getLong(max);
-            if (n.longValue()<minVal || n.longValue()>maxVal)
-            {   // Out of Range
-                throw new FieldValueOutOfRangeException(column, minVal, 
maxVal);
-            }
+        boolean belowMin = (min instanceof Number) ? ObjectUtils.compare(n, 
min)<0 : false;
+        boolean aboveMax = (max instanceof Number) ? ObjectUtils.compare(n, 
max)>0 : false;
+        if (belowMin && aboveMax)
+        {   // Out of Range
+            throw new FieldValueOutOfRangeException(column, (Number)min, 
(Number)max);
         }
-        else if (min!=null)
+        else if (belowMin)
         {   // Check Min Value
-            long minVal = ObjectUtils.getLong(min);
-            if (n.longValue()<minVal)
-            {   // Out of Range
-                throw new FieldValueOutOfRangeException(column, minVal, false);
-            }
+            throw new FieldValueOutOfRangeException(column, (Number)min, 
false);
         }
-        else if (max!=null)
+        else if (aboveMax)
         {   // Check Max Value
-            long maxVal = ObjectUtils.getLong(max);
-            if (n.longValue()>maxVal)
-            {   // Out of Range
-                throw new FieldValueOutOfRangeException(column, maxVal, true);
-            }
+            throw new FieldValueOutOfRangeException(column, (Number)max, true);
         }
         // Check overall
         if (type==DataType.DECIMAL)

Reply via email to