sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1757015 - in /sis/branches/JDK8/core/sis-feature/src: main/java/org/apache/sis/filter/ test/java/org/apache/sis/filter/
Date Sat, 20 Aug 2016 18:09:35 GMT
Author: desruisseaux
Date: Sat Aug 20 18:09:34 2016
New Revision: 1757015

URL: http://svn.apache.org/viewvc?rev=1757015&view=rev
Log:
Address type-safety issue with parameterized types in filter.
In particular, evaluate(...) should not accept a null Class argument.
This commit contains opportunist code reformatting.

Modified:
    sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/filter/AbstractExpression.java
    sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/filter/DefaultFilterFactory.java
    sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/filter/DefaultLiteral.java
    sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/filter/DefaultPropertyName.java
    sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/filter/DefaultPropertyNameTest.java

Modified: sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/filter/AbstractExpression.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/filter/AbstractExpression.java?rev=1757015&r1=1757014&r2=1757015&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/filter/AbstractExpression.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/filter/AbstractExpression.java
[UTF-8] Sat Aug 20 18:09:34 2016
@@ -17,22 +17,21 @@
 package org.apache.sis.filter;
 
 import java.io.Serializable;
-
+import org.opengis.filter.expression.Expression;
+import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.ObjectConverters;
-
 import org.apache.sis.util.UnconvertibleObjectException;
