Skip to content

Package: MappingSegmentIdeDescriptor

MappingSegmentIdeDescriptor

nameinstructionbranchcomplexitylinemethod
MappingSegmentIdeDescriptor()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getEStructuralFeatureSelectionValidator()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getMapKeyType(EClass)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getMapType(EClass, String)
M: 2 C: 24
92%
M: 4 C: 4
50%
M: 4 C: 1
20%
M: 1 C: 5
83%
M: 0 C: 1
100%
getMapValueType(EClass)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getReferenceTypeResolver()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getSegmentType()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isAllowedAsLastElementInPath()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isAvailableInIde()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isLastElementInPath()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$0(EStructuralFeature)
M: 0 C: 36
100%
M: 4 C: 8
67%
M: 4 C: 3
43%
M: 0 C: 10
100%
M: 0 C: 1
100%
lambda$1(EReference, VDomainModelReferenceSegment)
M: 0 C: 30
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-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: * Lucas Koehler - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.internal.ide.view.mappingsegment;
15:
16: import java.text.MessageFormat;
17:
18: import org.eclipse.emf.ecore.EClass;
19: import org.eclipse.emf.ecore.EReference;
20: import org.eclipse.emf.ecore.EStructuralFeature;
21: import org.eclipse.emf.ecore.EcorePackage;
22: import org.eclipse.emf.ecp.view.spi.editor.controls.EStructuralFeatureSelectionValidator;
23: import org.eclipse.emf.ecp.view.spi.editor.controls.ReferenceTypeResolver;
24: import org.eclipse.emf.ecp.view.spi.editor.controls.SegmentIdeDescriptor;
25: import org.eclipse.emfforms.spi.view.mappingsegment.model.VMappingDomainModelReferenceSegment;
26: import org.eclipse.emfforms.spi.view.mappingsegment.model.VMappingsegmentPackage;
27: import org.osgi.service.component.annotations.Component;
28:
29: /**
30: * {@link SegmentIdeDescriptor} for
31: * {@link org.eclipse.emfforms.spi.view.mappingsegment.model.VMappingDomainModelReferenceSegment
32: * VMappingDomainModelReferenceSegments}.
33: *
34: * @author Lucas Koehler
35: *
36: */
37: @Component(name = "MappingSegmentIdeDescriptor")
38: public class MappingSegmentIdeDescriptor implements SegmentIdeDescriptor {
39:
40:         /** Feature containing the key of the map. */
41:         private static final String KEY = "key"; //$NON-NLS-1$
42:         /** Feature containing the value of the map. */
43:         private static final String VALUE = "value"; //$NON-NLS-1$
44:         /** Name patter for EMap types. */
45:         private static final String MAP_ENTRY_TYPE_REGEX = "EClassTo.+Map"; //$NON-NLS-1$
46:         /** Instance class name of EMaps. */
47:         private static final String JAVA_UTIL_MAP_ENTRY = "java.util.Map$Entry"; //$NON-NLS-1$
48:
49:         @Override
50:         public EClass getSegmentType() {
51:                 return VMappingsegmentPackage.Literals.MAPPING_DOMAIN_MODEL_REFERENCE_SEGMENT;
52:         }
53:
54:         @Override
55:         public boolean isAvailableInIde() {
56:                 return true;
57:         }
58:
59:         @Override
60:         public boolean isLastElementInPath() {
61:                 return false;
62:         }
63:
64:         @Override
65:         public boolean isAllowedAsLastElementInPath() {
66:                 return false;
67:         }
68:
69:         @Override
70:         public EStructuralFeatureSelectionValidator getEStructuralFeatureSelectionValidator() {
71:                 return structuralFeature -> {
72:•                        if (structuralFeature != null && EReference.class.isInstance(structuralFeature)
73:•                                && structuralFeature.isMany()) {
74:                                 final EReference ref = (EReference) structuralFeature;
75:                                 final EClass refType = ref.getEReferenceType();
76:                                 final EClass keyEClass = getMapKeyType(refType);
77:•                                if (keyEClass != null && EcorePackage.eINSTANCE.getEClass().isSuperTypeOf(keyEClass)) {
78:                                         final EClass valueEClass = getMapValueType(refType);
79:•                                        if (valueEClass != null) {
80:                                                 return null;
81:                                         }
82:                                 }
83:                         }
84:                         return "A mapping segment requires a map which has EClass as its key type and an EReference for its values."; //$NON-NLS-1$
85:                 };
86:         }
87:
88:         @Override
89:         public ReferenceTypeResolver getReferenceTypeResolver() {
90:                 return (reference, segment) -> {
91:•                        if (VMappingDomainModelReferenceSegment.class.isInstance(segment)) {
92:                                 final VMappingDomainModelReferenceSegment mappingSegment = (VMappingDomainModelReferenceSegment) segment;
93:•                                if (mappingSegment.getMappedClass() != null) {
94:                                         return mappingSegment.getMappedClass();
95:                                 }
96:                                 return getMapValueType(reference.getEReferenceType());
97:                         }
98:                         throw new IllegalArgumentException(
99:                                 MessageFormat.format("The given segment '{0}' isn't a mapping segment", segment)); //$NON-NLS-1$
100:                 };
101:         }
102:
103:         /**
104:          * @param mapType
105:          * @return The type of the map's key, <code>null</code> if the type is not resolvable
106:          */
107:         private EClass getMapValueType(EClass mapType) {
108:                 return getMapType(mapType, VALUE);
109:         }
110:
111:         /**
112:          * @param mapType
113:          * @return The type of the map's value, <code>null</code> if the type is not resolvable
114:          */
115:         private EClass getMapKeyType(EClass mapType) {
116:                 return getMapType(mapType, KEY);
117:         }
118:
119:         private EClass getMapType(EClass mapType, String featureName) {
120:•                if (JAVA_UTIL_MAP_ENTRY.equals(mapType.getInstanceClassName())
121:•                        && mapType.getName().matches(MAP_ENTRY_TYPE_REGEX)) {
122:
123:                         final EStructuralFeature feature = mapType.getEStructuralFeature(featureName);
124:•                        if (feature != null && EReference.class.isInstance(feature)) {
125:                                 return ((EReference) feature).getEReferenceType();
126:                         }
127:                 }
128:                 return null;
129:         }
130: }