From 9ff63280e066855c78ce5422370da93636463f29 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Mon, 8 Jan 2018 19:02:04 +0100 Subject: [PATCH] Cleanup (minor optimization; formatting) --- .../rename/RenameInputValidatorRegistry.java | 72 +++++++------------ 1 file changed, 26 insertions(+), 46 deletions(-) diff --git a/platform/lang-api/src/com/intellij/refactoring/rename/RenameInputValidatorRegistry.java b/platform/lang-api/src/com/intellij/refactoring/rename/RenameInputValidatorRegistry.java index 9c22bda69401..47f6c5fea617 100644 --- a/platform/lang-api/src/com/intellij/refactoring/rename/RenameInputValidatorRegistry.java +++ b/platform/lang-api/src/com/intellij/refactoring/rename/RenameInputValidatorRegistry.java @@ -1,70 +1,50 @@ /* - * Copyright 2000-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. */ - package com.intellij.refactoring.rename; import com.intellij.openapi.extensions.Extensions; +import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Condition; +import com.intellij.openapi.util.Pair; import com.intellij.psi.PsiElement; import com.intellij.util.Function; import com.intellij.util.ProcessingContext; -import com.intellij.util.containers.hash.LinkedHashMap; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +import static com.intellij.openapi.util.Pair.pair; + public class RenameInputValidatorRegistry { - private RenameInputValidatorRegistry() { - } + private RenameInputValidatorRegistry() { } @Nullable - public static Condition getInputValidator(final PsiElement element) { - final LinkedHashMap acceptedValidators = new LinkedHashMap<>(); - for(final RenameInputValidator validator: Extensions.getExtensions(RenameInputValidator.EP_NAME)) { - final ProcessingContext context = new ProcessingContext(); + public static Condition getInputValidator(PsiElement element) { + List> validators = new ArrayList<>(); + for (RenameInputValidator validator : Extensions.getExtensions(RenameInputValidator.EP_NAME)) { + ProcessingContext context = new ProcessingContext(); if (validator.getPattern().accepts(element, context)) { - acceptedValidators.put(validator, context); + validators.add(pair(validator, context)); } } - return acceptedValidators.isEmpty() ? null : (Condition)s -> { - for (RenameInputValidator validator : acceptedValidators.keySet()) { - if (!validator.isInputValid(s, element, acceptedValidators.get(validator))) { - return false; - } - } - return true; - }; + + return validators.isEmpty() ? null : newName -> validators.stream().allMatch(p -> p.first.isInputValid(newName, element, p.second)); } @Nullable - public static Function getInputErrorValidator(final PsiElement element) { - final LinkedHashMap acceptedValidators = new LinkedHashMap<>(); - for(final RenameInputValidator validator: Extensions.getExtensions(RenameInputValidator.EP_NAME)) { - final ProcessingContext context = new ProcessingContext(); - if (validator instanceof RenameInputValidatorEx && validator.getPattern().accepts(element, context)) { - acceptedValidators.put((RenameInputValidatorEx)validator, context); + public static Function getInputErrorValidator(PsiElement element) { + List validators = new ArrayList<>(); + for (RenameInputValidator validator : Extensions.getExtensions(RenameInputValidator.EP_NAME)) { + if (validator instanceof RenameInputValidatorEx && validator.getPattern().accepts(element, new ProcessingContext())) { + validators.add((RenameInputValidatorEx)validator); } } - return acceptedValidators.isEmpty() ? null : (Function)newName -> { - for (RenameInputValidatorEx validator : acceptedValidators.keySet()) { - final String message = validator.getErrorMessage(newName, element.getProject()); - if (message != null) { - return message; - } - } - return null; - }; + if (validators.isEmpty()) return null; + Project project = element.getProject(); + return newName -> validators.stream().map(v -> v.getErrorMessage(newName, project)).filter(Objects::nonNull).findFirst().orElse(null); } -} +} \ No newline at end of file