← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20755: removed old Predicate class, replaced by Java 8 Predicate. Cleaned up unused code in PredicateUti...

 

------------------------------------------------------------
revno: 20755
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2015-10-19 15:56:45 +0700
message:
  removed old Predicate class, replaced by Java 8 Predicate. Cleaned up unused code in PredicateUtils/CollectionUtils.
removed:
  dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/functional/
  dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/functional/Predicate.java
  dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/functional/package-info.java
modified:
  dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/collection/CollectionUtils.java
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/PredicateUtils.java
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.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-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/collection/CollectionUtils.java'
--- dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/collection/CollectionUtils.java	2015-10-01 13:57:42 +0000
+++ dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/collection/CollectionUtils.java	2015-10-19 08:56:45 +0000
@@ -28,56 +28,25 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import org.hisp.dhis.commons.functional.Predicate;
-
-import java.lang.reflect.Method;
-import java.util.AbstractMap;
-import java.util.ArrayList;
 import java.util.Collection;
-import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
 import java.util.Set;
 
 /**
  * Utility methods for operations on various collections.
- * 
+ *
  * @author Morten Olav Hansen
  */
 public class CollectionUtils
 {
     public static final String[] STRING_ARR = new String[0];
-    public static final String[][] STRING_2D_ARR = new String[0][];
-
-    /**
-     * Filters the given Collection on the given Predicate.
-     *
-     * @param collection the Collection.
-     * @param predicate the Predicate.
-     * @param <T> the type.
-     */
-    public static <T> void filter( Collection<T> collection, Predicate<T> predicate )
-    {
-        Iterator<T> iterator = collection.iterator();
-
-        while ( iterator.hasNext() )
-        {
-            T object = iterator.next();
-
-            if ( !predicate.evaluate( object ) )
-            {
-                iterator.remove();
-            }
-        }
-    }
 
     /**
      * Returns the intersection of the given Collections.
      *
-     * @param c1 the first Collection.
-     * @param c2 the second Collection.
+     * @param c1  the first Collection.
+     * @param c2  the second Collection.
      * @param <T> the type.
      * @return the intersection of the Collections.
      */
@@ -89,166 +58,28 @@
     }
 
     /**
-     * Constructs a Map Entry (key, value). Used to construct a Map with asMap.
-     *
-     * @param key map entry key.
-     * @param value map entry value.
-     * @param <K> key type.
-     * @param <V> value type.
-     * @return entry with the key and value.
-     */
-    public static <K, V> AbstractMap.SimpleEntry<K, V> asEntry( K key, V value )
-    {
-        return new AbstractMap.SimpleEntry<>( key, value );
-    }
-
-    /**
-     * Constructs a Map from Entries, each containing a (key, value) pair.
-     *
-     * @param entries any number of (key, value) pairs.
-     * @param <K> key type.
-     * @param <V> value type.
-     * @return Map of the entries
-     */
-    @SafeVarargs
-    public static final <K, V> Map<K, V> asMap( final AbstractMap.SimpleEntry<K, V>... entries )
-    {
-        Map<K, V> map = new HashMap<>();
-
-        for ( AbstractMap.SimpleEntry<K, V> entry : entries )
-        {
-            map.put( entry.getKey(), entry.getValue() );
-        }
-
-        return map;
-    }
-
-    /**
-     * Creates a map with the elements of the collection as values using the
-     * specified keyMethod to obtain the key from the elements.
-     *
-     * @param collection the Collection.
-     * @param keyMethod the name of the method to obtain the key.
-     * @param <K> key type.
-     * @param <T> value type.
-     * @return Map of the elements.
-     */
-    @SuppressWarnings( "unchecked" )
-    public static <K, T> Map<K, T> createMap( Collection<T> collection, String keyMethod )
-    {
-        Map<K, T> map = new HashMap<>( collection.size() );
-
-        if ( collection.isEmpty() )
-        {
-            return map;
-        }
-
-        Class<?> elementClass = collection.iterator().next().getClass();
-
-        Method getKeyMethod;
-
-        try
-        {
-            getKeyMethod = elementClass.getMethod( keyMethod, new Class[0] );
-        }
-        catch ( Exception e )
-        {
-            throw new RuntimeException( "Failed to get key method", e );
-        }
-
-        for ( T element : collection )
-        {
-            K key;
-            try
-            {
-                key = (K) getKeyMethod.invoke( element, (Object[]) null );
-            }
-            catch ( Exception e )
-            {
-                throw new RuntimeException( "Failed to get key", e );
-            }
-
-            map.put( key, element );
-        }
-
-        return map;
-    }
-
-    /**
-     * Creates a list of values extracted from the provided list using the
-     * specified value method, keeping the order of the provided list.
-     *
-     * @param list the List.
-     * @param valueMethod the name of the method to obtain the value.
-     * @param <K> key type.
-     * @param <T> value type.
-     * @return an ordered List of the obtained values.
-     */
-    @SuppressWarnings( "unchecked" )
-    public static <K, T> List<K> createList( List<T> list, String valueMethod )
-    {
-        List<K> valueList = new ArrayList<>( list.size() );
-
-        if ( list.isEmpty() )
-        {
-            return valueList;
-        }
-
-        Class<?> elementClass = list.iterator().next().getClass();
-
-        Method getValueMethod;
-
-        try
-        {
-            getValueMethod = elementClass.getMethod( valueMethod, new Class[0] );
-        }
-        catch ( Exception e )
-        {
-            throw new RuntimeException( "Failed to get key method", e );
-        }
-
-        for ( T element : list )
-        {
-            K value;
-
-            try
-            {
-                value = (K) getValueMethod.invoke( element, (Object[]) null );
-            }
-            catch ( Exception e )
-            {
-                throw new RuntimeException( "Failed to get key", e );
-            }
-
-            valueList.add( value );
-        }
-
-        return valueList;
-    }
-    
-    /**
      * Searches for and returns the first string which starts with the given
      * prefix. Removes the match from the collection.
-     * 
+     *
      * @param collection the collection.
-     * @param prefix the string prefix.
+     * @param prefix     the string prefix.
      * @return a string, or null if no matches.
      */
     public static String popStartsWith( Collection<String> collection, String prefix )
     {
         Iterator<String> iterator = collection.iterator();
-        
+
         while ( iterator.hasNext() )
         {
             String element = iterator.next();
-            
+
             if ( element != null && element.startsWith( prefix ) )
             {
                 iterator.remove();
                 return element;
             }
         }
-        
+
         return null;
     }
 }

