[view] do not mutate objects in stream
authorAsier Lostalé <asier.lostale@openbravo.com>
Tue, 07 Aug 2018 08:44:29 +0200
changeset 35074 9e0c546f3a14
parent 35073 6b0da887fc26
child 35075 2388b7bdd44a
[view] do not mutate objects in stream
modules/org.openbravo.client.application/src/org/openbravo/client/application/window/StandardWindowComponent.java
--- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/StandardWindowComponent.java	Tue Aug 07 08:44:00 2018 +0200
+++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/StandardWindowComponent.java	Tue Aug 07 08:44:29 2018 +0200
@@ -23,10 +23,10 @@
 
 import java.util.AbstractMap.SimpleEntry;
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
+import java.util.stream.Stream;
 
 import org.apache.log4j.Logger;
 import org.hibernate.criterion.Order;
@@ -233,25 +233,25 @@
     Map<String, List<GCTab>> gcsByTab = qGCTab.stream() //
         .collect(groupingBy(gcTab -> gcTab.getTab().getId()));
 
-    return window.getADTabList().stream() //
-        .map(tb -> {
-          Optional<GCTab> selectedGC;
-          if (!gcsByTab.containsKey(tb.getId())) {
-            selectedGC = Optional.empty();
-          } else {
-            List<GCTab> candidates = gcsByTab.get(tb.getId());
-            Collections.sort(candidates, (o1, o2) -> {
-              if (o1.getSeqno().compareTo(o2.getSeqno()) != 0) {
-                return o1.getSeqno().compareTo(o2.getSeqno());
-              } else {
-                return o1.getId().compareTo(o2.getId());
-              }
-            });
-            selectedGC = Optional.of(candidates.get(candidates.size() - 1));
-          }
+    return window
+        .getADTabList()
+        .stream()
+        .map(
+            tb -> {
+              Stream<GCTab> candidates = gcsByTab.containsKey(tb.getId()) ? gcsByTab
+                  .get(tb.getId()).stream() : Stream.empty();
 
-          return new SimpleEntry<>(tb.getId(), selectedGC);
-        }) //
+              Optional<GCTab> selectedGC = candidates //
+                  .sorted((o1, o2) -> {
+                    if (o2.getSeqno().compareTo(o1.getSeqno()) != 0) {
+                      return o2.getSeqno().compareTo(o1.getSeqno());
+                    } else {
+                      return o2.getId().compareTo(o1.getId());
+                    }
+                  }) //
+                  .findFirst();
+              return new SimpleEntry<>(tb.getId(), selectedGC);
+            }) //
         .collect(toMap(SimpleEntry::getKey, SimpleEntry::getValue));
   }
 }