Replace instances of FluentIterable with Java 8 Streams
authorAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Fri, 23 Oct 2015 23:06:12 +0000 (19:06 -0400)
committerAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Fri, 30 Oct 2015 00:24:49 +0000 (20:24 -0400)
Guava's FluentIterable was already used in some places to make use of
functional idioms. Now that we have moved to Java 8 we can replace
those with base Java Streams.

Change-Id: I41bce430327020837b6fb3cc053405ab231801ab
Signed-off-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
Reviewed-on: https://git.eclipse.org/r/58882
Reviewed-by: Hudson CI
Reviewed-by: Marc-Andre Laperle <marc-andre.laperle@ericsson.com>
Tested-by: Marc-Andre Laperle <marc-andre.laperle@ericsson.com>
analysis/org.eclipse.tracecompass.analysis.os.linux.core/src/org/eclipse/tracecompass/analysis/os/linux/core/latency/LatencyAnalysis.java
common/org.eclipse.tracecompass.common.core.tests/src/org/eclipse/tracecompass/common/core/tests/collect/BufferedBlockingQueueTest.java
lttng/org.eclipse.tracecompass.lttng2.ust.core/src/org/eclipse/tracecompass/lttng2/ust/core/analysis/debuginfo/UstDebugInfoAnalysisModule.java
tmf/org.eclipse.tracecompass.tmf.remote.core/src/org/eclipse/tracecompass/tmf/remote/core/proxy/TmfRemoteConnectionFactory.java
tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/views/histogram/HistogramDataModel.java

index 31657bd96fdd18bcb6eea4e1fa51cdd53c0bcd0a..457c43402f3a1adf31f321c912ba95ce7666f542 100644 (file)
@@ -17,20 +17,19 @@ import java.util.Collection;
 import java.util.Comparator;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collectors;
 
 import org.eclipse.jdt.annotation.Nullable;
 import org.eclipse.tracecompass.analysis.os.linux.core.kernelanalysis.KernelTidAspect;
 import org.eclipse.tracecompass.analysis.os.linux.core.trace.IKernelAnalysisEventLayout;
 import org.eclipse.tracecompass.analysis.os.linux.core.trace.IKernelTrace;
 import org.eclipse.tracecompass.analysis.timing.core.segmentstore.AbstractSegmentStoreAnalysisModule;
-import org.eclipse.tracecompass.common.core.NonNullUtils;
 import org.eclipse.tracecompass.segmentstore.core.ISegment;
 import org.eclipse.tracecompass.segmentstore.core.ISegmentStore;
 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
 import org.eclipse.tracecompass.tmf.core.segment.ISegmentAspect;
 
-import com.google.common.base.Function;
-import com.google.common.collect.FluentIterable;
 import com.google.common.collect.ImmutableList;
 
 /**
@@ -108,14 +107,12 @@ public class LatencyAnalysis extends AbstractSegmentStoreAnalysisModule {
                 // String syscallName = fLayout.getSyscallNameFromEvent(event);
                 long startTime = event.getTimestamp().getValue();
                 String syscallName = eventName.substring(layout.eventSyscallEntryPrefix().length());
-                FluentIterable<String> argNames = FluentIterable.from(event.getContent().getFieldNames());
-                Map<String, String> args = argNames.toMap(new Function<String, String>() {
-                    @Override
-                    public String apply(@Nullable String input) {
-                        return checkNotNull(event.getContent().getField(input).getValue().toString());
-                    }
-                });
-                SystemCall.InitialInfo newSysCall = new SystemCall.InitialInfo(startTime, NonNullUtils.checkNotNull(syscallName), NonNullUtils.checkNotNull(args));
+
+                Map<String, String> args = event.getContent().getFieldNames().stream()
+                    .collect(Collectors.toMap(Function.identity(),
+                            input -> checkNotNull(event.getContent().getField(input).getValue().toString())));
+
+                SystemCall.InitialInfo newSysCall = new SystemCall.InitialInfo(startTime, checkNotNull(syscallName), checkNotNull(args));
                 fOngoingSystemCalls.put(tid, newSysCall);
 
             } else if (eventName.startsWith(layout.eventSyscallExitPrefix())) {
index 80f396cb991ebab68854a76dbf82be39e21cad12..f027fa7e1a467c0e7007ff8d3fb3155781dc71c5 100644 (file)
@@ -30,6 +30,7 @@ import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
 
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.tracecompass.common.core.NonNullUtils;
@@ -40,8 +41,6 @@ import org.junit.Test;
 import org.junit.rules.TestRule;
 import org.junit.rules.Timeout;
 
-import com.google.common.base.Functions;
-import com.google.common.collect.FluentIterable;
 import com.google.common.collect.HashMultiset;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Iterators;
@@ -353,11 +352,11 @@ public class BufferedBlockingQueueTest {
 
         /*
          * Convert the test's testBuffer into an array of String, one for each
-         * character. There are probably simpler ways of doing this, but it
-         * would not look as impressive.
+         * character.
          */
-        FluentIterable<Character> fi = FluentIterable.from(Chars.asList(testString.toCharArray()));
-        List<String> strings = fi.transform(Functions.toStringFunction()).toList();
+        List<String> strings = Chars.asList(testString.toCharArray()).stream()
+            .map(Object::toString)
+            .collect(Collectors.toList());
 
         Iterable<Iterable<String>> results =
                 runConcurrencyTest(queue, strings, poisonPill, 1, 1, 1);
index 09b3a6251aec23217ed19919ac113aa3b81471e8..8cdac0b4058b23464c7e6640c07c9c5d881cee5f 100644 (file)
@@ -15,8 +15,10 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 import java.util.NavigableSet;
+import java.util.Optional;
 import java.util.Set;
 import java.util.TreeSet;
