From 9bca4c393c1bed9876d9fbb723db6a6d10354e1b Mon Sep 17 00:00:00 2001 From: Bruce Collie Date: Wed, 1 Nov 2023 13:59:53 +0000 Subject: [PATCH] Simplify stream call chains --- .../org/kframework/backend/kore/ModuleToKORE.java | 6 ++---- .../java/org/kframework/compile/AddParentCells.java | 5 +---- .../org/kframework/compile/AddSortInjections.java | 4 +--- .../compile/ConvertDataStructureToLookup.java | 1 - .../java/org/kframework/compile/ExpandMacros.java | 2 +- .../org/kframework/compile/checks/CheckAtt.java | 13 +++---------- .../org/kframework/compile/checks/CheckBracket.java | 3 +-- .../org/kframework/compile/checks/CheckKLabels.java | 6 ++---- .../kframework/compile/checks/CheckListDecl.java | 2 +- .../main/java/org/kframework/kompile/Kompile.java | 1 - .../org/kframework/lsp/TextDocumentSyncHandler.java | 8 ++------ .../java/org/kframework/parser/ParserUtils.java | 2 +- .../parser/inner/RuleGrammarGenerator.java | 8 ++++---- .../parser/inner/disambiguation/AddEmptyLists.java | 2 +- .../parser/inner/disambiguation/TypeInferencer.java | 2 +- .../org/kframework/parser/inner/kernel/Scanner.java | 2 +- .../main/java/org/kframework/unparser/KPrint.java | 2 +- 17 files changed, 23 insertions(+), 46 deletions(-) diff --git a/kernel/src/main/java/org/kframework/backend/kore/ModuleToKORE.java b/kernel/src/main/java/org/kframework/backend/kore/ModuleToKORE.java index 8996a6e19df..18dfb377c0b 100644 --- a/kernel/src/main/java/org/kframework/backend/kore/ModuleToKORE.java +++ b/kernel/src/main/java/org/kframework/backend/kore/ModuleToKORE.java @@ -2034,9 +2034,7 @@ private void convert( for (Tuple2, ?> attribute : // Sort to stabilize error messages - stream(att.att()) - .sorted(Comparator.comparing(Tuple2::toString)) - .collect(Collectors.toList())) { + stream(att.att()).sorted(Comparator.comparing(Tuple2::toString)).toList()) { Att.Key key = attribute._1._1; String strKey = key.key(); String clsName = attribute._1._2; @@ -2095,7 +2093,7 @@ private void convertStringVarList( else throw KEMException.criticalError("No free variable found for " + s, location); }) - .collect(Collectors.toList()); + .toList(); String conn = ""; for (KVariable var : variables) { sb.append(conn); diff --git a/kernel/src/main/java/org/kframework/compile/AddParentCells.java b/kernel/src/main/java/org/kframework/compile/AddParentCells.java index 0f7523cc312..5c53bdeaefa 100644 --- a/kernel/src/main/java/org/kframework/compile/AddParentCells.java +++ b/kernel/src/main/java/org/kframework/compile/AddParentCells.java @@ -60,10 +60,7 @@ protected List makeParents(KLabel parent, boolean ellipses, List allC List children = allChildren.stream().filter(t -> !(t instanceof KRewrite)).collect(Collectors.toList()); List rewrites = - allChildren.stream() - .filter(t -> t instanceof KRewrite) - .map(t -> (KRewrite) t) - .collect(Collectors.toList()); + allChildren.stream().filter(t -> t instanceof KRewrite).map(t -> (KRewrite) t).toList(); // see if all children can fit together Set usedCells = Sets.newHashSet(); diff --git a/kernel/src/main/java/org/kframework/compile/AddSortInjections.java b/kernel/src/main/java/org/kframework/compile/AddSortInjections.java index 013cf1f4bb6..ae2db921a28 100644 --- a/kernel/src/main/java/org/kframework/compile/AddSortInjections.java +++ b/kernel/src/main/java/org/kframework/compile/AddSortInjections.java @@ -503,9 +503,7 @@ private static Sort lub( assert !entries.isEmpty(); entries = new HashSet<>(entries); Collection filteredEntries = - entries.stream() - .filter(s -> s != null && !s.name().equals(SORTPARAM_NAME)) - .collect(Collectors.toList()); + entries.stream().filter(s -> s != null && !s.name().equals(SORTPARAM_NAME)).toList(); if (filteredEntries.isEmpty()) { // if all sorts are parameters, take the first return entries.iterator().next(); } diff --git a/kernel/src/main/java/org/kframework/compile/ConvertDataStructureToLookup.java b/kernel/src/main/java/org/kframework/compile/ConvertDataStructureToLookup.java index 905d7fdbb5b..d0a40a55a5b 100644 --- a/kernel/src/main/java/org/kframework/compile/ConvertDataStructureToLookup.java +++ b/kernel/src/main/java/org/kframework/compile/ConvertDataStructureToLookup.java @@ -121,7 +121,6 @@ public static Set filteredMapConstructors(Module m) { return stream(m.productions()) .filter(p -> p.att().contains(Att.ASSOC()) && p.att().contains(Att.FILTER_ELEMENT())) .map(p -> p.klabel().get()) - .distinct() .collect(Collectors.toSet()); } diff --git a/kernel/src/main/java/org/kframework/compile/ExpandMacros.java b/kernel/src/main/java/org/kframework/compile/ExpandMacros.java index e27a885686c..9be9df41bad 100644 --- a/kernel/src/main/java/org/kframework/compile/ExpandMacros.java +++ b/kernel/src/main/java/org/kframework/compile/ExpandMacros.java @@ -117,7 +117,7 @@ public ExpandMacros( stream(mod.rules()) .filter(r -> isMacro(r.att(), reverse)) .sorted(Comparator.comparingInt(r -> ModuleToKORE.getPriority(r.att()))) - .collect(Collectors.toList()); + .toList(); macros = allMacros.stream() .filter(r -> getLeft(r, reverse) instanceof KApply) diff --git a/kernel/src/main/java/org/kframework/compile/checks/CheckAtt.java b/kernel/src/main/java/org/kframework/compile/checks/CheckAtt.java index 92cb8ec434f..129f90f1591 100644 --- a/kernel/src/main/java/org/kframework/compile/checks/CheckAtt.java +++ b/kernel/src/main/java/org/kframework/compile/checks/CheckAtt.java @@ -46,10 +46,7 @@ public void checkUnrecognizedModuleAtts() { "Unrecognized attributes on module " + m.name() + ": " - + stream(m.att().unrecognizedKeys()) - .map(Key::toString) - .sorted() - .collect(Collectors.toList()) + + stream(m.att().unrecognizedKeys()).map(Key::toString).sorted().toList() + "\nHint: User-defined groups can be added with the group(_) attribute.")); } } @@ -84,10 +81,7 @@ private void checkUnrecognizedAtts(Sentence sentence) { errors.add( KEMException.compilerError( "Unrecognized attributes: " - + stream(sentence.att().unrecognizedKeys()) - .map(Key::toString) - .sorted() - .collect(Collectors.toList()) + + stream(sentence.att().unrecognizedKeys()).map(Key::toString).sorted().toList() + "\nHint: User-defined groups can be added with the group(_) attribute.", sentence)); } @@ -99,8 +93,7 @@ private void checkRestrictedAtts(Sentence sentence) { Set keys = stream(att.att().keySet()).map(k -> k._1()).collect(Collectors.toSet()); keys.removeIf(k -> k.allowedSentences().exists(c -> c.isAssignableFrom(cls))); if (!keys.isEmpty()) { - List sortedKeys = - keys.stream().map(k -> k.toString()).sorted().collect(Collectors.toList()); + List sortedKeys = keys.stream().map(k -> k.toString()).sorted().toList(); errors.add( KEMException.compilerError( cls.getSimpleName() diff --git a/kernel/src/main/java/org/kframework/compile/checks/CheckBracket.java b/kernel/src/main/java/org/kframework/compile/checks/CheckBracket.java index c2c0864ee15..614b43b4899 100644 --- a/kernel/src/main/java/org/kframework/compile/checks/CheckBracket.java +++ b/kernel/src/main/java/org/kframework/compile/checks/CheckBracket.java @@ -2,7 +2,6 @@ package org.kframework.compile.checks; import java.util.List; -import java.util.stream.Collectors; import org.kframework.attributes.Att; import org.kframework.kil.Module; import org.kframework.kil.ModuleItem; @@ -27,7 +26,7 @@ private static void check(ModuleItem i) { List nts = p.getItems().stream() .filter(x -> x instanceof NonTerminal) - .collect(Collectors.toList()); + .toList(); if (nts.size() != 1 || !((NonTerminal) nts.get(0)).getSort().equals(s.getDeclaredSort().getSort())) throw KEMException.outerParserError( diff --git a/kernel/src/main/java/org/kframework/compile/checks/CheckKLabels.java b/kernel/src/main/java/org/kframework/compile/checks/CheckKLabels.java index dbc7344ddd2..2387227c6e0 100644 --- a/kernel/src/main/java/org/kframework/compile/checks/CheckKLabels.java +++ b/kernel/src/main/java/org/kframework/compile/checks/CheckKLabels.java @@ -167,16 +167,14 @@ public void check(Module mainMod) { || s.isEmpty() || (prod.att().contains(Att.CELL()) && stream(prod.nonterminals()) - .filter( + .anyMatch( nt -> klabels .get(symbol) .sortAttributesFor() .get(nt.sort().head()) .getOrElse(() -> Att.empty()) - .contains(Att.CELL_COLLECTION())) - .findAny() - .isPresent())) { + .contains(Att.CELL_COLLECTION())))) { continue; } if (canonicalPath == null || !s.get().source().contains(canonicalPath)) { diff --git a/kernel/src/main/java/org/kframework/compile/checks/CheckListDecl.java b/kernel/src/main/java/org/kframework/compile/checks/CheckListDecl.java index 49e1127e73f..2dad9460702 100644 --- a/kernel/src/main/java/org/kframework/compile/checks/CheckListDecl.java +++ b/kernel/src/main/java/org/kframework/compile/checks/CheckListDecl.java @@ -21,7 +21,7 @@ public class CheckListDecl { public static void check(Module m) { - m.getItems().stream().forEach(CheckListDecl::check); // i -> check(i) + m.getItems().forEach(CheckListDecl::check); // i -> check(i) } private static final Set BASE_SORTS = diff --git a/kernel/src/main/java/org/kframework/kompile/Kompile.java b/kernel/src/main/java/org/kframework/kompile/Kompile.java index f75ab4047c1..a9d67ab20da 100644 --- a/kernel/src/main/java/org/kframework/kompile/Kompile.java +++ b/kernel/src/main/java/org/kframework/kompile/Kompile.java @@ -736,7 +736,6 @@ private void checkIsSortPredicates(scala.collection.Set modules) { m.productionsForSort() .getOrElse(Sorts.Bool().head(), Set$.MODULE$::empty))) .collect(Collectors.toSet()) - .stream() .forEach( prod -> { Seq items = prod.items(); diff --git a/kernel/src/main/java/org/kframework/lsp/TextDocumentSyncHandler.java b/kernel/src/main/java/org/kframework/lsp/TextDocumentSyncHandler.java index 92788117e1a..76782fc4d36 100644 --- a/kernel/src/main/java/org/kframework/lsp/TextDocumentSyncHandler.java +++ b/kernel/src/main/java/org/kframework/lsp/TextDocumentSyncHandler.java @@ -523,9 +523,7 @@ public CompletableFuture> references(ReferenceParams pa if (isPositionOverLocation(pos, nameLoc)) { List allDi = - files.values().stream() - .flatMap(doc -> doc.dis.stream()) - .collect(Collectors.toList()); + files.values().stream().flatMap(doc -> doc.dis.stream()).toList(); allDi.stream() .filter(ddi -> ddi instanceof Module) .map(ddi -> ((Module) ddi)) @@ -787,9 +785,7 @@ public CompletableFuture> selectionRange(SelectionRangePara + " #poss: " + poss + " rezDepth: " - + lloc.stream() - .map(TextDocumentSyncHandler::getSelectionRangeDepth) - .collect(Collectors.toList())); + + lloc.stream().map(TextDocumentSyncHandler::getSelectionRangeDepth).toList()); return lloc; }); diff --git a/kernel/src/main/java/org/kframework/parser/ParserUtils.java b/kernel/src/main/java/org/kframework/parser/ParserUtils.java index 6e0ed4fc9b9..361a725c79e 100644 --- a/kernel/src/main/java/org/kframework/parser/ParserUtils.java +++ b/kernel/src/main/java/org/kframework/parser/ParserUtils.java @@ -254,7 +254,7 @@ public Set loadModules( groupedModules.entrySet().stream() .filter(e -> e.getValue().size() > 1) .map(Map.Entry::getKey) - .collect(Collectors.toList()); + .toList(); int errors = 0; for (String moduleName : duplicateModules) { diff --git a/kernel/src/main/java/org/kframework/parser/inner/RuleGrammarGenerator.java b/kernel/src/main/java/org/kframework/parser/inner/RuleGrammarGenerator.java index 55dfe71747a..c36f93e4598 100644 --- a/kernel/src/main/java/org/kframework/parser/inner/RuleGrammarGenerator.java +++ b/kernel/src/main/java/org/kframework/parser/inner/RuleGrammarGenerator.java @@ -375,7 +375,7 @@ public static Tuple3 getCombinedGrammarImpl( List allSorts = stream(mod.allSorts()) .filter(s -> (!isParserSort(s) || s.equals(Sorts.KItem()) || s.equals(Sorts.K()))) - .collect(Collectors.toList()); + .toList(); for (SortHead sh : mutable(mod.definedInstantiations()).keySet()) { for (Sort s : mutable(mod.definedInstantiations().apply(sh))) { // syntax MInt{K} ::= MInt{6} @@ -583,7 +583,7 @@ public static Tuple3 getCombinedGrammarImpl( .collect(Collectors.toSet()); } - disambProds = parseProds.stream().collect(Collectors.toSet()); + disambProds = new HashSet<>(parseProds); if (mod.importedModuleNames().contains(PROGRAM_LISTS)) { Set prods3 = new HashSet<>(); // if no start symbol has been defined in the configuration, then use K @@ -594,7 +594,7 @@ public static Tuple3 getCombinedGrammarImpl( } } // for each triple, generate a new pattern which works better for parsing lists in programs. - prods3.addAll(parseProds.stream().collect(Collectors.toSet())); + prods3.addAll(new HashSet<>(parseProds)); Set res = new HashSet<>(); for (UserList ul : UserList.getLists(prods3)) { Production prod1, prod2, prod3 = null, prod4 = null, prod5 = null; @@ -714,7 +714,7 @@ public static Tuple3 getCombinedGrammarImpl( stream(mod.importedModules()) .filter(m -> m.att().contains(Att.NOT_LR1())) .map(Module::name) - .collect(Collectors.toList()); + .toList(); if (!notLrModules.isEmpty()) { att = att.add(Att.NOT_LR1_MODULES(), notLrModules.toString()); } diff --git a/kernel/src/main/java/org/kframework/parser/inner/disambiguation/AddEmptyLists.java b/kernel/src/main/java/org/kframework/parser/inner/disambiguation/AddEmptyLists.java index 003d42404d7..885565d9c97 100644 --- a/kernel/src/main/java/org/kframework/parser/inner/disambiguation/AddEmptyLists.java +++ b/kernel/src/main/java/org/kframework/parser/inner/disambiguation/AddEmptyLists.java @@ -100,7 +100,7 @@ public Tuple2, Term>, Set> apply(TermCons orig); java.util.Set warnings = new HashSet<>(); - List reversed = tc.items().stream().collect(Collectors.toList()); + List reversed = new ArrayList<>(tc.items()); Collections.reverse( reversed); // TermCons with PStack requires the elements to be in the reverse order Iterator items = reversed.iterator(); diff --git a/kernel/src/main/java/org/kframework/parser/inner/disambiguation/TypeInferencer.java b/kernel/src/main/java/org/kframework/parser/inner/disambiguation/TypeInferencer.java index 21d22822985..d298381d849 100644 --- a/kernel/src/main/java/org/kframework/parser/inner/disambiguation/TypeInferencer.java +++ b/kernel/src/main/java/org/kframework/parser/inner/disambiguation/TypeInferencer.java @@ -176,7 +176,7 @@ private void makeSubsorts(Module mod, String name, POSet relations) { for (Tuple2> relation : stream(relations.relations()) .sorted(Comparator.comparing(t -> -ordinals.getOrDefault(t._1().head(), 0))) - .collect(Collectors.toList())) { + .toList()) { if (!isRealSort(relation._1().head())) { continue; } diff --git a/kernel/src/main/java/org/kframework/parser/inner/kernel/Scanner.java b/kernel/src/main/java/org/kframework/parser/inner/kernel/Scanner.java index c9ad59fb189..9d4022e9c78 100644 --- a/kernel/src/main/java/org/kframework/parser/inner/kernel/Scanner.java +++ b/kernel/src/main/java/org/kframework/parser/inner/kernel/Scanner.java @@ -153,7 +153,7 @@ public void appendScanner( List ordered = tokens.keySet().stream() .sorted((t1, t2) -> tokens.get(t2)._2() - tokens.get(t1)._2()) - .collect(Collectors.toList()); + .toList(); for (TerminalLike key : ordered) { if (key instanceof Terminal t) { flex.append(StringUtil.enquoteCString(t.value())); diff --git a/kernel/src/main/java/org/kframework/unparser/KPrint.java b/kernel/src/main/java/org/kframework/unparser/KPrint.java index 7c256b17201..b681321f98d 100644 --- a/kernel/src/main/java/org/kframework/unparser/KPrint.java +++ b/kernel/src/main/java/org/kframework/unparser/KPrint.java @@ -312,7 +312,7 @@ public Optional filterEquality(K term, Multiset vars, Module mod) return Optional.of(term); } Set leftVars = vars(kapp.items().get(0)); - if (leftVars.stream().filter(v -> !v.att().contains(Att.ANONYMOUS())).findAny().isPresent()) { + if (leftVars.stream().anyMatch(v -> !v.att().contains(Att.ANONYMOUS()))) { return Optional.of(term); } for (KVariable var : leftVars) {