Bug 579791 - Simplify DiagnosticMessageExtractor and PreSetValidation
Change-Id: I9924bd4df98ff2de5d2558efd4014d6b417859c9
Signed-off-by: Hannes Wellmann <wellmann.hannes1@gmx.net>
diff --git a/bundles/org.eclipse.emf.ecp.view.model/src/org/eclipse/emf/ecp/view/spi/model/DiagnosticMessageExtractor.java b/bundles/org.eclipse.emf.ecp.view.model/src/org/eclipse/emf/ecp/view/spi/model/DiagnosticMessageExtractor.java
index 9eb4f18..969ce3b 100644
--- a/bundles/org.eclipse.emf.ecp.view.model/src/org/eclipse/emf/ecp/view/spi/model/DiagnosticMessageExtractor.java
+++ b/bundles/org.eclipse.emf.ecp.view.model/src/org/eclipse/emf/ecp/view/spi/model/DiagnosticMessageExtractor.java
@@ -15,9 +15,9 @@
import java.util.ArrayList;
import java.util.Collection;
-import java.util.Collections;
import java.util.Comparator;
import java.util.List;
+import java.util.StringJoiner;
import org.eclipse.emf.common.util.Diagnostic;
@@ -45,19 +45,20 @@
if (diagnostic.getSeverity() == Diagnostic.OK) {
return ""; //$NON-NLS-1$
}
- if (diagnostic.getChildren() != null && diagnostic.getChildren().size() == 0) {
+ if (diagnostic.getChildren() != null && diagnostic.getChildren().isEmpty()) {
return diagnostic.getMessage();
}
- final StringBuilder sb = new StringBuilder();
+ final StringJoiner sb = new StringJoiner("\n"); //$NON-NLS-1$
for (final Diagnostic childDiagnostic : diagnostic.getChildren()) {
- if (sb.length() > 0) {
- sb.append("\n"); //$NON-NLS-1$
- }
- sb.append(childDiagnostic.getMessage());
+ sb.add(childDiagnostic.getMessage());
}
return sb.toString();
}
+ private static final Comparator<Diagnostic> HIGEST_SEVERITY_FIRST = Comparator //
+ .comparingInt(Diagnostic::getSeverity).reversed() // highest first
+ .thenComparing(Diagnostic::getMessage);
+
/**
* Extract the message to display from a collection of {@link Diagnostic Diagnostics}. If the severity of the
* Diagnostic is {@link Diagnostic#OK} then it is skipped.
@@ -67,27 +68,13 @@
*/
public static String getMessage(Collection<Diagnostic> diagnostics) {
final List<Diagnostic> diagnosticList = new ArrayList<>(diagnostics);
- Collections.sort(diagnosticList, new Comparator<Diagnostic>() {
+ diagnosticList.sort(HIGEST_SEVERITY_FIRST);
- @Override
- public int compare(Diagnostic o1, Diagnostic o2) {
- if (o1.getSeverity() != o2.getSeverity()) {
- // highest first
- return o2.getSeverity() - o1.getSeverity();
- }
- return o1.getMessage().compareTo(o2.getMessage());
- }
- });
-
- final StringBuilder sb = new StringBuilder();
+ final StringJoiner sb = new StringJoiner("\n"); //$NON-NLS-1$
for (final Diagnostic diagnostic : diagnosticList) {
- if (diagnostic.getSeverity() == Diagnostic.OK) {
- continue;
+ if (diagnostic.getSeverity() != Diagnostic.OK) {
+ sb.add(getMessage(diagnostic));
}
- if (sb.length() > 0) {
- sb.append("\n"); //$NON-NLS-1$
- }
- sb.append(getMessage(diagnostic));
}
return sb.toString();
}
diff --git a/bundles/org.eclipse.emfforms.common.prevalidation/src/org/eclipse/emfforms/internal/common/prevalidation/PreSetValidationServiceImpl.java b/bundles/org.eclipse.emfforms.common.prevalidation/src/org/eclipse/emfforms/internal/common/prevalidation/PreSetValidationServiceImpl.java
index c5b3528..495f2f9 100644
--- a/bundles/org.eclipse.emfforms.common.prevalidation/src/org/eclipse/emfforms/internal/common/prevalidation/PreSetValidationServiceImpl.java
+++ b/bundles/org.eclipse.emfforms.common.prevalidation/src/org/eclipse/emfforms/internal/common/prevalidation/PreSetValidationServiceImpl.java
@@ -18,7 +18,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
-import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.emf.common.util.BasicDiagnostic;
@@ -60,8 +59,7 @@
private static final String MULTI_LITERAL_SEP = "|"; //$NON-NLS-1$
private static final String ESCAPED_MULTI_LITERAL_SEP = "\\|"; //$NON-NLS-1$
- private Map<ENamedElement, Set<IFeatureConstraint>> constraints = //
- new LinkedHashMap<ENamedElement, Set<IFeatureConstraint>>();
+ private Map<ENamedElement, Set<IFeatureConstraint>> constraints = new LinkedHashMap<>();
@Override
public Diagnostic validate(final EStructuralFeature eStructuralFeature, Object value) {
@@ -70,19 +68,14 @@
@Override
public Diagnostic validate(final EStructuralFeature eStructuralFeature, Object value, Map<Object, Object> context) {
- return validate(
- new PreSetValidator() {
- @Override
- public boolean validate(EDataType eDataType, Object value, DiagnosticChain diagnostics,
- Map<Object, Object> context) {
- EValidator validator = EValidator.Registry.INSTANCE
- .getEValidator(eStructuralFeature.getEType().getEPackage());
- if (validator == null) {
- validator = new EObjectValidator();
- }
- return validator.validate(eDataType, value, diagnostics, context);
- }
- },
+ return validate((eDataType, innerValue, diagnostics, innerContext) -> {
+ EValidator validator = EValidator.Registry.INSTANCE
+ .getEValidator(eStructuralFeature.getEType().getEPackage());
+ if (validator == null) {
+ validator = new EObjectValidator();
+ }
+ return validator.validate(eDataType, innerValue, diagnostics, innerContext);
+ },
eStructuralFeature,
value,
context);
@@ -98,7 +91,7 @@
public Diagnostic validateLoose(EStructuralFeature eStructuralFeature, Object value) {
final EClassifier eType = eStructuralFeature.getEType();
- if (!EDataType.class.isInstance(eType) || EDataType.class.cast(eType).getEPackage() == EcorePackage.eINSTANCE) {
+ if (!(eType instanceof EDataType) || ((EDataType) eType).getEPackage() == EcorePackage.eINSTANCE) {
return new BasicDiagnostic();
}
@@ -116,9 +109,9 @@
BasicDiagnostic diagnostics = new BasicDiagnostic();
- if (eType instanceof EDataType && EDataType.class.cast(eType).getEPackage() != EcorePackage.eINSTANCE) {
+ if (eType instanceof EDataType && ((EDataType) eType).getEPackage() != EcorePackage.eINSTANCE) {
- final EDataType eDataType = EDataType.class.cast(eType);
+ final EDataType eDataType = (EDataType) eType;
diagnostics = Diagnostician.INSTANCE.createDefaultDiagnostic(eDataType, value);
boolean skipValidator = false;
@@ -197,22 +190,16 @@
for (final IFeatureConstraint constraint : constraints) {
final Diagnostic result = constraint.validate(eStructuralFeature, value, context);
- if (result.getSeverity() == Diagnostic.OK) {
- continue;
+ if (result.getSeverity() != Diagnostic.OK) {
+ diagnostics.add(result);
}
- diagnostics.add(result);
}
}
@Override
public void addConstraintValidator(ENamedElement element, IFeatureConstraint constraint) {
-
- if (!constraints.containsKey(element)) {
- constraints.put(element, new LinkedHashSet<IFeatureConstraint>());
- }
- constraints.get(element).add(constraint);
-
+ constraints.computeIfAbsent(element, e -> new LinkedHashSet<>()).add(constraint);
}
/**
@@ -222,7 +209,7 @@
*/
@Activate
protected void activate(BundleContext bundleContext) {
- constraints = new LinkedHashMap<ENamedElement, Set<IFeatureConstraint>>();
+ constraints = new LinkedHashMap<>();
}
/**
@@ -254,18 +241,9 @@
final Optional<String> loosePattern = findLooseConstraint(eDataType, LOOSE_PATTERN_KEY);
if (loosePattern.isPresent()) {
- return super.validatePattern(eDataType, value, new PatternMatcher[][] {
- {
- new PatternMatcher() {
- @Override
- public boolean matches(String value) {
- final Pattern pattern = Pattern.compile(loosePattern.get());
- final Matcher matcher = pattern.matcher(value);
- return matcher.matches();
- }
- }
- }
- }, diagnostics, context);
+ return super.validatePattern(eDataType, value, new PatternMatcher[][] { {
+ v -> Pattern.matches(loosePattern.get(), v)
+ } }, diagnostics, context);
}
return super.validatePattern(eDataType, value, patterns, diagnostics, context);