← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21007: add class for doing cached object type lookups, used in Operators

 

------------------------------------------------------------
revno: 21007
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2015-11-10 14:03:49 +0700
message:
  add class for doing cached object type lookups, used in Operators
added:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Type.java
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/BetweenOperator.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/EqualOperator.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/GreaterEqualOperator.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/GreaterThanOperator.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/InOperator.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/LessEqualOperator.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/LessThanOperator.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/LikeOperator.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/Operator.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
=== added file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Type.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Type.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/Type.java	2015-11-10 07:03:49 +0000
@@ -0,0 +1,172 @@
+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.base.MoreObjects;
+
+import java.util.Collection;
+import java.util.Date;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * Simple class for caching of object type. Mainly for usage in speeding up Operator type lookup.
+ *
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class Type
+{
+    private boolean isString;
+
+    private boolean isChar;
+
+    private boolean isByte;
+
+    private boolean isNumber;
+
+    private boolean isInteger;
+
+    private boolean isFloat;
+
+    private boolean isDouble;
+
+    private boolean isBoolean;
+
+    private boolean isEnum;
+
+    private boolean isDate;
+
+    private boolean isCollection;
+
+    private boolean isList;
+
+    private boolean isSet;
+
+    public Type( Object object )
+    {
+        isString = String.class.isInstance( object );
+        isChar = Character.class.isInstance( object );
+        isByte = Byte.class.isInstance( object );
+        isNumber = Number.class.isInstance( object );
+        isInteger = Integer.class.isInstance( object );
+        isFloat = Float.class.isInstance( object );
+        isDouble = Double.class.isInstance( object );
+        isBoolean = Boolean.class.isInstance( object );
+        isEnum = Enum.class.isInstance( object );
+        isDate = Date.class.isInstance( object );
+        isCollection = Collection.class.isInstance( object );
+        isList = List.class.isInstance( object );
+        isSet = Set.class.isInstance( object );
+    }
+
+    public boolean isString()
+    {
+        return isString;
+    }
+
+    public boolean isChar()
+    {
+        return isChar;
+    }
+
+    public boolean isByte()
+    {
+        return isByte;
+    }
+
+    public boolean isNumber()
+    {
+        return isNumber;
+    }
+
+    public boolean isInteger()
+    {
+        return isInteger;
+    }
+
+    public boolean isFloat()
+    {
+        return isFloat;
+    }
+
+    public boolean isDouble()
+    {
+        return isDouble;
+    }
+
+    public boolean isBoolean()
+    {
+        return isBoolean;
+    }
+
+    public boolean isEnum()
+    {
+        return isEnum;
+    }
+
+    public boolean isDate()
+    {
+        return isDate;
+    }
+
+    public boolean isCollection()
+    {
+        return isCollection;
+    }
+
+    public boolean isList()
+    {
+        return isList;
+    }
+
+    public boolean isSet()
+    {
+        return isSet;
+    }
+
+    @Override
+    public String toString()
+    {
+        return MoreObjects.toStringHelper( this )
+            .add( "isString", isString )
+            .add( "isChar", isChar )
+            .add( "isByte", isByte )
+            .add( "isNumber", isNumber )
+            .add( "isInteger", isInteger )
+            .add( "isFloat", isFloat )
+            .add( "isDouble", isDouble )
+            .add( "isBoolean", isBoolean )
+            .add( "isEnum", isEnum )
+            .add( "isDate", isDate )
+            .add( "isCollection", isCollection )
+            .add( "isList", isList )
+            .add( "isSet", isSet )
+            .toString();
+    }
+}

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/BetweenOperator.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/BetweenOperator.java	2015-11-09 06:56:21 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/BetweenOperator.java	2015-11-10 07:03:49 +0000
@@ -30,6 +30,7 @@
 
 import org.hibernate.criterion.Criterion;
 import org.hibernate.criterion.Restrictions;
+import org.hisp.dhis.query.Type;
 import org.hisp.dhis.query.Typed;
 
 import java.util.Collection;
@@ -59,7 +60,9 @@
             return false;
         }
 
-        if ( Integer.class.isInstance( value ) )
+        Type type = new Type( value );
+
+        if ( type.isInteger() )
         {
             Integer s1 = getValue( Integer.class, value );
             Integer min = getValue( Integer.class, 0 );
@@ -67,7 +70,7 @@
 
             return s1 >= min && s1 <= max;
         }
