package demo; import java.util.*; import java.util.stream.*; import static java.util.stream.Collectors.toList; public class Chains { private static final int LIMIT = 3; static final List NUMS = List.of(1, 2, 3); static final List DOUBLED = NUMS.stream().map(x -> x * 2).toList(); // C_FIELD_INIT final long countAll = NUMS.stream().count(); // C_INSTANCE_FIELD int stride = 2; static { long c = NUMS.stream().count(); // C_STATIC_INIT } { long c = NUMS.stream().filter(x -> x > 1).count(); // C_INSTANCE_INIT } List collectors(List nums) { List a = nums.stream().filter(x -> x % 2 == 0).map(x -> x * LIMIT).collect(Collectors.toList()); // C_NEW List b = nums.stream().map(x -> x + 1).collect(toList()); // C_STATIC_IMPORT int o = Optional.of(1).map(x -> x + 1).filter(x -> x > 0).orElse(0); // C_OPTIONAL a.addAll(b); return a; } long roots(Stream given, List nums, Repository repo) { long n = given.filter(s -> !s.isEmpty()).count(); // C_VARIABLE int sq = IntStream.range(0, 10).map(i -> i * i).sum(); // C_INT Iterator it = nums.stream().map(x -> x + 1).iterator(); // C_ITERATOR long r = repo.findAll().filter(x -> x > 0).count(); // C_FOREIGN_ROOT long u = nums.stream().map(x -> x).mapToEntry(x -> x).count(); // C_UNKNOWN_OP return n + sq + r + u; } static int next(int x) { return x + 1; } int bump(int x) { return x + stride; } void sources(List nums, java.io.BufferedReader reader, int step) { List p = nums.parallelStream().sequential().map(x -> x).parallel().toList(); // C_PARALLEL List g = Stream.generate(Math::random).limit(3).toList(); // C_GENERATE List i1 = Stream.iterate(1, Chains::next).limit(3).toList(); // C_ITERATE_REF List i2 = Stream.iterate(1, x -> x + step).limit(3).toList(); // C_ITERATE_CAPTURE List i3 = Stream.iterate(1, x -> x * 2).limit(3).toList(); // C_ITERATE_PURE List i4 = Stream.iterate(1, x -> x + this.stride).limit(3).toList(); // C_ITERATE_THIS_FIELD List i5 = Stream.iterate(1, x -> this.bump(x)).limit(3).toList(); // C_ITERATE_THIS_CALL List i6 = Stream.iterate(1, x -> { int y = x * 2; return y; }).limit(3).toList(); // C_ITERATE_BLOCK_LOCAL long l1 = reader.lines().count(); // C_LINES long l2 = "a\nb".lines().count(); // C_LINES_LITERAL } void shapes(List nums, List xs, List ys) { List m = nums.stream() .map(x -> x * 2) .filter(y -> y > 0) // C_MULTI .collect(Collectors.toList()); List k = nums.stream().map(x -> { int y = x * 2; return y + LIMIT; // C_IN_LAMBDA }).collect(Collectors.toList()); List inner = xs.stream().map(x -> ys.stream().filter(y -> y < x).count()).toList(); // C_INNER long a1 = xs.stream().count(), a2 = ys.stream().filter(y -> y > 0).count(); // C_TWO nums.stream().forEach(x -> System.out.println(x)); // C_FOREACH var s = nums.stream().map(x -> x); // C_NO_TERMINAL String t = "é😀"; long c = nums.stream().count(); // C_UTF16 } void idioms(List nums, java.io.BufferedReader reader) { nums.stream().map(x -> x + 1).collect(Collectors.toList()).forEach(x -> { // C_COLLECT_FOREACH System.out.println(x); // C_LIST_FOREACH_BODY }); nums.stream().filter(x -> x > 1).toList().forEach(System.out::println); // C_TOLIST_FOREACH nums.stream().collect(Collectors.groupingBy(x -> x % 2)).forEach((k, v) -> System.out.println(k + "=" + v)); // C_GROUPING_FOREACH long rr = reader.lines().collect(Collectors.toList()).stream().count(); // C_REOPEN_READER long rg = Stream.generate(Math::random).limit(2).collect(Collectors.toList()).stream().count(); // C_REOPEN_GENERATE int f = nums.stream().filter(x -> x > 0).findFirst().orElse(0); // C_FIND_FIRST int z = nums.stream().map(x -> x).collect(Collectors.toList()).size(); // C_COLLECT_SIZE int total = 0; if (nums.stream().anyMatch(x -> x > 0)) { // C_IF_HEADER total += 1; // C_IF_BODY } } Stream items; static void foo(Object a, Object b) {} static boolean keep(int x) { return x > 0; } void plans(List nums, List names, List list, int base, int[] arr) { List a = nums.stream().filter(Chains::keep).map(x -> x * 10 + base).sorted().map(String::valueOf).collect(Collectors.toList()); // C_PLAN_COLLECTION int r = IntStream.range(0, 20).filter(i -> i % 3 == 0).map(i -> i * i).sum(); // C_PLAN_RANGE Stream s = names.stream(); long n = s.filter(v -> !v.isEmpty()).count(); // C_PLAN_LOCAL long f = this.items.filter(v -> !v.isEmpty()).count(); // C_PLAN_FIELD long g = items.filter(v -> !v.isEmpty()).count(); // C_PLAN_FIELD_BARE List par = list.parallelStream().map(v -> v + "!").toList(); // C_PLAN_PARALLEL long deep = list.stream().collect(Collectors.toList()).stream().filter(x -> true).count(); // C_PLAN_SKIP long twice = list.stream().map(v -> v).filter(v -> true).map(v -> v).count(); // C_PLAN_SAME_NAME long lazy = Math.max(names.stream().map(v -> list.stream().count()).count(), list.stream().map(v -> v).count()); // C_PLAN_LAMBDA_CALLS boolean same = Objects.equals(names.stream().map(v -> v).toList(), list.stream().map(v -> v).toList()); // C_PLAN_SIBLING long ar = Arrays.stream(arr).boxed().collect(Collectors.toList()).stream().count(); // C_PLAN_ARRAYS_NESTED long so = Stream.of(1, 2, 3).map(v -> v).count(); // C_PLAN_STREAM_OF foo(Optional.of(1), Stream.of(2).map(v -> v).count()); // C_PLAN_OTHER_FAMILY if (names.stream().anyMatch(x -> x.isEmpty()) || list.stream().anyMatch(x -> x.isEmpty())) { // C_PLAN_IF_HEADER base++; } if (names.stream().anyMatch(x -> x.isEmpty())) { base++; } else if (list.stream().anyMatch(x -> x.isEmpty())) { // C_PLAN_ELSE_IF base--; } if (base > 0) { names.stream().forEach(x -> x.length()); } else if (list.stream().anyMatch(x -> x.isEmpty())) { // C_PLAN_ELSE_IF_BODY base--; } if (names.stream().anyMatch(x -> x.isEmpty())) base++; else if (list.stream().noneMatch(x -> x.isEmpty())) base--; // C_PLAN_ELSE_IF_ONE_LINE List out = nums.stream() // C_PLAN_FIRST_LINE .filter(x -> x > 0) .map(x -> x * 2) .sorted() .map(String::valueOf) .collect(Collectors.toList()); } void sourceFamilies(int[] arr, String text, java.nio.file.Path dir, java.util.regex.Pattern pat, Random rnd) throws Exception { long c1 = Arrays.stream(arr).count(); // C_SRC_ARRAYS long c2 = text.chars().count(); // C_SRC_CHARS long c6 = text.lines().count(); // C_SRC_LINES long c3 = java.nio.file.Files.lines(dir).count(); // C_SRC_FILES long c4 = pat.splitAsStream(text).count(); // C_SRC_PATTERN long c5 = rnd.ints(5).count(); // C_SRC_RANDOM } void broken(List nums) { nums.stream().map(x -> x +).toList(); // C_BROKEN long ok = nums.stream().count(); // C_BROKEN_NEXT } void frames(List nums) { Runnable r = new Runnable() { public void run() { long c = nums.stream().count(); // C_ANON } }; class Helper { long go() { return nums.stream().count(); // C_LOCAL } } } static class Inner { > long sortedCount(List items) { return items.stream().sorted().count(); // C_GENERIC } static long staticCount(List items) { return items.stream().count(); // C_STATIC_GENERIC } class Lid { long count(List items) { return items.stream().count(); // C_INNER_GENERIC } } static class Tag { long count(List items) { return items.stream().count(); // C_STATIC_NESTED } } record Pair

(P left) { long count(List

items) { return items.stream().count(); // C_RECORD_GENERIC } } } record Point(int x, int y) { long count(List nums) { return nums.stream().filter(v -> v > x).count(); // C_RECORD } } enum Mode { FAST { long go(List nums) { return nums.stream().count(); // C_ENUM_BODY } }; } interface Repository { Stream findAll(); } } class Secondary { long go(List nums) { return nums.stream().count(); // C_SECONDARY } }