You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by js...@apache.org on 2019/10/31 15:41:17 UTC

[sis] branch geoapi-4.0 updated: Filter : add between filter implementation

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

jsorel pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git


The following commit(s) were added to refs/heads/geoapi-4.0 by this push:
     new c7a6e01  Filter : add between filter implementation
c7a6e01 is described below

commit c7a6e01e62922d7a8ed0cb53d8ec3288371483d5
Author: jsorel <jo...@geomatys.com>
AuthorDate: Thu Oct 31 16:41:02 2019 +0100

    Filter : add between filter implementation
---
 .../org/apache/sis/filter/ComparisonFunction.java  | 53 +++++++++++++
 .../apache/sis/filter/DefaultFilterFactory.java    |  2 +-
 .../org/apache/sis/filter/BetweenFunctionTest.java | 90 ++++++++++++++++++++++
 .../apache/sis/test/suite/FeatureTestSuite.java    |  1 +
 4 files changed, 145 insertions(+), 1 deletion(-)

diff --git a/core/sis-feature/src/main/java/org/apache/sis/filter/ComparisonFunction.java b/core/sis-feature/src/main/java/org/apache/sis/filter/ComparisonFunction.java
index 3292bb1..21edf62 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/filter/ComparisonFunction.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/filter/ComparisonFunction.java
@@ -32,6 +32,8 @@ import java.time.chrono.ChronoLocalDateTime;
 import java.time.chrono.ChronoZonedDateTime;
 import java.time.temporal.ChronoField;
 import java.time.temporal.Temporal;
+import java.util.Arrays;
+import java.util.Collection;
 import org.apache.sis.math.Fraction;
 import org.apache.sis.util.ArgumentChecks;
 
@@ -715,4 +717,55 @@ abstract class ComparisonFunction extends BinaryFunction implements BinaryCompar
             return visitor.visit(this, extraData);
         }
     }
+
+    /**
+     * The {@value #NAME} filter.
+     */
+    static final class Between extends Node implements org.opengis.filter.PropertyIsBetween {
+        /** For cross-version compatibility during (de)serialization. */
+        private static final long serialVersionUID = -2434954008425799595L;
+
+        private final GreaterThanOrEqualTo lower;
+        private final LessThanOrEqualTo upper;
+
+        Between(final Expression expression, final Expression lower, final Expression upper) {
+            this.lower = new GreaterThanOrEqualTo(expression, lower, true, MatchAction.ANY);
+            this.upper = new LessThanOrEqualTo(expression, upper, true, MatchAction.ANY);
+        }
+
+
+        /** Identification of this operation. */
+        @Override protected String getName() {return NAME;}
+
+        @Override
+        protected Collection<?> getChildren() {
+            return Arrays.asList(lower.expression1, lower.expression2, upper.expression2);
+        }
+
+        @Override
+        public Expression getExpression() {
+            return lower.expression1;
+        }
+
+        @Override
+        public Expression getLowerBoundary() {
+            return lower.expression2;
+        }
+
+        @Override
+        public Expression getUpperBoundary() {
+            return upper.expression2;
+        }
+
+        @Override
+        public boolean evaluate(Object object) {
+            return lower.evaluate(object) && upper.evaluate(object);
+        }
+
+        @Override
+        public Object accept(FilterVisitor visitor, Object extraData) {
+            return visitor.visit(this, extraData);
+        }
+
+    }
 }