-        else if ( Float.class.isInstance( value ) )
+        else if ( type.isFloat() )
         {
             Float s1 = getValue( Float.class, value );
             Integer min = getValue( Integer.class, 0 );
@@ -75,7 +78,7 @@
 
             return s1 >= min && s1 <= max;
         }
-        else if ( Date.class.isInstance( value ) )
+        else if ( type.isDate() )
         {
             Date min = getValue( Date.class, 0 );
             Date max = getValue( Date.class, 1 );
@@ -83,7 +86,7 @@
 
             return (s2.equals( min ) || s2.after( min )) && (s2.before( max ) || s2.equals( max ));
         }
-        else if ( Collection.class.isInstance( value ) )
+        else if ( type.isCollection() )
         {
             Collection<?> collection = (Collection<?>) value;
             Integer min = getValue( Integer.class, 0 );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/EqualOperator.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/EqualOperator.java	2015-11-05 03:55:41 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/EqualOperator.java	2015-11-10 07:03:49 +0000
@@ -30,6 +30,7 @@
 
 import org.hibernate.criterion.Criterion;
 import org.hibernate.criterion.Restrictions;
+import org.hisp.dhis.query.Type;
 import org.hisp.dhis.query.Typed;
 
 import java.util.Collection;
@@ -59,49 +60,51 @@
             return false;
         }
 
-        if ( String.class.isInstance( value ) )
+        Type type = new Type( value );
+
+        if ( type.isString() )
         {
             String s1 = getValue( String.class );
             String s2 = (String) value;
 
             return s1 != null && s2.equals( s1 );
         }
-        else if ( Boolean.class.isInstance( value ) )
+        else if ( type.isBoolean() )
         {
             Boolean s1 = getValue( Boolean.class );
             Boolean s2 = (Boolean) value;
 
             return s1 != null && s2.equals( s1 );
         }
-        else if ( Integer.class.isInstance( value ) )
+        else if ( type.isInteger() )
         {
             Integer s1 = getValue( Integer.class );
             Integer s2 = (Integer) value;
 
             return s1 != null && s2.equals( s1 );
         }
-        else if ( Float.class.isInstance( value ) )
+        else if ( type.isFloat() )
         {
             Float s1 = getValue( Float.class );
             Float s2 = (Float) value;
 
             return s1 != null && s2.equals( s1 );
         }
-        else if ( Collection.class.isInstance( value ) )
+        else if ( type.isCollection() )
         {
             Collection<?> collection = (Collection<?>) value;
             Integer size = getValue( Integer.class );
 
             return size != null && collection.size() == size;
         }
-        else if ( Date.class.isInstance( value ) )
+        else if ( type.isDate() )
         {
             Date s1 = getValue( Date.class );
             Date s2 = (Date) value;
 
             return s1 != null && s2.equals( s1 );
         }
-        else if ( Enum.class.isInstance( value ) )
+        else if ( type.isEnum() )
         {
             String s1 = String.valueOf( args.get( 0 ) );
             String s2 = String.valueOf( value );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/GreaterEqualOperator.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/GreaterEqualOperator.java	2015-11-09 06:56:21 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/GreaterEqualOperator.java	2015-11-10 07:03:49 +0000
@@ -30,6 +30,7 @@
 
 import org.hibernate.criterion.Criterion;
 import org.hibernate.criterion.Restrictions;
+import org.hisp.dhis.query.Type;
 import org.hisp.dhis.query.Typed;
 
 import java.util.Collection;
@@ -59,28 +60,30 @@
             return false;
         }
 
-        if ( Integer.class.isInstance( value ) )
+        Type type = new Type( value );
+
+        if ( type.isInteger() )
         {
             Integer s1 = getValue( Integer.class );
             Integer s2 = (Integer) value;
 
             return s1 != null && s2 >= s1;
         }
-        else if ( Float.class.isInstance( value ) )
+        else if ( type.isFloat() )
         {
             Float s1 = getValue( Float.class );
             Float s2 = (Float) value;
 
             return s1 != null && s2 >= s1;
         }
-        else if ( Date.class.isInstance( value ) )
+        else if ( type.isDate() )
         {
             Date s1 = getValue( Date.class );
             Date s2 = (Date) value;
 
             return s1 != null && (s2.after( s1 ) || s2.equals( s1 ));
         }
