← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 18346: typed parameters for Operator class

 

------------------------------------------------------------
revno: 18346
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2015-02-20 10:59:30 +0700
message:
  typed parameters for Operator class
added:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/Typed.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/Klass.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/Operator.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/Property.java
  dhis-2/dhis-api/src/test/java/org/hisp/dhis/query/QueryTest.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/Operator.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/Operator.java	2015-02-18 13:08:37 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/Operator.java	2015-02-20 03:59:30 +0000
@@ -28,44 +28,71 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.schema.Klass;
+
+import java.util.Date;
+
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
 public enum Operator
 {
-    // TODO should probably introduce typed parameters here, but for now we just use a simple integer
-    EQ( 1 ),
-    NE( 1 ),
-    GT( 1 ),
-    LT( 1 ),
-    GE( 1 ),
-    LE( 1 ),
-    BETWEEN( 2 ),
-    LIKE( 1 ),
+    EQ( Typed.from( String.class, Number.class, Date.class ), 1 ),
+    NE( Typed.from( String.class, Number.class, Date.class ), 1 ),
+    GT( Typed.from( String.class, Number.class, Date.class ), 1 ),
+    LT( Typed.from( String.class, Number.class, Date.class ), 1 ),
+    GE( Typed.from( String.class, Number.class, Date.class ), 1 ),
+    LE( Typed.from( String.class, Number.class, Date.class ), 1 ),
+    BETWEEN( Typed.from( String.class, Number.class, Date.class ), 2 ),
+    LIKE( Typed.from( String.class ), 1 ),
     IN( 1, Integer.MAX_VALUE );
 
     Integer min;
 
     Integer max;
 
+    // default is to allow all types
+    Typed typed = Typed.from();
+
     Operator()
     {
         this.min = null;
         this.max = null;
     }
 
+    Operator( Typed typed )
+    {
+        this.typed = typed;
+        this.min = null;
+        this.max = null;
+    }
+
     Operator( int value )
     {
         this.min = value;
         this.max = value;
     }
 
+    Operator( Typed typed, int value )
+    {
+        this.typed = typed;
+        this.min = value;
+        this.max = value;
+    }
+
     Operator( int min, int max )
     {
         this.min = min;
         this.max = max;
     }
 
+    Operator( Typed typed, int min, int max )
+    {
+        this.typed = typed;
+        this.min = min;
+        this.max = max;
+    }
+
     public Integer getMin()
     {
         return min;
@@ -75,4 +102,9 @@
     {
         return max;
     }
+
+    public boolean isValid( Klass klass )
+    {
+        return typed.isValid( klass );
+    }
 }

=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/Typed.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/Typed.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/Typed.java	2015-02-20 03:59:30 +0000
@@ -0,0 +1,76 @@
+package org.hisp.dhis.query;
+
+/*
+ * Copyright (c) 2004-2015, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import com.google.common.collect.Iterators;
+import org.hisp.dhis.schema.Klass;
+
+/**
+ * Simple class for checking if an object is one of several allowed classes, mainly used in Operator where
+ * a parameter can be type constrained.
+ *
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class Typed
+{
+    private final Class<?>[] klasses;
+
+    public Typed( Class<?>[] klasses )
+    {
+        this.klasses = klasses;
+    }
+
+    public boolean isValid( Klass klass )
+    {
+        if ( klasses.length == 0 || klass == null || klass.getKlass() == null )
+        {
+            return true;
+        }
+
+        for ( Class<?> k : klasses )
+        {
+            if ( k != null && k.isAssignableFrom( klass.getKlass() ) )
+            {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+    public static Typed from( Class... klasses )
+    {
+        return new Typed( klasses );
+    }
+
+    public static Typed from( Iterable<? extends Class> iterable )
+    {
+        return new Typed( Iterators.toArray( iterable.iterator(), Class.class ) );
+    }
+}

=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/Klass.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/Klass.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/Klass.java	2015-02-20 03:59:30 +0000
@@ -0,0 +1,39 @@
+package org.hisp.dhis.schema;
+
+/*
+ * Copyright (c) 2004-2015, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * Simple interface for classes that exposes a class containment. I.e. a Property have a property class.
+ *
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public interface Klass
+{
+    Class<?> getKlass();
+}

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/Property.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/Property.java	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/Property.java	2015-02-20 03:59:30 +0000
@@ -44,7 +44,7 @@
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
 @JacksonXmlRootElement( localName = "property", namespace = DxfNamespaces.DXF_2_0 )
-public class Property implements Ordered
+public class Property implements Ordered, Klass
 {
     /**
      * Class for property.

=== modified file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/query/QueryTest.java'
--- dhis-2/dhis-api/src/test/java/org/hisp/dhis/query/QueryTest.java	2015-02-18 13:08:37 +0000
+++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/query/QueryTest.java	2015-02-20 03:59:30 +0000
@@ -35,7 +35,7 @@
 
 import java.util.Date;
 
-import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.*;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -59,6 +59,7 @@
         schema.addProperty( createProperty( String.class, "id", true, true ) );
         schema.addProperty( createProperty( String.class, "name", true, true ) );
         schema.addProperty( createProperty( String.class, "code", true, true ) );
+        schema.addProperty( createProperty( Date.class, "created", true, true ) );
         schema.addProperty( createProperty( Date.class, "lastUpdated", true, true ) );
 
         return schema;
@@ -90,4 +91,20 @@
 
         assertEquals( 0, query.getRestrictions().size() );
     }
+
+    @Test
+    public void operatorValid()
+    {
+        Schema schema = createSchema();
+
+        assertTrue( Operator.EQ.isValid( schema.getProperty( "id" ) ) );
+        assertTrue( Operator.GE.isValid( schema.getProperty( "id" ) ) );
+        assertTrue( Operator.LT.isValid( schema.getProperty( "id" ) ) );
+        assertTrue( Operator.GE.isValid( schema.getProperty( "id" ) ) );
+        assertTrue( Operator.LE.isValid( schema.getProperty( "id" ) ) );
+        assertTrue( Operator.BETWEEN.isValid( schema.getProperty( "id" ) ) );
+        assertTrue( Operator.LIKE.isValid( schema.getProperty( "id" ) ) );
+
+        assertFalse( Operator.LIKE.isValid( schema.getProperty( "lastUpdated" ) ) );
+    }
 }