StreamToLoopInspection: allow custom sources when option is set

This commit is contained in:
Tagir Valeev
2017-01-24 15:15:44 +07:00
parent 28f8e7760b
commit 1820f2db99
10 changed files with 181 additions and 25 deletions
@@ -0,0 +1,24 @@
// "Replace Stream API chain with loop" "true"
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Test {
Stream<String> names() {
return Stream.of("foo", "bar");
}
public static void main(String[] args) {
List<String> list = new ArrayList<>();
for (Iterator<String> it = new Test().names().iterator(); it.hasNext(); ) {
String name = it.next();
String n = name.trim();
if (!n.isEmpty()) {
list.add(n);
}
}
}
}
@@ -0,0 +1,25 @@
// "Replace Stream API chain with loop" "true"
import java.util.PrimitiveIterator;
import java.util.Random;
import java.util.stream.IntStream;
public class Test {
static void test() {
for (int n = 1; n < 100; n++) {
if (n > 20) {
Integer integer = n;
for (PrimitiveIterator.OfDouble it = new Random(integer).doubles(integer).iterator(); it.hasNext(); ) {
double v = it.next();
if (v < 0.01) {
System.out.println(v);
}
}
}
}
}
public static void main(String[] args) {
test();
}
}
@@ -0,0 +1,16 @@
// "Replace Stream API chain with loop" "true"
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Test {
Stream<String> names() {
return Stream.of("foo", "bar");
}
public static void main(String[] args) {
List<String> list = new Test().names().map(String::trim).filter(n -> !n.isEmpty())
.<caret>collect(Collectors.toList());
}
}
@@ -0,0 +1,19 @@
// "Replace Stream API chain with loop" "true"
import java.util.Random;
import java.util.stream.IntStream;
public class Test {
static void test() {
IntStream.range(1, 100)
.filter(n -> n > 20)
.boxed()
.flatMapToDouble(n -> new Random(n).doubles(n))
.filter(n -> n < 0.01)
.fo<caret>rEach(System.out::println);
}
public static void main(String[] args) {
test();
}
}