ReduceResult (@NotNull PsiElement context) { boolean changed = false; Map<PsiType, List<PsiType>> componentCache = new HashMap<>(); Set<PatternDescription> toAdd = new HashSet<>(); Set<PatternDescription> toRemove = new HashSet<>(); Map<PsiType, Set<PatternDeconstructionDescription>> groupedByType = StreamEx.of(patterns) .select(PatternDeconstructionDescription.class) .groupingBy(t -> t.type(), Collectors.toSet()); for (Map.Entry<PsiType, Set<PatternDeconstructionDescription>> entry : groupedByType.entrySet()) { for (PatternDeconstructionDescription patternDeconstructionDescription : entry.getValue()) { List<PsiType> descriptionTypes = new ArrayList<>(); for (PatternDescription description : patternDeconstructionDescription.list()) { if (description instanceof PatternTypeTestDescription patternTypeTestDescription) { descriptionTypes.add(patternTypeTestDescription.type()); } } PsiType descriptionType = patternDeconstructionDescription.type(); List<PsiType> recordComponentTypes = componentCache.get(descriptionType); if (recordComponentTypes == null) { List<PsiType> recordTypes = getComponentTypes(context, descriptionType); if (recordTypes == null) continue; componentCache.put(descriptionType, recordTypes); recordComponentTypes = recordTypes; } if (recordComponentTypes.size() != descriptionTypes.size()) { continue; } boolean allCovered = true; for (int i = 0; i < recordComponentTypes.size(); i++) { PsiType recordComponentType = recordComponentTypes.get(i); PsiType descriptionComponentType = descriptionTypes.get(i); if (!SwitchBlockHighlightingModel.oneOfUnconditional(descriptionComponentType, recordComponentType)) { allCovered = false; break; } } if (allCovered) { changed = true; toAdd.add(new PatternTypeTestDescription(descriptionType)); toRemove.addAll(entry.getValue()); break; } } } if (!changed) { return new ReduceResult(patterns, changed()); } Set<PatternDescription> result = combineResult(patterns, toRemove, toAdd); return new ReduceResult(result, true); }