Cleanup (minor optimization; formatting)

This commit is contained in:
Roman Shevchenko
2018-01-08 19:02:04 +01:00
parent 0c60dfb4a6
commit 9ff63280e0
@@ -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<String> getInputValidator(final PsiElement element) {
final LinkedHashMap<RenameInputValidator, ProcessingContext> acceptedValidators = new LinkedHashMap<>();
for(final RenameInputValidator validator: Extensions.getExtensions(RenameInputValidator.EP_NAME)) {
final ProcessingContext context = new ProcessingContext();
public static Condition<String> getInputValidator(PsiElement element) {
List<Pair<RenameInputValidator, ProcessingContext>> 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<String>)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<String, String> getInputErrorValidator(final PsiElement element) {
final LinkedHashMap<RenameInputValidatorEx, ProcessingContext> 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<String, String> getInputErrorValidator(PsiElement element) {
List<RenameInputValidatorEx> 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<String, String>)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);
}
}
}