-        else if ( Collection.class.isInstance( value ) )
+        else if ( type.isCollection() )
         {
             Collection<?> collection = (Collection<?>) value;
             Integer size = getValue( Integer.class );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/GreaterThanOperator.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/GreaterThanOperator.java	2015-11-05 03:55:41 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/GreaterThanOperator.java	2015-11-10 07:03:49 +0000
@@ -30,6 +30,7 @@
 
 import org.hibernate.criterion.Criterion;
 import org.hibernate.criterion.Restrictions;
+import org.hisp.dhis.query.Type;
 import org.hisp.dhis.query.Typed;
 
 import java.util.Collection;
@@ -59,34 +60,36 @@
             return false;
         }
 
-        if ( Integer.class.isInstance( value ) )
+        Type type = new Type( value );
+
+        if ( type.isInteger() )
         {
             Integer s1 = getValue( Integer.class );
             Integer s2 = (Integer) value;
 
             return s1 != null && s2 > s1;
         }
-        else if ( Float.class.isInstance( value ) )
+        else if ( type.isFloat() )
         {
             Float s1 = getValue( Float.class );
             Float s2 = (Float) value;
 
             return s1 != null && s2 > s1;
         }
-        else if ( Collection.class.isInstance( value ) )
+        else if ( type.isDate() )
+        {
+            Date s1 = getValue( Date.class );
+            Date s2 = (Date) value;
+
+            return s1 != null && s2.after( s1 );
+        }
+        else if ( type.isCollection() )
         {
             Collection<?> collection = (Collection<?>) value;
             Integer size = getValue( Integer.class );
 
             return size != null && collection.size() > size;
         }
-        else if ( Date.class.isInstance( value ) )
-        {
-            Date s1 = getValue( Date.class );
-            Date s2 = (Date) value;
-
-            return s1 != null && s2.after( s1 );
-        }
 
         return false;
     }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/InOperator.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/InOperator.java	2015-11-05 06:07:23 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/InOperator.java	2015-11-10 07:03:49 +0000
@@ -30,6 +30,7 @@
 
 import org.hibernate.criterion.Criterion;
 import org.hibernate.criterion.Restrictions;
+import org.hisp.dhis.query.Type;
 import org.hisp.dhis.query.Typed;
 
 import java.util.Collection;
@@ -62,9 +63,11 @@
             return false;
         }
 
+        Type type = new Type( value );
+
         for ( Object item : items )
         {
-            if ( compare( item, value ) )
+            if ( compare( type, item, value ) )
             {
                 return true;
             }
@@ -73,44 +76,44 @@
         return false;
     }
 
-    private boolean compare( Object item, Object object )
+    private boolean compare( Type type, Object item, Object object )
     {
-        if ( String.class.isInstance( object ) )
+        if ( type.isString() )
         {
             String s1 = getValue( String.class, item );
             String s2 = (String) object;
 
             return s1 != null && s2.equals( s1 );
         }
-        else if ( Boolean.class.isInstance( object ) )
+        else if ( type.isBoolean() )
         {
             Boolean s1 = getValue( Boolean.class, item );
             Boolean s2 = (Boolean) object;
 
             return s1 != null && s2.equals( s1 );
         }
-        else if ( Integer.class.isInstance( object ) )
+        else if ( type.isInteger() )
         {
             Integer s1 = getValue( Integer.class, item );
             Integer s2 = (Integer) object;
 
             return s1 != null && s2.equals( s1 );
         }
-        else if ( Float.class.isInstance( object ) )
+        else if ( type.isFloat() )
         {
             Float s1 = getValue( Float.class, item );
             Float s2 = (Float) object;
 
             return s1 != null && s2.equals( s1 );
         }
-        else if ( Date.class.isInstance( object ) )
+        else if ( type.isDate() )
         {
             Date s1 = getValue( Date.class, item );
             Date s2 = (Date) object;
 
             return s1 != null && s2.equals( s1 );
         }
-        else if ( Enum.class.isInstance( object ) )
+        else if ( type.isEnum() )
         {
             String s2 = String.valueOf( object );
 

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/LessEqualOperator.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/LessEqualOperator.java	2015-11-05 03:55:41 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/LessEqualOperator.java	2015-11-10 07:03:49 +0000
@@ -30,6 +30,7 @@
 
 import org.hibernate.criterion.Criterion;
 import org.hibernate.criterion.Restrictions;
+import org.hisp.dhis.query.Type;
 import org.hisp.dhis.query.Typed;
 
 import java.util.Collection;
@@ -59,34 +60,36 @@
             return false;
         }
 
-        if ( Integer.class.isInstance( value ) )
+        Type type = new Type( value );
+
+        if ( type.isInteger() )
         {
             Integer s1 = getValue( Integer.class );
             Integer s2 = (Integer) value;
 
             return s1 != null && s2 <= s1;
         }
-        else if ( Float.class.isInstance( value ) )
+        else if ( type.isFloat() )
         {
             Float s1 = getValue( Float.class );
             Float s2 = (Float) value;
 
             return s1 != null && s2 <= s1;
         }
-        else if ( Collection.class.isInstance( value ) )
+        else if ( type.isDate() )
+        {
+            Date s1 = getValue( Date.class );
+            Date s2 = (Date) value;
+
+            return s1 != null && (s2.before( s1 ) || s2.equals( s1 ));
+        }
+        else if ( type.isCollection() )
         {
             Collection<?> collection = (Collection<?>) value;
             Integer size = getValue( Integer.class );
 
             return size != null && collection.size() <= size;
         }
-        else if ( Date.class.isInstance( value ) )
-        {
-            Date s1 = getValue( Date.class );
-            Date s2 = (Date) value;
-
-            return s1 != null && (s2.before( s1 ) || s2.equals( s1 ));
-        }
 
         return false;
     }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/LessThanOperator.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/LessThanOperator.java	2015-11-05 03:55:41 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/LessThanOperator.java	2015-11-10 07:03:49 +0000