diff --git a/core/sis-feature/src/main/java/org/apache/sis/filter/DefaultFilterFactory.java b/core/sis-feature/src/main/java/org/apache/sis/filter/DefaultFilterFactory.java
index fad06e9..8905573 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/filter/DefaultFilterFactory.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/filter/DefaultFilterFactory.java
@@ -407,7 +407,7 @@ public class DefaultFilterFactory implements FilterFactory2 {
      */
     @Override
     public PropertyIsBetween between(final Expression expression, final Expression lower, final Expression upper) {
-        throw new UnsupportedOperationException("Not supported yet.");
+        return new ComparisonFunction.Between(expression, lower, upper);
     }
 
     /**
diff --git a/core/sis-feature/src/test/java/org/apache/sis/filter/BetweenFunctionTest.java b/core/sis-feature/src/test/java/org/apache/sis/filter/BetweenFunctionTest.java
new file mode 100644
index 0000000..1dc4204
--- /dev/null
+++ b/core/sis-feature/src/test/java/org/apache/sis/filter/BetweenFunctionTest.java
@@ -0,0 +1,90 @@
+/*
+ * 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.sis.filter;
+
+import org.opengis.filter.FilterFactory;
+import org.opengis.filter.expression.Literal;
+import org.apache.sis.test.TestCase;
+import org.junit.Test;
+
+import static org.apache.sis.test.Assert.*;
+import org.opengis.filter.Filter;
+import org.opengis.filter.PropertyIsBetween;
+
+
+/**
+ * Tests {@link ComparisonFunction.Between} implementations.
+ *
+ * @author  Johann Sorel (Geomatys)
+ * @version 2.0
+ * @since   2.0
+ * @module
+ */
+public final strictfp class BetweenFunctionTest extends TestCase {
+    /**
+     * The factory to use for creating the objects to test.
+     */
+    private final FilterFactory factory;
+
+    /**
+     * Expressions used as constant for the tests.
+     */
+    private final Literal c05, c10, c20;
+
+    /**
+     * Expected name of the filter to be evaluated. The {@link #evaluate(Filter)} method
+     * will compare {@link Filter#getName()} against this value.
+     */
+    private String expectedName;
+
+    /**
+     * The filter tested by last call to {@link #evaluate(Filter)}.
+     */
+    private Filter filter;
+
+    /**
+     * Creates a new test case.
+     */
+    public BetweenFunctionTest() {
+        factory = new DefaultFilterFactory();
+        c05 = factory.literal(5);
+        c10 = factory.literal(10);
+        c20 = factory.literal(20);
+    }
+
+    /**
+     * Evaluates the given filter. The {@link #expectedName} field must be set before this method is invoked.
+     */
+    private boolean evaluate(final Filter filter) {
+        this.filter = filter;
+        assertInstanceOf("Expected SIS implementation.", PropertyIsBetween.class, filter);
+        assertEquals("name", expectedName, ((Node) filter).getName());
+        return filter.evaluate(null);
+    }
+
+    /**
+     * Tests "Between" (construction, evaluation, serialization, equality).
+     */
+    @Test
+    public void testBetween() {
+        expectedName = "Between";
+        assertTrue (evaluate(factory.between(c10, c05, c20)));
+        assertFalse(evaluate(factory.between(c20, c05, c10)));
+        assertFalse(evaluate(factory.between(c05, c10, c20)));
+        assertSerializedEquals(filter);
+    }
+}
diff --git a/core/sis-feature/src/test/java/org/apache/sis/test/suite/FeatureTestSuite.java b/core/sis-feature/src/test/java/org/apache/sis/test/suite/FeatureTestSuite.java
index ad0cd8b..9eb9b1d 100644
--- a/core/sis-feature/src/test/java/org/apache/sis/test/suite/FeatureTestSuite.java
+++ b/core/sis-feature/src/test/java/org/apache/sis/test/suite/FeatureTestSuite.java
@@ -56,6 +56,7 @@ import org.junit.runners.Suite;
     org.apache.sis.filter.FilterByIdentifierTest.class,
     org.apache.sis.filter.ArithmeticFunctionTest.class,
     org.apache.sis.filter.ComparisonFunctionTest.class,
+    org.apache.sis.filter.BetweenFunctionTest.class,
     org.apache.sis.filter.TemporalFunctionTest.class,
     org.apache.sis.filter.SQLMMTest.class,
     org.apache.sis.internal.feature.AttributeConventionTest.class,