=== removed directory 'dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/functional'
=== removed file 'dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/functional/Predicate.java'
--- dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/functional/Predicate.java	2015-10-01 13:57:42 +0000
+++ dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/functional/Predicate.java	1970-01-01 00:00:00 +0000
@@ -1,39 +0,0 @@
-package org.hisp.dhis.commons.functional;
-
-/*
- * 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.
- */
-
-/**
- * Predicate on T.
- *
- * @author Morten Olav Hansen
- */
-public interface Predicate<T>
-{
-    boolean evaluate( T object );
-}

=== removed file 'dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/functional/package-info.java'
--- dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/functional/package-info.java	2015-10-01 15:07:20 +0000
+++ dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/functional/package-info.java	1970-01-01 00:00:00 +0000
@@ -1,7 +0,0 @@
-
-/**
- * Functional interfaces.
- * 
- * @author Lars Helge Overland
- */
-package org.hisp.dhis.commons.functional;
\ No newline at end of file

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/PredicateUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/PredicateUtils.java	2015-06-15 13:44:20 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/PredicateUtils.java	2015-10-19 08:56:45 +0000
@@ -30,55 +30,32 @@
 
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.annotation.Scanned;
-import org.hisp.dhis.commons.functional.Predicate;
 
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Field;
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
 import java.util.Collection;
