jackrabbit-oak-commits mailing list archives

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

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

Added:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/LongConstraint.java
  (with props)
Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/BooleanConstraint.java
    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/StringConstraint.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/BooleanConstraint.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/BooleanConstraint.java?rev=1381975&r1=1381974&r2=1381975&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/BooleanConstraint.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/BooleanConstraint.java
Fri Sep  7 11:11:22 2012
@@ -37,7 +37,7 @@ public class BooleanConstraint implement
         }
         else {
             requiredValue = null;
-            log.warn('\'' + definition + "' is not a valid value constraint format for BOOLEAN
values");
+            log.warn('\'' + definition + "' is not a valid value constraint format for boolean
values");
         }
     }
 
@@ -47,8 +47,13 @@ public class BooleanConstraint implement
             return value != null && requiredValue != null && value.getBoolean()
== requiredValue;
         }
         catch (RepositoryException e) {
-            log.warn("Error checking boolean constraint", e);
+            log.warn("Error checking boolean constraint " + this, e);
             return false;
         }
     }
+
+    @Override
+    public String toString() {
+        return "'" + requiredValue + '\'';
+    }
 }

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=1381975&r1=1381974&r2=1381975&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:22 2012
@@ -72,7 +72,7 @@ public class Constraints {
     }
 
     private static Predicate<Value> longConstraint(String constraint) {
-        return Predicates.alwaysTrue(); // todo implement longConstraint
+        return new LongConstraint(constraint);
     }
 
     private static Predicate<Value> doubleConstraint(String constraint) {

Added: 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=1381975&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/LongConstraint.java
(added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/LongConstraint.java
Fri Sep  7 11:11:22 2012
@@ -0,0 +1,123 @@
+/*
+ * 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 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> {
+    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);
+        }
+    }
+
+    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);
+        }
+    }
+
+    @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;
+        }
+    }
+
+    @Override
+    public String toString() {
+        return (lowerInclusive ? "[" : "(") +
+               (lowerBound == null ? "" : lowerBound) + ", " +
+               (upperBound == null ? "" : upperBound) +
+               (upperInclusive ? "]" : ")");
+    }
+
+}

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

Propchange: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/LongConstraint.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/StringConstraint.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/StringConstraint.java?rev=1381975&r1=1381974&r2=1381975&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/StringConstraint.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/constraint/StringConstraint.java
Fri Sep  7 11:11:22 2012
@@ -60,7 +60,13 @@ public class StringConstraint implements
             return matcher.matches();
         }
         catch (RepositoryException e) {
+            log.warn("Error checking string constraint " + this, e);
             return false;
         }
     }
+
+    @Override
+    public String toString() {
+        return "'" + pattern + '\'';
+    }
 }



Mime
View raw message