Skip to content

Package: DiagnosticMessageExtractor

DiagnosticMessageExtractor

nameinstructionbranchcomplexitylinemethod
getMessage(Collection)
M: 0 C: 35
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
getMessage(Diagnostic)
M: 2 C: 38
95%
M: 2 C: 6
75%
M: 2 C: 3
60%
M: 1 C: 7
88%
M: 0 C: 1
100%
static {...}
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2015 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Eugen Neufeld - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.spi.model;
15:
16: import java.util.ArrayList;
17: import java.util.Collection;
18: import java.util.Comparator;
19: import java.util.List;
20: import java.util.StringJoiner;
21:
22: import org.eclipse.emf.common.util.Diagnostic;
23:
24: /**
25: * This Util allows to extract messages from Diagnostics.
26: *
27: * @author Eugen Neufeld
28: * @since 1.5
29: *
30: */
31: public final class DiagnosticMessageExtractor {
32:         private DiagnosticMessageExtractor() {
33:                 // Util class constructor
34:         }
35:
36:         /**
37:          * Extract the message to display from a single {@link Diagnostic}. If the severity of the Diagnostic is
38:          * {@link Diagnostic#OK} then the message is empty.
39:          *
40:          * @param diagnostic The {@link Diagnostic} to get the message for
41:          * @return The message or an empty string if the severity of the {@link Diagnostic} is ok.
42:          */
43:
44:         public static String getMessage(Diagnostic diagnostic) {
45:•                if (diagnostic.getSeverity() == Diagnostic.OK) {
46:                         return ""; //$NON-NLS-1$
47:                 }
48:•                if (diagnostic.getChildren() != null && diagnostic.getChildren().isEmpty()) {
49:                         return diagnostic.getMessage();
50:                 }
51:                 final StringJoiner sb = new StringJoiner("\n"); //$NON-NLS-1$
52:•                for (final Diagnostic childDiagnostic : diagnostic.getChildren()) {
53:                         sb.add(childDiagnostic.getMessage());
54:                 }
55:                 return sb.toString();
56:         }
57:
58:         private static final Comparator<Diagnostic> HIGEST_SEVERITY_FIRST = Comparator //
59:                 .comparingInt(Diagnostic::getSeverity).reversed() // highest first
60:                 .thenComparing(Diagnostic::getMessage);
61:
62:         /**
63:          * Extract the message to display from a collection of {@link Diagnostic Diagnostics}. If the severity of the
64:          * Diagnostic is {@link Diagnostic#OK} then it is skipped.
65:          *
66:          * @param diagnostics The Collection of {@link Diagnostic Diagnostics} to get the message for
67:          * @return The compound message for all {@link Diagnostic Diagnostics}
68:          */
69:         public static String getMessage(Collection<Diagnostic> diagnostics) {
70:                 final List<Diagnostic> diagnosticList = new ArrayList<>(diagnostics);
71:                 diagnosticList.sort(HIGEST_SEVERITY_FIRST);
72:
73:                 final StringJoiner sb = new StringJoiner("\n"); //$NON-NLS-1$
74:•                for (final Diagnostic diagnostic : diagnosticList) {
75:•                        if (diagnostic.getSeverity() != Diagnostic.OK) {
76:                                 sb.add(getMessage(diagnostic));
77:                         }
78:                 }
79:                 return sb.toString();
80:         }
81: }