mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-10 18:09:38 +07:00
StreamToLoop: support Optional unwrap if possible
This commit is contained in:
@@ -14,7 +14,7 @@ public class Main {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count == 0 ? OptionalDouble.empty() : OptionalDouble.of(sum / count);
|
||||
return count > 0 ? OptionalDouble.of(sum / count) : OptionalDouble.empty();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -13,7 +13,7 @@ public class Main {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count == 0 ? OptionalDouble.empty() : OptionalDouble.of((double) sum / count);
|
||||
return count > 0 ? OptionalDouble.of((double) sum / count) : OptionalDouble.empty();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -13,7 +13,7 @@ public class Main {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count == 0 ? OptionalDouble.empty() : OptionalDouble.of((double) sum / count);
|
||||
return count > 0 ? OptionalDouble.of((double) sum / count) : OptionalDouble.empty();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -14,7 +14,7 @@ public class Main {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
System.out.println(count == 0 ? 0.0 : sum / count);
|
||||
System.out.println(count > 0 ? sum / count : 0.0);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -14,7 +14,7 @@ public class Main {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
System.out.println(count == 0 ? 0.0 : (double) sum / count);
|
||||
System.out.println(count > 0 ? (double) sum / count : 0.0);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -7,21 +7,22 @@ import static java.util.Arrays.asList;
|
||||
|
||||
public class Main {
|
||||
public static Optional<String> test(List<List<String>> list) {
|
||||
String res = null;
|
||||
OUTER:
|
||||
for(int i=0; i<10; i++) {
|
||||
Optional<String> found = Optional.empty();
|
||||
String found = "";
|
||||
OUTER1:
|
||||
for (List<String> x : list) {
|
||||
if (x != null) {
|
||||
for (String s : x) {
|
||||
found = Optional.of(s);
|
||||
found = s;
|
||||
break OUTER1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return found.orElse("");
|
||||
res = found;
|
||||
}
|
||||
return null;
|
||||
return res;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
public class Main {
|
||||
public static boolean test(List<List<String>> list) {
|
||||
for (List<String> strings : list) {
|
||||
if (Objects.nonNull(strings)) {
|
||||
for (String s : strings) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(asList(asList(), asList("a"), asList("b", "c"))));
|
||||
}
|
||||
}
|
||||
@@ -6,14 +6,12 @@ import java.util.stream.*;
|
||||
public class Main {
|
||||
|
||||
private static int test() {
|
||||
OptionalInt found = OptionalInt.empty();
|
||||
for (int x = 0; x < 100; x++) {
|
||||
if (x > 50) {
|
||||
found = OptionalInt.of(x);
|
||||
break;
|
||||
return x;
|
||||
}
|
||||
}
|
||||
return found.orElse(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.*;
|
||||
|
||||
public class Main {
|
||||
|
||||
private static int test() {
|
||||
for (int x = 0; x < 100; x++) {
|
||||
if (x > 50) {
|
||||
return x;
|
||||
}
|
||||
}
|
||||
return Math.abs(-1);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.*;
|
||||
|
||||
public class Main {
|
||||
|
||||
private static int test() {
|
||||
OptionalInt found = OptionalInt.empty();
|
||||
for (int x = 0; x < 100; x++) {
|
||||
if (x > 50) {
|
||||
found = OptionalInt.of(x);
|
||||
break;
|
||||
}
|
||||
}
|
||||
int res = found.orElseGet(() -> Math.abs(-1));
|
||||
return res;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test());
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ public class Main {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count == 0 ? 0.0 : (double) sum / count;
|
||||
return count > 0 ? (double) sum / count : 0.0;
|
||||
};
|
||||
System.out.println(s.getAsDouble());
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class Main {
|
||||
public static String test(List<String> strings) {
|
||||
@@ -16,7 +15,7 @@ public class Main {
|
||||
best = string;
|
||||
}
|
||||
}
|
||||
return (seen ? Optional.of(best) : Optional.<String>empty()).orElse(null);
|
||||
return seen ? best : null;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class Main {
|
||||
public static String test(List<String> strings, Comparator<String> cmp) {
|
||||
@@ -15,7 +14,7 @@ public class Main {
|
||||
best = string;
|
||||
}
|
||||
}
|
||||
return (seen ? Optional.of(best) : Optional.<String>empty()).orElse(null);
|
||||
return seen ? best : null;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class Main {
|
||||
public static String test(List<String> strings, Comparator<CharSequence> comparator) {
|
||||
@@ -16,7 +15,7 @@ public class Main {
|
||||
best = string;
|
||||
}
|
||||
}
|
||||
return (seen ? Optional.of(best) : Optional.<String>empty()).orElse(null);
|
||||
return seen ? best : strings.toString();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.OptionalInt;
|
||||
import java.util.function.IntSupplier;
|
||||
|
||||
public class Main {
|
||||
public static int test(List<String> strings) {
|
||||
public static int test(List<String> strings, IntSupplier supplier) {
|
||||
boolean seen = false;
|
||||
int best = 0;
|
||||
for (String string : strings) {
|
||||
@@ -15,11 +15,11 @@ public class Main {
|
||||
best = length;
|
||||
}
|
||||
}
|
||||
return (seen ? OptionalInt.of(best) : OptionalInt.empty()).orElse(-1);
|
||||
return seen ? best : supplier.getAsInt();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList()));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d")));
|
||||
System.out.println(test(Arrays.asList(), () -> -1));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d"), () -> 2));
|
||||
}
|
||||
}
|
||||
@@ -2,20 +2,17 @@
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class Main {
|
||||
private static String test(List<String> list) {
|
||||
if (list == null) return null;
|
||||
else {
|
||||
Optional<String> found = Optional.empty();
|
||||
for (String str : list) {
|
||||
if (str.contains("x")) {
|
||||
found = Optional.of(str);
|
||||
break;
|
||||
return str;
|
||||
}
|
||||
}
|
||||
return found.orElse(null);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
private static String test(List<String> list) {
|
||||
if (list == null) return null;
|
||||
else {
|
||||
for (String str : list) {
|
||||
if (str.contains("x")) {
|
||||
return str;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList("a", "b", "syz")));
|
||||
}
|
||||
}
|
||||
@@ -7,11 +7,12 @@ import static java.util.Arrays.asList;
|
||||
|
||||
public class Main {
|
||||
public static Optional<String> test(List<List<String>> list) {
|
||||
String res = null;
|
||||
OUTER:
|
||||
for(int i=0; i<10; i++) {
|
||||
return list.stream().filter(x -> x != null).flatMap(x -> x.stream()).fi<caret>ndAny().orElse("");
|
||||
res = list.stream().filter(x -> x != null).flatMap(x -> x.stream()).fi<caret>ndAny().orElse("");
|
||||
}
|
||||
return null;
|
||||
return res;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
public class Main {
|
||||
public static boolean test(List<List<String>> list) {
|
||||
return list.stream().filter(Objects::nonNull).flatMap(List::stream).findA<caret>ny().isPresent();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(asList(asList(), asList("a"), asList("b", "c"))));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.*;
|
||||
|
||||
public class Main {
|
||||
|
||||
private static int test() {
|
||||
return IntStream.range(0, 100).filter(x -> x > 50).findFir<caret>st().orElseGet(() -> Math.abs(-1));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.*;
|
||||
|
||||
public class Main {
|
||||
|
||||
private static int test() {
|
||||
int res = IntStream.range(0, 100).filter(x -> x > 50).findFir<caret>st().orElseGet(() -> Math.abs(-1));
|
||||
return res;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test());
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import java.util.List;
|
||||
|
||||
public class Main {
|
||||
public static String test(List<String> strings, Comparator<CharSequence> comparator) {
|
||||
return strings.stream().m<caret>in(comparator.reversed()).orElse(null);
|
||||
return strings.stream().m<caret>in(comparator.reversed()).orElseGet(strings::toString);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -2,14 +2,15 @@
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.function.IntSupplier;
|
||||
|
||||
public class Main {
|
||||
public static int test(List<String> strings) {
|
||||
return strings.stream().mapToInt(String::length).mi<caret>n().orElse(-1);
|
||||
public static int test(List<String> strings, IntSupplier supplier) {
|
||||
return strings.stream().mapToInt(String::length).mi<caret>n().orElseGet(supplier);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList()));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d")));
|
||||
System.out.println(test(Arrays.asList(), () -> -1));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d"), () -> 2));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
private static String test(List<String> list) {
|
||||
if (list == null) return null;
|
||||
else return list.stream().filter(str -> str.contains("x")).fin<caret>dFirst().orElseGet(() -> null);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList("a", "b", "syz")));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user