← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20616: BaseAnalyticalObject, java8 updates

 

------------------------------------------------------------
revno: 20616
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2015-10-11 21:50:39 +0200
message:
  BaseAnalyticalObject, java8 updates
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseAnalyticalObject.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectUtils.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/common/BaseAnalyticalObject.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseAnalyticalObject.java	2015-09-17 07:17:45 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseAnalyticalObject.java	2015-10-11 19:50:39 +0000
@@ -49,6 +49,7 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.stream.Collectors;
 
 import org.apache.commons.lang3.StringUtils;
 import org.hisp.dhis.analytics.AggregationType;
@@ -87,6 +88,7 @@
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Maps;
 
 /**
  * This class contains associations to dimensional meta-data. Should typically
@@ -247,14 +249,7 @@
      */
     public List<NameableObject> getDataDimensionNameableObjects()
     {
-        List<NameableObject> items = new ArrayList<>();
-        
-        for ( DataDimensionItem item : dataDimensionItems )
-        {
-            items.add( item.getNameableObject() );
-        }
-        
-        return items;
+        return dataDimensionItems.stream().map( DataDimensionItem::getNameableObject ).collect( Collectors.toList() );
     }
     
     /**
@@ -290,36 +285,21 @@
     @JsonIgnore
     public List<DataElement> getDataElements()
     {
-        List<DataElement> objects = new ArrayList<>();
-        
-        for ( DataDimensionItem item : dataDimensionItems )
-        {
-            if ( item.getDataElement() != null )
-            {
-                objects.add( item.getDataElement() );
-            }
-        }
-        
-        return ImmutableList.copyOf( objects );
+        return ImmutableList.copyOf( dataDimensionItems.stream().
+            filter( i -> i.getDataElement() != null ).
+            map( DataDimensionItem::getDataElement ).collect( Collectors.toList() ) );
     }
 
     /**
-     * Returns all data elements in the data dimensions.
+     * Returns all indicators in the data dimensions. The returned list is
+     * immutable.
      */
     @JsonIgnore
     public List<Indicator> getIndicators()
     {
-        List<Indicator> objects = new ArrayList<>();
-        
-        for ( DataDimensionItem items : dataDimensionItems )
-        {
-            if ( items.getIndicator() != null )
-            {
-                objects.add( items.getIndicator() );
-            }
-        }
-        
-        return ImmutableList.copyOf( objects );
+        return ImmutableList.copyOf( dataDimensionItems.stream().
+            filter( i -> i.getIndicator() != null ).
+            map( DataDimensionItem::getIndicator ).collect( Collectors.toList() ) );
     }
 
     /**
@@ -476,12 +456,7 @@
 
             // Tracked entity attribute
 
-            Map<String, TrackedEntityAttributeDimension> attributes = new HashMap<>();
-
-            for ( TrackedEntityAttributeDimension attribute : attributeDimensions )
-            {
-                attributes.put( attribute.getUid(), attribute );
-            }
+            Map<String, TrackedEntityAttributeDimension> attributes = Maps.uniqueIndex( attributeDimensions, TrackedEntityAttributeDimension::getUid );
 
             if ( attributes.containsKey( dimension ) )
             {
@@ -492,12 +467,7 @@
 
             // Tracked entity data element
 
-            Map<String, TrackedEntityDataElementDimension> dataElements = new HashMap<>();
-
-            for ( TrackedEntityDataElementDimension dataElement : dataElementDimensions )
-            {
-                dataElements.put( dataElement.getUid(), dataElement );
-            }
+            Map<String, TrackedEntityDataElementDimension> dataElements = Maps.uniqueIndex( dataElementDimensions, TrackedEntityDataElementDimension::getUid );
 
             if ( dataElements.containsKey( dimension ) )
             {
@@ -668,12 +638,7 @@
 
             // Tracked entity attribute
 
-            Map<String, TrackedEntityAttributeDimension> attributes = new HashMap<>();
-
-            for ( TrackedEntityAttributeDimension attribute : attributeDimensions )
-            {
-                attributes.put( attribute.getUid(), attribute );
-            }
+            Map<String, TrackedEntityAttributeDimension> attributes = Maps.uniqueIndex( attributeDimensions, TrackedEntityAttributeDimension::getUid );
 
             if ( attributes.containsKey( dimension ) )
             {
@@ -684,12 +649,7 @@
 
             // Tracked entity data element
 
-            Map<String, TrackedEntityDataElementDimension> dataElements = new HashMap<>();
-
-            for ( TrackedEntityDataElementDimension dataElement : dataElementDimensions )
-            {
-                dataElements.put( dataElement.getUid(), dataElement );
-            }
+            Map<String, TrackedEntityDataElementDimension> dataElements = Maps.uniqueIndex( dataElementDimensions, TrackedEntityDataElementDimension::getUid );
 
             if ( dataElements.containsKey( dimension ) )
             {

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectUtils.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectUtils.java	2015-10-08 15:04:40 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectUtils.java	2015-10-11 19:50:39 +0000
@@ -51,6 +51,7 @@
 import org.hisp.dhis.period.PeriodType;
 
 import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
 
 /**
  * @author Lars Helge Overland
@@ -333,17 +334,7 @@
      */
     public static <T extends IdentifiableObject> Map<String, T> getUidObjectMap( Collection<T> objects )
     {
-        Map<String, T> map = new HashMap<>();
-
-        if ( objects != null )
-        {
-            for ( T object : objects )
-            {
-                map.put( object.getUid(), object );
-            }
-        }
-
-        return map;
+        return objects != null ? Maps.uniqueIndex( objects, T::getUid ) : Maps.newHashMap();
     }
 
     /**