Skip to content

Package: VDiagnosticHelper

VDiagnosticHelper

nameinstructionbranchcomplexitylinemethod
arePropertiesEqual(VDiagnostic, VDiagnostic)
M: 0 C: 26
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
areUnderlyingDiagnosticsEqual(Diagnostic, Diagnostic)
M: 10 C: 65
87%
M: 5 C: 9
64%
M: 5 C: 3
38%
M: 5 C: 10
67%
M: 0 C: 1
100%
clean(VDiagnostic)
M: 1 C: 58
98%
M: 2 C: 6
75%
M: 2 C: 3
60%
M: 1 C: 12
92%
M: 0 C: 1
100%
isEqual(VDiagnostic, VDiagnostic)
M: 2 C: 49
96%
M: 1 C: 13
93%
M: 1 C: 7
88%
M: 1 C: 14
93%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2013 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.internal.validation;
15:
16: import java.util.LinkedHashMap;
17: import java.util.Map;
18:
19: import org.eclipse.emf.common.util.Diagnostic;
20: import org.eclipse.emf.common.util.DiagnosticChain;
21: import org.eclipse.emf.ecore.EObject;
22: import org.eclipse.emf.ecp.view.spi.model.VDiagnostic;
23: import org.eclipse.emf.ecp.view.spi.model.VViewFactory;
24:
25: /**
26: * This class compares to {@link VDiagnostic} elements for equality.
27: *
28: * @author Eugen Neufeld
29: *
30: */
31: public final class VDiagnosticHelper {
32:
33:         private VDiagnosticHelper() {
34:
35:         }
36:
37:         /**
38:          * Compares two {@link VDiagnostic VDiagnostics} with each other.
39:          *
40:          * @param vDiagnostic1 the first {@link VDiagnostic} to compare
41:          * @param vDiagnostic2 the second {@link VDiagnostic} to compare
42:          * @return true if both {@link VDiagnostic VDiagnostics} are equal
43:          */
44:         public static boolean isEqual(VDiagnostic vDiagnostic1, VDiagnostic vDiagnostic2) {
45:
46:•                if (vDiagnostic1 == null && vDiagnostic2 == null) {
47:                         return true;
48:                 }
49:•                if (vDiagnostic1 == null) {
50:                         return false;
51:                 }
52:•                if (vDiagnostic2 == null) {
53:                         return false;
54:                 }
55:
56:                 final boolean arePropertiesEqual = arePropertiesEqual(vDiagnostic1, vDiagnostic2);
57:•                if (!arePropertiesEqual) {
58:                         return false;
59:                 }
60:
61:•                for (int i = 0; i < vDiagnostic1.getDiagnostics().size(); i++) {
62:                         final Diagnostic diagnostic1 = (Diagnostic) vDiagnostic1.getDiagnostics().get(i);
63:                         final Diagnostic diagnostic2 = (Diagnostic) vDiagnostic2.getDiagnostics().get(i);
64:•                        if (!areUnderlyingDiagnosticsEqual(diagnostic1, diagnostic2)) {
65:                                 return false;
66:                         }
67:                 }
68:
69:                 return true;
70:         }
71:
72:         private static boolean arePropertiesEqual(VDiagnostic vDiagnostic1, VDiagnostic vDiagnostic2) {
73:•                if (vDiagnostic1.getHighestSeverity() != vDiagnostic2.getHighestSeverity()) {
74:                         return false;
75:                 }
76:•                if (!vDiagnostic1.getMessage().equals(vDiagnostic2.getMessage())) {
77:                         return false;
78:                 }
79:•                if (vDiagnostic1.getDiagnostics().size() != vDiagnostic2.getDiagnostics().size()) {
80:                         return false;
81:                 }
82:                 return true;
83:         }
84:
85:         /**
86:          * @param vDiagnostic1
87:          * @param vDiagnostic2
88:          */
89:         private static boolean areUnderlyingDiagnosticsEqual(Diagnostic diagnostic1, Diagnostic diagnostic2) {
90:
91:                 // TODO: How can these cases ever apply? We already did check these since VDiagnostic#getHighestSeverity()
92:                 // and VDiagnostic#getMessage() test the underlying diagnostic
93:                 // FIXME: test order
94:•                if (diagnostic1.getSeverity() != diagnostic2.getSeverity()) {
95:                         return false;
96:                 }
97:
98:•                if (diagnostic1.getData().size() != diagnostic2.getData().size()) {
99:                         return false;
100:                 }
101:
102:•                if (diagnostic1.getChildren().size() != diagnostic2.getChildren().size()) {
103:                         return false;
104:                 }
105:
106:•                for (int j = 0; j < diagnostic1.getData().size(); j++) {
107:                         final Object data1 = diagnostic1.getData().get(j);
108:                         final Object data2 = diagnostic2.getData().get(j);
109:
110:•                        if (!data1.equals(data2)) {
111:                                 return false;
112:                         }
113:                 }
114:
115:•                for (int i = 0; i < diagnostic1.getChildren().size(); i++) {
116:•                        if (!areUnderlyingDiagnosticsEqual(diagnostic1.getChildren().get(i), diagnostic2.getChildren().get(i))) {
117:                                 return false;
118:                         }
119:                 }
120:
121:                 return true;
122:         }
123:
124:         /**
125:          * Analysis a {@link VDiagnostic} and merges all Diagnostic pointing to the same EObject.
126:          *
127:          * @param value the {@link VDiagnostic} that should be cleaned
128:          * @return a cleaned {@link VDiagnostic}
129:          */
130:         public static VDiagnostic clean(VDiagnostic value) {
131:                 final VDiagnostic result = VViewFactory.eINSTANCE.createDiagnostic();
132:                 final Map<EObject, Diagnostic> map = new LinkedHashMap<EObject, Diagnostic>();
133:•                for (final Object object : value.getDiagnostics()) {
134:                         final Diagnostic diagnostic = (Diagnostic) object;
135:•                        if (diagnostic.getData() == null || diagnostic.getData().size() == 0) {
136:                                 continue;
137:                         }
138:                         final EObject eObject = (EObject) diagnostic.getData().get(0);
139:•                        if (map.containsKey(eObject)) {
140:                                 ((DiagnosticChain) map.get(eObject)).merge(diagnostic);
141:                         } else {
142:                                 map.put(eObject, diagnostic);
143:                         }
144:                 }
145:                 result.getDiagnostics().addAll(map.values());
146:                 return result;
147:         }
148: }