From 08dff33002728b19a453b0fcaf0271d23b8e0590 Mon Sep 17 00:00:00 2001 From: Stypox Date: Thu, 17 Mar 2022 16:00:50 +0100 Subject: [PATCH] Use Java 8 streams in NewPipe class --- .../org/schabi/newpipe/extractor/NewPipe.java | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/NewPipe.java b/extractor/src/main/java/org/schabi/newpipe/extractor/NewPipe.java index 767a2e64..e4d48070 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/NewPipe.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/NewPipe.java @@ -71,22 +71,20 @@ public final class NewPipe { return ServiceList.all(); } - public static StreamingService getService(int serviceId) throws ExtractionException { - for (StreamingService service : ServiceList.all()) { - if (service.getServiceId() == serviceId) { - return service; - } - } - throw new ExtractionException("There's no service with the id = \"" + serviceId + "\""); + public static StreamingService getService(final int serviceId) throws ExtractionException { + return ServiceList.all().stream() + .filter(service -> service.getServiceId() == serviceId) + .findFirst() + .orElseThrow(() -> new ExtractionException( + "There's no service with the id = \"" + serviceId + "\"")); } - public static StreamingService getService(String serviceName) throws ExtractionException { - for (StreamingService service : ServiceList.all()) { - if (service.getServiceInfo().getName().equals(serviceName)) { - return service; - } - } - throw new ExtractionException("There's no service with the name = \"" + serviceName + "\""); + public static StreamingService getService(final String serviceName) throws ExtractionException { + return ServiceList.all().stream() + .filter(service -> service.getServiceInfo().getName().equals(serviceName)) + .findFirst() + .orElseThrow(() -> new ExtractionException( + "There's no service with the name = \"" + serviceName + "\"")); } public static StreamingService getServiceByUrl(final String url) throws ExtractionException {