+import java.util.stream.Collectors;
 
 import org.eclipse.jdt.annotation.Nullable;
 import org.eclipse.tracecompass.internal.lttng2.ust.core.analysis.debuginfo.UstDebugInfoBinaryFile;
@@ -36,12 +38,7 @@ import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModul
 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
 import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfUtils;
 
-import com.google.common.base.Function;
-import com.google.common.base.Optional;
-import com.google.common.base.Predicate;
-import com.google.common.collect.FluentIterable;
 import com.google.common.collect.ImmutableList;
-import com.google.common.collect.Ordering;
 
 /**
  * Analysis to provide TMF Callsite information by mapping IP (instruction
@@ -185,14 +182,13 @@ public class UstDebugInfoAnalysisModule extends TmfStateSystemAnalysisModule {
         List<Integer> possibleBaddrQuarks = ss.getQuarks(String.valueOf(vpid), "*"); //$NON-NLS-1$
 
         /* Get the most probable base address from all the known ones */
-        NavigableSet<Long> possibleBaddrs = FluentIterable.from(possibleBaddrQuarks)
-                .transform(new Function<Integer, Long>(){
-                    @Override
-                    public Long apply(@Nullable Integer quark) {
-                        String baddrStr = ss.getAttributeName(checkNotNull(quark).intValue());
-                        return checkNotNull(Long.valueOf(baddrStr));
-                    }
-                }).toSortedSet(Ordering.natural());
+        NavigableSet<Long> possibleBaddrs = possibleBaddrQuarks.stream()
+            .map(quark -> {
+                String baddrStr = ss.getAttributeName(checkNotNull(quark).intValue());
+                return checkNotNull(Long.valueOf(baddrStr));
+            })
+            .collect(Collectors.toCollection(TreeSet::new));
+
         final Long potentialBaddr = possibleBaddrs.floor(ip);
 
         /* Make sure the 'ip' fits in the expected memory range */
@@ -215,14 +211,13 @@ public class UstDebugInfoAnalysisModule extends TmfStateSystemAnalysisModule {
              * library was loaded there at that time.
              */
             List<Integer> buildIds = ss.getSubAttributes(baddrQuark, false);
-            Optional<Integer> potentialBuildIdQuark = FluentIterable.from(buildIds).firstMatch(new Predicate<Integer>() {
-                @Override
-                public boolean apply(@Nullable Integer input) {
-                    int quark = checkNotNull(input).intValue();
+            Optional<Integer> potentialBuildIdQuark = buildIds.stream()
+                .filter(id -> {
+                    int quark = checkNotNull(id).intValue();
                     ITmfStateValue value = fullState.get(quark).getStateValue();
                     return (!value.isNull());
-                }
-            });
+                })
+                .findFirst();
 
             if (!potentialBuildIdQuark.isPresent()) {
                 /* We didn't have the information after all. */
index d3dfa907a77da39579be7196f13a1ac76c4242a5..778791533fde1d9df156b7639baa7f15bb073c72 100644 (file)
@@ -28,10 +28,6 @@ import org.eclipse.remote.core.exception.RemoteConnectionException;
 import org.eclipse.tracecompass.internal.tmf.remote.core.Activator;
 import org.eclipse.tracecompass.internal.tmf.remote.core.messages.Messages;
 
-import com.google.common.base.Optional;
-import com.google.common.base.Predicate;
-import com.google.common.collect.FluentIterable;
-
 /**
  * Factory for creation of remote connections programmatically.
  *
@@ -224,14 +220,13 @@ public class TmfRemoteConnectionFactory {
         if (manager == null) {
             return null;
         }
-        FluentIterable<IRemoteConnection> connections = FluentIterable.from(manager.getAllRemoteConnections());
-        Optional<IRemoteConnection> ret = connections.firstMatch(new Predicate<IRemoteConnection>() {
-            @Override
-            public boolean apply(@Nullable IRemoteConnection input) {
-                return ((input != null) && input.getConnectionType().getId().equals(remoteServicesId.toString()) && input.getName().equals(name.toString()));
-            }
-        });
-        return ret.orNull();
+        return manager.getAllRemoteConnections().stream()
+            .filter(connection ->
+                (connection != null) &&
+                connection.getConnectionType().getId().equals(remoteServicesId.toString()) &&
+                connection.getName().equals(name.toString()))
+            .findFirst()
+            .orElse(null);
     }
 
     /**
index 95d66bf069a940d4f30fe535090b9092d62afbc7..ea4f1356d8e76a7d7c16813d1a8cce7f39634414 100644 (file)
@@ -29,9 +29,6 @@ import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
 
-import com.google.common.base.Function;
-import com.google.common.collect.FluentIterable;
-
 /**
  * Histogram-independent data model.
  *
@@ -283,14 +280,9 @@ public class HistogramDataModel implements IHistogramDataModel {
      * @return an array of trace names
      */
     public String[] getTraceNames() {
-        FluentIterable<ITmfTrace> traces = FluentIterable.from(TmfTraceManager.getTraceSet(fTrace));
-        FluentIterable<String> traceNames = traces.transform(new Function<ITmfTrace, String>() {
-            @Override
-            public String apply(ITmfTrace input) {
-                return input.getName();
-            }
-        });
-        return traceNames.toArray(String.class);
+        return TmfTraceManager.getTraceSet(fTrace).stream()
+            .map(trace -> trace.getName())
+            .toArray(String[]::new);
     }
 
     /**
This page took 0.029929 seconds and 5 git commands to generate.