Skip to content

Package: DiagnosticHelper

DiagnosticHelper

nameinstructionbranchcomplexitylinemethod
checkDiagnosticData(Diagnostic)
M: 4 C: 17
81%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 2 C: 6
75%
M: 0 C: 1
100%
getEStructuralFeature(List)
M: 29 C: 18
38%
M: 8 C: 4
33%
M: 5 C: 2
29%
M: 5 C: 5
50%
M: 0 C: 1
100%
getFirstInternalEObject(List)
M: 27 C: 15
36%
M: 9 C: 3
25%
M: 6 C: 1
14%
M: 6 C: 4
40%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2017-2019 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: * Mat Hansen - initial API and implementation
13: * Christian W. Damus - bug 545686
14: ******************************************************************************/
15: package org.eclipse.emfforms.common.internal.validation;
16:
17: import java.util.List;
18:
19: import org.eclipse.emf.common.util.Diagnostic;
20: import org.eclipse.emf.ecore.EStructuralFeature;
21: import org.eclipse.emf.ecore.InternalEObject;
22:
23: /**
24: * Helper class for validation handling.
25: *
26: * @author Mat Hansen <mhansen@eclipsesource.com>
27: */
28: public final class DiagnosticHelper {
29:
30:         private DiagnosticHelper() {
31:
32:         }
33:
34:         /**
35:          * Returns the first {@link EStructuralFeature} in the given list.
36:          *
37:          * @param data the list to search for a {@link EStructuralFeature}
38:          * @return the found {@link EStructuralFeature}, null if no {@link EStructuralFeature} is found
39:          */
40:         public static EStructuralFeature getEStructuralFeature(List<?> data) {
41:                 // Exclude first object for cases when we validate an EStructuralFeature.
42:•                if (data == null || data.size() < 2) {
43:                         return null;
44:                 }
45:
46:                 // The usual case is that the structural feature is the second object.
47:                 final Object usual = data.get(1);
48:•                if (usual instanceof EStructuralFeature) {
49:                         return (EStructuralFeature) usual;
50:                 }
51:
52:•                if (data.size() > 2) {
53:•                        for (final Object object : data.subList(2, data.size())) {
54:•                                if (EStructuralFeature.class.isInstance(object)) {
55:                                         return EStructuralFeature.class.cast(object);
56:                                 }
57:                         }
58:                 }
59:
60:                 return null;
61:         }
62:
63:         /**
64:          * Returns the first {@link InternalEObject} in the given list.
65:          *
66:          * @param data the list to search for a {@link InternalEObject}
67:          * @return the found {@link InternalEObject}, null if no {@link InternalEObject} is found
68:          */
69:         public static InternalEObject getFirstInternalEObject(List<?> data) {
70:•                if (data == null || data.isEmpty()) {
71:                         return null;
72:                 }
73:
74:                 // The usual case is that the subject of the problem is the first object
75:                 final Object usual = data.get(0);
76:•                if (usual instanceof InternalEObject) {
77:                         return (InternalEObject) usual;
78:                 }
79:
80:•                if (data.size() > 1) {
81:•                        for (final Object object : data) {
82:•                                if (InternalEObject.class.isInstance(object)) {
83:                                         return InternalEObject.class.cast(object);
84:                                 }
85:                         }
86:                 }
87:
88:                 return null;
89:         }
90:
91:         /**
92:          * Verify if the given {@link Diagnostic} contains at least one {@link EStructuralFeature} or a
93:          * {@link InternalEObject}.
94:          *
95:          * @param diagnostic the {@link Diagnostic} to check
96:          * @return true if the {@link Diagnostic} contains at least one {@link EStructuralFeature} or
97:          * {@link InternalEObject}, false otherwise
98:          */
99:         public static boolean checkDiagnosticData(Diagnostic diagnostic) {
100:                 final List<?> data = diagnostic.getData();
101:•                if (data.size() < 2) {
102:                         return false;
103:                 }
104:•                if (getFirstInternalEObject(data) == null) {
105:                         return false;
106:                 }
107:•                if (getEStructuralFeature(data) == null) {
108:                         return false;
109:                 }
110:                 return true;
111:         }
112:
113: }