jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1381976 - in /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint: Constraints.java DoubleConstraint.java LongConstraint.java NumericConstraint.java
Date Fri, 07 Sep 2012 11:11:42 GMT
Author: mduerig
Date: Fri Sep  7 11:11:42 2012
New Revision: 1381976

URL: http://svn.apache.org/viewvc?rev=1381976&view=rev
Log:
OAK-66: JCR Node Type Management
value constraints for Double

Added:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/DoubleConstraint.java
  (with props)
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/NumericConstraint.java
      - copied, changed from r1381975, jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/LongConstraint.java
Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/Constraints.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/LongConstraint.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/Constraints.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/Constraints.java?rev=1381976&r1=1381975&r2=1381976&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/Constraints.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/Constraints.java
Fri Sep  7 11:11:42 2012
@@ -76,7 +76,7 @@ public class Constraints {
     }
 
     private static Predicate<Value> doubleConstraint(String constraint) {
-        return Predicates.alwaysTrue(); // todo implement doubleConstraint
+        return new DoubleConstraint(constraint);
     }
 
     private static Predicate<Value> dateConstraint(String constraint) {

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/DoubleConstraint.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/DoubleConstraint.java?rev=1381976&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/DoubleConstraint.java
(added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/DoubleConstraint.java
Fri Sep  7 11:11:42 2012
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.oak.plugins.type.constraint;
+
+import javax.jcr.RepositoryException;
+import javax.jcr.Value;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class DoubleConstraint extends NumericConstraint<Double> {
+    private static final Logger log = LoggerFactory.getLogger(DoubleConstraint.class);
+
+    public DoubleConstraint(String constraint) {
+        super(constraint);
+    }
+
+    @Override
+    protected void setBounds(String lowerBound, String upperBound) {
+        try {
+            this.lowerBound = lowerBound == null || lowerBound.isEmpty()
+                ? null
+                : Double.parseDouble(lowerBound);
+
+            this.upperBound = upperBound == null || upperBound.isEmpty()
+                ? null
+                : Double.parseDouble(upperBound);
+        }
+        catch (NumberFormatException e) {
+            this.lowerBound = 1.0;
+            this.upperBound = 0.0;
+            log.warn("Invalid bound for numeric constraint" + this, e);
+        }
+    }
+
+    @Override
+    protected Double getValue(Value value) throws RepositoryException {
+        return value.getDouble();
+    }
+
+    @Override
+    protected boolean less(Double val, Double bound) {
+        return val < bound;
+    }
+}

Propchange: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/DoubleConstraint.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/DoubleConstraint.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/LongConstraint.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/LongConstraint.java?rev=1381976&r1=1381975&r2=1381976&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/LongConstraint.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/LongConstraint.java
Fri Sep  7 11:11:42 2012
@@ -16,48 +16,21 @@
  */
 package org.apache.jackrabbit.oak.plugins.type.constraint;
 
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-import javax.annotation.Nullable;
 import javax.jcr.RepositoryException;
 import javax.jcr.Value;
 
-import com.google.common.base.Predicate;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class LongConstraint implements Predicate<Value> {
+public class LongConstraint extends NumericConstraint<Long> {
     private static final Logger log = LoggerFactory.getLogger(LongConstraint.class);
 
-    private boolean lowerInclusive;
-    private Long lowerBound;
-    private Long upperBound;
-    private boolean upperInclusive;
-
     public LongConstraint(String definition) {
-        // format: '(<min>, <max>)',  '[<min>, <max>]', '(, <max>)'
etc.
-        Pattern pattern = Pattern.compile("([\\(\\[])[^,]*,[^\\)\\]]*([\\)\\]])");
-        Matcher matcher = pattern.matcher(definition);
-        if (matcher.matches()) {
-            // group 1 is lower inclusive/exclusive
-            String match = matcher.group(1);
-            lowerInclusive = "[".equals(match);
-
-            // group 2 is lower, group 3 is upper  bound
-            setBounds(matcher.group(2), matcher.group(3));
-
-            // group 4 is lower inclusive/exclusive
-            match = matcher.group(4);
-            upperInclusive = "]".equals(match);
-        }
-        else {
-            String msg = '\'' + definition + "' is not a valid value constraint format for
numeric values";
-            log.warn(msg);
-        }
+        super(definition);
     }
 
-    private void setBounds(String lowerBound, String upperBound) {
+    @Override
+    protected void setBounds(String lowerBound, String upperBound) {
         try {
             this.lowerBound = lowerBound == null || lowerBound.isEmpty()
                 ? null
@@ -75,49 +48,12 @@ public class LongConstraint implements P
     }
 
     @Override
-    public boolean apply(@Nullable Value value) {
-        if (value == null) {
-            return false;
-        }
-
-        try {
-            long val = value.getLong();
-            if (lowerBound != null) {
-                if (lowerInclusive) {
-                    if (val < (lowerBound)) {
-                        return false;
-                    }
-                } else {
-                    if (val <= (lowerBound)) {
-                        return false;
-                    }
-                }
-            }
-            if (upperBound != null) {
-                if (upperInclusive) {
-                    if (val > (upperBound)) {
-                        return false;
-                    }
-                } else {
-                    if (val >= (upperBound)) {
-                        return false;
-                    }
-                }
-            }
-            return true;
-        }
-        catch (RepositoryException e) {
-            log.warn("Error checking numeric constraint " + this, e);
-            return false;
-        }
+    protected Long getValue(Value value) throws RepositoryException {
+        return value.getLong();
     }
 
     @Override
-    public String toString() {
-        return (lowerInclusive ? "[" : "(") +
-               (lowerBound == null ? "" : lowerBound) + ", " +
-               (upperBound == null ? "" : upperBound) +
-               (upperInclusive ? "]" : ")");
+    protected boolean less(Long val, Long bound) {
+        return val < bound;
     }
-
 }

Copied: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/NumericConstraint.java
(from r1381975, jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/LongConstraint.java)
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/NumericConstraint.java?p2=jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/NumericConstraint.java&p1=jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/LongConstraint.java&r1=1381975&r2=1381976&rev=1381976&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/LongConstraint.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/NumericConstraint.java
Fri Sep  7 11:11:42 2012
@@ -27,15 +27,15 @@ import com.google.common.base.Predicate;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class LongConstraint implements Predicate<Value> {
-    private static final Logger log = LoggerFactory.getLogger(LongConstraint.class);
+public abstract class NumericConstraint<T> implements Predicate<Value> {
+    private static final Logger log = LoggerFactory.getLogger(NumericConstraint.class);
 
     private boolean lowerInclusive;
-    private Long lowerBound;
-    private Long upperBound;
+    protected T lowerBound;
+    protected T upperBound;
     private boolean upperInclusive;
 
-    public LongConstraint(String definition) {
+    protected NumericConstraint(String definition) {
         // format: '(<min>, <max>)',  '[<min>, <max>]', '(, <max>)'
etc.
         Pattern pattern = Pattern.compile("([\\(\\[])[^,]*,[^\\)\\]]*([\\)\\]])");
         Matcher matcher = pattern.matcher(definition);
@@ -57,22 +57,7 @@ public class LongConstraint implements P
         }
     }
 
-    private void setBounds(String lowerBound, String upperBound) {
-        try {
-            this.lowerBound = lowerBound == null || lowerBound.isEmpty()
-                ? null
-                : Long.parseLong(lowerBound);
-
-            this.upperBound = upperBound == null || upperBound.isEmpty()
-                ? null
-                : Long.parseLong(upperBound);
-        }
-        catch (NumberFormatException e) {
-            this.lowerBound = 1L;
-            this.upperBound = 0L;
-            log.warn("Invalid bound for numeric constraint" + this, e);
-        }
-    }
+    protected abstract void setBounds(String lowerBound, String upperBound);
 
     @Override
     public boolean apply(@Nullable Value value) {
@@ -81,25 +66,25 @@ public class LongConstraint implements P
         }
 
         try {
-            long val = value.getLong();
+            T t = getValue(value);
             if (lowerBound != null) {
                 if (lowerInclusive) {
-                    if (val < (lowerBound)) {
+                    if (less(t, lowerBound)) {
                         return false;
                     }
                 } else {
-                    if (val <= (lowerBound)) {
+                    if (lessOrEqual(t, lowerBound)) {
                         return false;
                     }
                 }
             }
             if (upperBound != null) {
                 if (upperInclusive) {
-                    if (val > (upperBound)) {
+                    if (greater(t, upperBound)) {
                         return false;
                     }
                 } else {
-                    if (val >= (upperBound)) {
+                    if (greaterOrEqual(t, upperBound)) {
                         return false;
                     }
                 }
@@ -112,12 +97,29 @@ public class LongConstraint implements P
         }
     }
 
+    protected abstract T getValue(Value value) throws RepositoryException;
+    protected abstract boolean less(T val, T bound);
+
+    protected boolean greater(T val, T bound) {
+        return less(bound, val);
+    }
+
+    protected boolean equals(T val, T bound) {
+        return val.equals(bound);
+    }
+
+    protected boolean greaterOrEqual(T val, T bound) {
+        return greater(val, bound) || equals(val, bound);
+    }
+
+    protected boolean lessOrEqual(T val, T bound) {
+        return less(val, bound) || equals(val, bound);
+    }
+
     @Override
     public String toString() {
         return (lowerInclusive ? "[" : "(") +
-               (lowerBound == null ? "" : lowerBound) + ", " +
-               (upperBound == null ? "" : upperBound) +
-               (upperInclusive ? "]" : ")");
-    }
-
-}
+                (lowerBound == null ? "" : lowerBound) + ", " +
+                (upperBound == null ? "" : upperBound) +
+                (upperInclusive ? "]" : ")");
+    }}



Mime
View raw message