dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #41127
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20945: in-memory implementation of between operator
------------------------------------------------------------
revno: 20945
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2015-11-04 14:52:45 +0700
message:
in-memory implementation of between operator
modified:
dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/Between.java
dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/query/OperatorTest.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-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/Between.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/Between.java 2015-11-04 06:46:21 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/Between.java 2015-11-04 07:52:45 +0000
@@ -30,6 +30,7 @@
import org.hisp.dhis.query.Typed;
+import java.util.Collection;
import java.util.Date;
/**
@@ -37,14 +38,44 @@
*/
public class Between extends Operator
{
- public Between( String arg )
+ public Between( String arg0, String arg1 )
{
- super( Typed.from( String.class, Number.class, Date.class ), arg );
+ super( Typed.from( String.class, Number.class, Date.class ), arg0, arg1 );
}
@Override
public boolean test( Object value )
{
+ if ( args.isEmpty() || value == null )
+ {
+ return false;
+ }
+
+ if ( Integer.class.isInstance( value ) )
+ {
+ Integer s1 = getValue( Integer.class, value );
+ Integer min = getValue( Integer.class, 0 );
+ Integer max = getValue( Integer.class, 1 );
+
+ return s1 >= min && s1 <= max;
+ }
+ else if ( Float.class.isInstance( value ) )
+ {
+ Float s1 = getValue( Float.class, value );
+ Integer min = getValue( Integer.class, 0 );
+ Integer max = getValue( Integer.class, 1 );
+
+ return s1 >= min && s1 <= max;
+ }
+ else if ( Collection.class.isInstance( value ) )
+ {
+ Collection<?> collection = (Collection<?>) value;
+ Integer min = getValue( Integer.class, 0 );
+ Integer max = getValue( Integer.class, 1 );
+
+ return collection.size() >= min && collection.size() <= max;
+ }
+
return false;
}
}
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/query/OperatorTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/query/OperatorTest.java 2015-11-04 06:26:51 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/query/OperatorTest.java 2015-11-04 07:52:45 +0000
@@ -41,6 +41,7 @@
import org.hisp.dhis.query.operators.Null;
import org.junit.Test;
+import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
@@ -56,7 +57,7 @@
@Test
public void testBetweenValidTypes()
{
- Between operator = new Between( "operator" );
+ Between operator = new Between( "10", "20" );
assertTrue( operator.isValid( String.class ) );
assertTrue( operator.isValid( Number.class ) );
@@ -65,6 +66,30 @@
}
@Test
+ public void testBetweenInt()
+ {
+ Between operator = new Between( "10", "20" );
+
+ assertTrue( operator.test( 10 ) );
+ assertTrue( operator.test( 15 ) );
+ assertTrue( operator.test( 20 ) );
+ assertFalse( operator.test( 9 ) );
+ assertFalse( operator.test( 21 ) );
+ }
+
+ @Test
+ public void testBetweenCollection()
+ {
+ Between operator = new Between( "2", "4" );
+
+ assertFalse( operator.test( Collections.singletonList( 1 ) ) );
+ assertTrue( operator.test( Arrays.asList( 1, 2 ) ) );
+ assertTrue( operator.test( Arrays.asList( 1, 2, 3 ) ) );
+ assertTrue( operator.test( Arrays.asList( 1, 2, 3, 4 ) ) );
+ assertFalse( operator.test( Arrays.asList( 1, 2, 3, 4, 5 ) ) );
+ }
+
+ @Test
public void testEqualValidTypes()
{
Equal operator = new Equal( "operator" );