-import org.opengis.filter.expression.Expression;
+
 
 /**
  * Override evaluate(Object,Class) by using the converters system.
  *
- * @author Johann Sorel (Geomatys)
- * @since   0.7
- * @version 0.7
+ * @author  Johann Sorel (Geomatys)
+ * @since   0.8
+ * @version 0.8
  * @module
  */
 public abstract class AbstractExpression implements Expression,Serializable {
-
     /**
      * Use SIS object converters to convert the default result object
      * to the wished class.
@@ -42,15 +41,12 @@ public abstract class AbstractExpression
      */
     @Override
     public <T> T evaluate(final Object candidate, final Class<T> target) {
+        ArgumentChecks.ensureNonNull("target", target);
         final Object value = evaluate(candidate);
-        if (target == null) {
-            return (T) value;
-        }
-        try{
+        try {
             return ObjectConverters.convert(value, target);
-        }catch (UnconvertibleObjectException ex){
+        } catch (UnconvertibleObjectException ex) {
             return null;
         }
     }
-
 }

Modified: sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/filter/DefaultFilterFactory.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/filter/DefaultFilterFactory.java?rev=1757015&r1=1757014&r2=1757015&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/filter/DefaultFilterFactory.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/filter/DefaultFilterFactory.java
[UTF-8] Sat Aug 20 18:09:34 2016
@@ -95,9 +95,9 @@ import org.opengis.util.GenericName;
 /**
  * Default implementation of GeoAPI filter factory.
  *
- * @author Johann Sorel (Geomatys)
- * @since   0.7
- * @version 0.7
+ * @author  Johann Sorel (Geomatys)
+ * @since   0.8
+ * @version 0.8
  * @module
  */
 public class DefaultFilterFactory implements FilterFactory2 {
@@ -109,7 +109,8 @@ public class DefaultFilterFactory implem
      */
     @Override
     public BBOX bbox(final String propertyName, final double minx,
-            final double miny, final double maxx, final double maxy, final String srs) {
+            final double miny, final double maxx, final double maxy, final String srs)
+    {
         return bbox(property(propertyName), minx, miny, maxx, maxy, srs);
     }
 
@@ -118,7 +119,8 @@ public class DefaultFilterFactory implem
      */
     @Override
     public BBOX bbox(final Expression e, final double minx, final double miny,
-            final double maxx, final double maxy, final String srs) {
+            final double maxx, final double maxy, final String srs)
+    {
         throw new UnsupportedOperationException("Not supported yet.");
     }
 
@@ -126,7 +128,8 @@ public class DefaultFilterFactory implem
      * {@inheritDoc }
      */
     @Override
-    public BBOX bbox(final Expression e, final Envelope bounds) {
+    public BBOX bbox(final Expression e, final Envelope bounds)
+    {
         throw new UnsupportedOperationException("Not supported yet.");
     }
 
@@ -135,7 +138,8 @@ public class DefaultFilterFactory implem
      */
     @Override
     public Beyond beyond(final String propertyName, final Geometry geometry,
-            final double distance, final String units) {
+            final double distance, final String units)
+    {
         final PropertyName name = property(propertyName);
         final Literal geom = literal(geometry);
         return beyond(name, geom, distance, units);
@@ -146,7 +150,8 @@ public class DefaultFilterFactory implem
      */
     @Override
     public Beyond beyond(final Expression left, final Expression right,
-            final double distance, final String units) {
+            final double distance, final String units)
+    {
         throw new UnsupportedOperationException("Not supported yet.");
     }
 
@@ -716,7 +721,7 @@ public class DefaultFilterFactory implem
      */
     @Override
     public Literal literal(final Object value) {
-        return new DefaultLiteral(value);
+        return new DefaultLiteral<>(value);
     }
 
     /**
@@ -724,7 +729,7 @@ public class DefaultFilterFactory implem
      */
     @Override
     public Literal literal(final byte value) {
-        return new DefaultLiteral(value);
+        return new DefaultLiteral<>(value);
     }
 
     /**
@@ -732,7 +737,7 @@ public class DefaultFilterFactory implem
      */
     @Override
     public Literal literal(final short value) {
-        return new DefaultLiteral(value);
+        return new DefaultLiteral<>(value);
     }
 
     /**
@@ -740,7 +745,7 @@ public class DefaultFilterFactory implem
      */
     @Override
     public Literal literal(final int value) {
-        return new DefaultLiteral(value);
+        return new DefaultLiteral<>(value);
     }
 
     /**
@@ -748,7 +753,7 @@ public class DefaultFilterFactory implem
      */
     @Override
     public Literal literal(final long value) {
-        return new DefaultLiteral(value);
+        return new DefaultLiteral<>(value);
     }
 
     /**
@@ -756,7 +761,7 @@ public class DefaultFilterFactory implem
      */
     @Override
     public Literal literal(final float value) {
-        return new DefaultLiteral(value);
+        return new DefaultLiteral<>(value);
     }
 
     /**
@@ -764,7 +769,7 @@ public class DefaultFilterFactory implem
      */
     @Override
     public Literal literal(final double value) {
-        return new DefaultLiteral(value);
+        return new DefaultLiteral<>(value);
     }
 
     /**
@@ -772,7 +777,7 @@ public class DefaultFilterFactory implem
      */
     @Override
     public Literal literal(final char value) {
-        return new DefaultLiteral(value);
+        return new DefaultLiteral<>(value);
     }
 
     /**
@@ -780,7 +785,7 @@ public class DefaultFilterFactory implem
      */
     @Override
     public Literal literal(final boolean value) {
-        return new DefaultLiteral(value);
+        return new DefaultLiteral<>(value);
     }
 
     // SORT BY /////////////////////////////////////////////////////////////////
@@ -848,8 +853,7 @@ public class DefaultFilterFactory implem
      * {@inheritDoc }
      */
     @Override
-    public ArithmeticOperators arithmeticOperators(final boolean simple,
-            final Functions functions) {
+    public ArithmeticOperators arithmeticOperators(final boolean simple, final Functions
functions) {
         throw new UnsupportedOperationException("Not supported yet.");
     }
 
@@ -858,7 +862,8 @@ public class DefaultFilterFactory implem
      */
     @Override
     public ScalarCapabilities scalarCapabilities(final ComparisonOperators comparison,
-            final ArithmeticOperators arithmetic, final boolean logical) {
+            final ArithmeticOperators arithmetic, final boolean logical)
+    {
         throw new UnsupportedOperationException("Not supported yet.");
     }
 
@@ -867,7 +872,8 @@ public class DefaultFilterFactory implem
      */
     @Override
     public SpatialCapabilities spatialCapabilities(
-            final GeometryOperand[] geometryOperands, final SpatialOperators spatial) {
+            final GeometryOperand[] geometryOperands, final SpatialOperators spatial)
+    {
         throw new UnsupportedOperationException("Not supported yet.");
     }
 
@@ -885,7 +891,8 @@ public class DefaultFilterFactory implem
     @Override
     public FilterCapabilities capabilities(final String version,
             final ScalarCapabilities scalar, final SpatialCapabilities spatial,
-            final TemporalCapabilities temporal, final IdCapabilities id) {
+            final TemporalCapabilities temporal, final IdCapabilities id)
+    {
         throw new UnsupportedOperationException("Not supported yet.");
     }
 
@@ -896,5 +903,4 @@ public class DefaultFilterFactory implem
     public TemporalCapabilities temporalCapabilities(TemporalOperand[] temporalOperands,
TemporalOperators temporal) {
         throw new UnsupportedOperationException("Not supported yet.");
     }
-
 }

Modified: sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/filter/DefaultLiteral.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/filter/DefaultLiteral.java?rev=1757015&r1=1757014&r2=1757015&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/filter/DefaultLiteral.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/filter/DefaultLiteral.java
[UTF-8] Sat Aug 20 18:09:34 2016
@@ -16,18 +16,19 @@
  */
 package org.apache.sis.filter;
 
-import org.apache.sis.util.ObjectConverters;
-import org.apache.sis.util.UnconvertibleObjectException;
+import java.util.Objects;
 import org.opengis.filter.expression.ExpressionVisitor;
 import org.opengis.filter.expression.Literal;
 
+
 /**
  * Immutable literal expression.
  *
- * @author Johann Sorel (Geomatys)
  * @param <T> literal value type
- * @since   0.7
- * @version 0.7
+ *
+ * @author  Johann Sorel (Geomatys)
+ * @since   0.8
+ * @version 0.8
  * @module
  */
 public class DefaultLiteral<T> extends AbstractExpression implements Literal {
@@ -73,7 +74,7 @@ public class DefaultLiteral<T> extends A
      */
     @Override
     public String toString() {
-        return value == null ? "NULL" : value.toString();
+        return String.valueOf(value);
     }
 
     /**
@@ -81,32 +82,10 @@ public class DefaultLiteral<T> extends A
      */
     @Override
     public boolean equals(final Object obj) {
-        if (obj == null) {
-            return false;
-        }
-        if (getClass() != obj.getClass()) {
-            return false;
-        }
-        final DefaultLiteral<T> other = (DefaultLiteral<T>) obj;
-
-        if (this.value == null && other.value == null) {
-            return true;
-        }
-        if (this.value == null) {
-            return false;
-        }
-        if (other.value == null) {
+        if (obj == null || getClass() != obj.getClass()) {
             return false;
         }
-
-        final Object otherVal;
-        try{
-            otherVal = ObjectConverters.convert(other.value, this.value.getClass());
-        }catch(UnconvertibleObjectException ex){
-            //type can not be matched
-            return false;
-        }
-        return this.value.equals(otherVal);
+        return Objects.equals(value, ((DefaultLiteral<?>) obj).value);
     }
 
     /**
@@ -114,9 +93,6 @@ public class DefaultLiteral<T> extends A
      */
     @Override
     public int hashCode() {
-        int hash = 3;
-        hash = 89 * hash + (this.value != null ? this.value.hashCode() : 0);
-        return hash;
+        return Objects.hashCode(value) + 3;
     }
-
 }

Modified: sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/filter/DefaultPropertyName.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/filter/DefaultPropertyName.java?rev=1757015&r1=1757014&r2=1757015&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/filter/DefaultPropertyName.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/filter/DefaultPropertyName.java
[UTF-8] Sat Aug 20 18:09:34 2016
@@ -17,20 +17,24 @@
 package org.apache.sis.filter;
 
 import java.util.Map;
-import static org.apache.sis.util.ArgumentChecks.ensureNonNull;
+import org.apache.sis.util.ArgumentChecks;
+
+// Branch-dependent imports
+import java.util.Objects;
 import org.opengis.feature.Feature;
 import org.opengis.feature.PropertyNotFoundException;
 import org.opengis.filter.expression.ExpressionVisitor;
 import org.opengis.filter.expression.PropertyName;
 
+
 /**
  * Immutable property name expression.
  * A property name does not store any value, it acts as an indirection to a
  * property value of the evaluated feature.
  *
- * @author Johann Sorel (Geomatys)
- * @since   0.7
- * @version 0.7
+ * @author  Johann Sorel (Geomatys)
+ * @since   0.8
+ * @version 0.8
  * @module
  */
 public class DefaultPropertyName extends AbstractExpression implements PropertyName {
@@ -40,11 +44,11 @@ public class DefaultPropertyName extends
     private final String property;
 
     /**
-     * 
+     *
      * @param property attribute name
      */
     public DefaultPropertyName(final String property) {
-        ensureNonNull("property name", property);
+        ArgumentChecks.ensureNonNull("property", property);
         this.property = property;
     }
 
@@ -61,17 +65,15 @@ public class DefaultPropertyName extends
      */
     @Override
     public Object evaluate(final Object candidate) {
-
-        if(candidate instanceof Feature){
-            try{
+        if (candidate instanceof Feature) {
+            try {
                 return ((Feature) candidate).getPropertyValue(property);
-            }catch(PropertyNotFoundException ex){
+            } catch (PropertyNotFoundException ex) {
                 return null;
             }
-        }else if(candidate instanceof Map){
-            return ((Map) candidate).get(property);
+        } else if (candidate instanceof Map<?,?>) {
+            return ((Map<?,?>) candidate).get(property);
         }
-
         return null;
     }
 
@@ -88,7 +90,7 @@ public class DefaultPropertyName extends
      */
     @Override
     public String toString() {
-        return "{"+property+"}";
+        return '{' + property + '}';
     }
 
     /**
@@ -96,17 +98,10 @@ public class DefaultPropertyName extends
      */
     @Override
     public boolean equals(final Object obj) {
-        if (obj == null) {
-            return false;
-        }
-        if (getClass() != obj.getClass()) {
-            return false;
-        }
-        final DefaultPropertyName other = (DefaultPropertyName) obj;
-        if ((this.property == null) ? (other.property != null) : !this.property.equals(other.property))
{
+        if (obj == null || getClass() != obj.getClass()) {
             return false;
         }
-        return true;
+        return Objects.equals(property, ((DefaultPropertyName) obj).property);
     }
 
     /**
@@ -114,8 +109,6 @@ public class DefaultPropertyName extends
      */
     @Override
     public int hashCode() {
-        int hash = 7;
-        hash = 73 * hash + (this.property != null ? this.property.hashCode() : 0);
-        return hash;
+        return Objects.hashCode(property) + 7;
     }
 }

Modified: sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/filter/DefaultPropertyNameTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/filter/DefaultPropertyNameTest.java?rev=1757015&r1=1757014&r2=1757015&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/filter/DefaultPropertyNameTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/filter/DefaultPropertyNameTest.java
[UTF-8] Sat Aug 20 18:09:34 2016
@@ -16,32 +16,32 @@
  */
 package org.apache.sis.filter;
 
-import java.util.HashMap;
 import java.util.Map;
-import static org.apache.sis.test.Assert.*;
-import org.apache.sis.test.TestCase;
-import org.apache.sis.util.iso.Names;
-import org.junit.Test;
+import java.util.HashMap;
 import org.opengis.filter.FilterFactory2;
 import org.opengis.filter.expression.PropertyName;
+import org.apache.sis.util.iso.Names;
+import org.apache.sis.test.TestCase;
+import org.junit.Test;
+
+import static org.apache.sis.test.Assert.*;
+
 
 /**
  * Tests {@link DefaultPropertyName}.
  *
  * @author Johann Sorel (Geomatys)
- * @since   0.7
- * @version 0.7
+ * @since   0.8
+ * @version 0.8
  * @module
  */
-public class DefaultPropertyNameTest extends TestCase {
-
+public final strictfp class DefaultPropertyNameTest extends TestCase {
     /**
      * Test factory.
      */
     @Test
     public void testConstructor() {
         final FilterFactory2 FF = new DefaultFilterFactory();
-
         assertNotNull(FF.property(Names.parseGenericName(null, ":", "type")));
         assertNotNull(FF.property("type"));
     }
@@ -51,7 +51,7 @@ public class DefaultPropertyNameTest ext
      */
     @Test
     public void testEvaluate() {
-        final Map candidate = new HashMap();
+        final Map<String,String> candidate = new HashMap<>();
 
         final PropertyName prop = new DefaultPropertyName("type");
         assertEquals("type", prop.getPropertyName());
@@ -65,10 +65,9 @@ public class DefaultPropertyNameTest ext
 
         candidate.put("type", "45.1");
         assertEquals("45.1", prop.evaluate(candidate));
-        assertEquals("45.1", prop.evaluate(candidate,null));
-        assertEquals("45.1", prop.evaluate(candidate,String.class));
-        assertEquals(45.1, prop.evaluate(candidate,Double.class), STRICT);
-
+        assertEquals("45.1", prop.evaluate(candidate, Object.class));
+        assertEquals("45.1", prop.evaluate(candidate, String.class));
+        assertEquals( 45.1,  prop.evaluate(candidate, Double.class), STRICT);
     }
 
     /**
@@ -78,6 +77,4 @@ public class DefaultPropertyNameTest ext
     public void testSerialize() {
         assertSerializedEquals(new DefaultPropertyName("type"));
     }
-
 }
-



Mime
View raw message