@@ -30,6 +30,7 @@
 
 import org.hibernate.criterion.Criterion;
 import org.hibernate.criterion.Restrictions;
+import org.hisp.dhis.query.Type;
 import org.hisp.dhis.query.Typed;
 
 import java.util.Collection;
@@ -59,34 +60,36 @@
             return false;
         }
 
-        if ( Integer.class.isInstance( value ) )
+        Type type = new Type( value );
+
+        if ( type.isInteger() )
         {
             Integer s1 = getValue( Integer.class );
             Integer s2 = (Integer) value;
 
             return s1 != null && s2 < s1;
         }
-        else if ( Float.class.isInstance( value ) )
+        else if ( type.isFloat() )
         {
             Float s1 = getValue( Float.class );
             Float s2 = (Float) value;
 
             return s1 != null && s2 < s1;
         }
-        else if ( Collection.class.isInstance( value ) )
+        else if ( type.isDate() )
+        {
+            Date s1 = getValue( Date.class );
+            Date s2 = (Date) value;
+
+            return s1 != null && (s2.before( s1 ));
+        }
+        else if ( type.isCollection() )
         {
             Collection<?> collection = (Collection<?>) value;
             Integer size = getValue( Integer.class );
 
             return size != null && collection.size() < size;
         }
-        else if ( Date.class.isInstance( value ) )
-        {
-            Date s1 = getValue( Date.class );
-            Date s2 = (Date) value;
-
-            return s1 != null && (s2.before( s1 ));
-        }
 
         return false;
     }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/LikeOperator.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/LikeOperator.java	2015-11-09 05:35:52 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/LikeOperator.java	2015-11-10 07:03:49 +0000
@@ -31,6 +31,7 @@
 import org.hibernate.criterion.Criterion;
 import org.hibernate.criterion.MatchMode;
 import org.hibernate.criterion.Restrictions;
+import org.hisp.dhis.query.Type;
 import org.hisp.dhis.query.Typed;
 
 /**
@@ -70,7 +71,9 @@
             return false;
         }
 
-        if ( String.class.isInstance( value ) )
+        Type type = new Type( value );
+
+        if ( type.isString() )
         {
             String s1 = caseSensitive ? getValue( String.class ) : getValue( String.class ).toLowerCase();
             String s2 = caseSensitive ? (String) value : ((String) value).toLowerCase();

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/Operator.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/Operator.java	2015-11-05 08:40:46 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/Operator.java	2015-11-10 07:03:49 +0000
@@ -30,6 +30,7 @@
 
 import org.hibernate.criterion.Criterion;
 import org.hisp.dhis.query.QueryUtils;
+import org.hisp.dhis.query.Type;
 import org.hisp.dhis.query.Typed;
 
 import java.util.ArrayList;
@@ -45,6 +46,8 @@
 
     protected final Typed typed;
 
+    protected Type argumentType;
+
     public Operator( Typed typed )
     {
         this.typed = typed;
@@ -52,13 +55,15 @@
 
     public Operator( Typed typed, Object arg )
     {
-        this.typed = typed;
+        this( typed );
+        this.argumentType = new Type( arg );
         this.args.add( arg );
     }
 
     public Operator( Typed typed, Object... args )
     {
-        this.typed = typed;
+        this( typed );
+        this.argumentType = new Type( args[0] );
         Collections.addAll( this.args, args );
     }