+import java.util.function.Predicate;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
 public class PredicateUtils
 {
-    public static final Predicate<Field> alwaysTrue = new StaticReply( true );
-
-    public static final Predicate<Field> alwaysFalse = new StaticReply( false );
-
     public static final Predicate<Field> idObjects = new ObjectWithTypePredicate( IdentifiableObject.class );
 
     public static final Predicate<Field> collections = new CollectionPredicate();
 
     public static final Predicate<Field> idObjectCollections = new CollectionWithTypePredicate( IdentifiableObject.class );
 
-    public static final Predicate<Field> objectCollectionsWithScanned = new CollectionWithAnnotationPredicate( Scanned.class );
-
     public static final Predicate<Field> idObjectCollectionsWithScanned = new CollectionWithTypeAndAnnotationPredicate( IdentifiableObject.class, Scanned.class );
 
-    public static class StaticReply
-        implements Predicate<Field>
-    {
-        private boolean value = false;
-
-        public StaticReply( boolean value )
-        {
-            this.value = value;
-        }
-
-        @Override
-        public boolean evaluate( Field object )
-        {
-            return value;
-        }
-    }
-
     public static class CollectionPredicate
         implements Predicate<Field>
     {
         @Override
-        public boolean evaluate( Field field )
+        public boolean test( Field field )
         {
             return Collection.class.isAssignableFrom( field.getType() );
         }
@@ -97,9 +74,9 @@
         }
 
         @Override
-        public boolean evaluate( Field field )
+        public boolean test( Field field )
         {
-            if ( collectionPredicate.evaluate( field ) )
+            if ( collectionPredicate.test( field ) )
             {
                 ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
                 Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
@@ -117,33 +94,6 @@
         }
     }
 
-    public static class CollectionWithAnnotationPredicate
-        implements Predicate<Field>
-    {
-        private CollectionPredicate collectionPredicate = new CollectionPredicate();
-
-        private Class<? extends Annotation> annotation;
-
-        public CollectionWithAnnotationPredicate( Class<? extends Annotation> annotation )
-        {
-            this.annotation = annotation;
-        }
-
-        @Override
-        public boolean evaluate( Field field )
-        {
-            if ( field.isAnnotationPresent( annotation ) )
-            {
-                if ( collectionPredicate.evaluate( field ) )
-                {
-                    return true;
-                }
-            }
-
-            return false;
-        }
-    }
-
     public static class CollectionWithTypeAndAnnotationPredicate
         implements Predicate<Field>
     {
@@ -158,11 +108,11 @@
         }
 
         @Override
-        public boolean evaluate( Field field )
+        public boolean test( Field field )
         {
             if ( field.isAnnotationPresent( annotation ) )
             {
-                if ( collectionWithTypePredicate.evaluate( field ) )
+                if ( collectionWithTypePredicate.test( field ) )
                 {
                     return true;
                 }
@@ -183,7 +133,7 @@
         }
 
         @Override
-        public boolean evaluate( Field field )
+        public boolean test( Field field )
         {
             return type.isAssignableFrom( field.getType() );
         }

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.java	2015-10-01 13:31:25 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.java	2015-10-19 08:56:45 +0000
@@ -28,6 +28,10 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import javassist.util.proxy.ProxyFactory;
+import org.hibernate.collection.spi.PersistentCollection;
+import org.springframework.util.StringUtils;
+
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
@@ -43,12 +47,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-
-import javassist.util.proxy.ProxyFactory;
-
-import org.hibernate.collection.spi.PersistentCollection;
-import org.hisp.dhis.commons.functional.Predicate;
-import org.springframework.util.StringUtils;
+import java.util.function.Predicate;
 
 /**
  * @author Lars Helge Overland
@@ -469,12 +468,6 @@
         }
     }
 
-    @SuppressWarnings( "unchecked" )
-    public static <T> T getFieldObject( Field field, T target )
-    {
-        return (T) invokeGetterMethod( field.getName(), target );
-    }
-
     public static Collection<Field> collectFields( Class<?> clazz, Predicate<Field> predicate )
     {
         Class<?> type = clazz;
@@ -486,7 +479,7 @@
 
             for ( Field field : declaredFields )
             {
-                if ( Modifier.isStatic( field.getModifiers() ) || (predicate != null && !predicate.evaluate( field )) )
+                if ( Modifier.isStatic( field.getModifiers() ) || (predicate != null && !predicate.test( field )) )
                 {
                     continue;
                 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.java	2015-09-24 05:44:40 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.java	2015-10-19 08:56:45 +0000
@@ -657,22 +657,11 @@
         // TODO optimize this using field filter (collection filtering)
         if ( !rootNode.getChildren().isEmpty() && rootNode.getChildren().get( 0 ).isCollection() )
         {
-            for ( Node node : rootNode.getChildren().get( 0 ).getChildren() )
-            {
-                if ( node.isComplex() )
-                {
-                    for ( Node child : node.getChildren() )
-                    {
-                        if ( child.isSimple() && child.getName().equals( "id" ) )
-                        {
-                            if ( !((SimpleNode) child).getValue().equals( pvItemId ) )
-                            {
-                                rootNode.getChildren().get( 0 ).removeChild( node );
-                            }
-                        }
-                    }
-                }
-            }
+            rootNode.getChildren().get( 0 ).getChildren().stream().filter( Node::isComplex ).forEach( node -> {
+                node.getChildren().stream()
+                    .filter( child -> child.isSimple() && child.getName().equals( "id" ) && !((SimpleNode) child).getValue().equals( pvItemId ) )
+                    .forEach( child -> rootNode.getChildren().get( 0 ).removeChild( node ) );
+            } );
         }
 
         return rootNode;
@@ -1142,17 +1131,4 @@
 
         return entitySimpleName;
     }
-
-    @SuppressWarnings( "unchecked" )
-    protected T getEntityInstance()
-    {
-        try
-        {
-            return (T) Class.forName( getEntityName() ).newInstance();
-        }
-        catch ( InstantiationException | IllegalAccessException | ClassNotFoundException ex )
-        {
-            throw new RuntimeException( ex );
-        }
-    }
 }