Skip to content

Package: MappedSetting

MappedSetting

nameinstructionbranchcomplexitylinemethod
MappedSetting(EObject, EStructuralFeature, EClass)
M: 0 C: 21
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
checkMapType(EStructuralFeature)
M: 15 C: 31
67%
M: 4 C: 4
50%
M: 4 C: 1
20%
M: 6 C: 8
57%
M: 0 C: 1
100%
checkStructuralFeature(EStructuralFeature)
M: 20 C: 21
51%
M: 5 C: 5
50%
M: 5 C: 1
17%
M: 8 C: 5
38%
M: 0 C: 1
100%
get(boolean)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getEObject()
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%
getEStructuralFeature()
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%
isSet()
M: 10 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
set(Object)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
unset()
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2018 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.core.services.segments.mapping;
15:
16: import org.eclipse.emf.common.util.EMap;
17: import org.eclipse.emf.ecore.EClass;
18: import org.eclipse.emf.ecore.EObject;
19: import org.eclipse.emf.ecore.EReference;
20: import org.eclipse.emf.ecore.EStructuralFeature;
21: import org.eclipse.emf.ecore.EStructuralFeature.Setting;
22:
23: /**
24: * A {@link MappedSetting} is a {@link Setting} whose {@link EStructuralFeature} is a map which has {@link EClass
25: * EClasses} as its keys. Additionally, the setting contains an {@link EClass} that defines which value of the map is
26: * the value of the setting.
27: *
28: * @author Lucas Koehler
29: *
30: */
31: public class MappedSetting implements Setting {
32:
33:         private final EObject eObject;
34:         private final EStructuralFeature eStructuralFeature;
35:         private final EClass mappedEClass;
36:         private final EMap<EClass, Object> map;
37:
38:         /**
39:          * Creates a new {@link MappedSetting}.
40:          *
41:          * @param eObject The {@link EObject} holding a map
42:          * @param eStructuralFeature The specific feature holding a map
43:          * @param mappedEClass The EClass that defines which map entry's value is the value of this {@link Setting}.
44:          * @throws IllegalMapTypeException If the structural feature does not describe a valid map.
45:          */
46:         @SuppressWarnings("unchecked")
47:         public MappedSetting(EObject eObject, EStructuralFeature eStructuralFeature, EClass mappedEClass)
48:                 throws IllegalMapTypeException {
49:                 checkMapType(eStructuralFeature);
50:
51:                 this.eObject = eObject;
52:                 this.eStructuralFeature = eStructuralFeature;
53:                 this.mappedEClass = mappedEClass;
54:                 map = (EMap<EClass, Object>) eObject.eGet(eStructuralFeature);
55:         }
56:
57:         @Override
58:         public EObject getEObject() {
59:                 return eObject;
60:         }
61:
62:         @Override
63:         public EStructuralFeature getEStructuralFeature() {
64:                 return eStructuralFeature;
65:         }
66:
67:         @Override
68:         public Object get(boolean resolve) {
69:                 // Always resolved => ignore the parameter
70:                 return map.get(mappedEClass);
71:         }
72:
73:         @Override
74:         public void set(Object newValue) {
75:                 map.put(mappedEClass, newValue);
76:         }
77:
78:         @Override
79:         public boolean isSet() {
80:•                return map.get(mappedEClass) != null ? true : false;
81:         }
82:
83:         @Override
84:         public void unset() {
85:                 map.removeKey(mappedEClass);
86:         }
87:
88:         /**
89:          * Checks whether the given structural feature references a proper map.
90:          *
91:          * @param structuralFeature The feature to check
92:          * @throws IllegalMapTypeException if the structural feature doesn't reference a proper map.
93:          */
94:         private void checkMapType(EStructuralFeature structuralFeature) throws IllegalMapTypeException {
95:                 checkStructuralFeature(structuralFeature);
96:
97:                 final EClass eClass = (EClass) structuralFeature.getEType();
98:                 final EStructuralFeature keyFeature = eClass.getEStructuralFeature("key"); //$NON-NLS-1$
99:                 final EStructuralFeature valueFeature = eClass.getEStructuralFeature("value"); //$NON-NLS-1$
100:•                if (keyFeature == null || valueFeature == null) {
101:                         throw new IllegalMapTypeException(
102:                                 "The given structural feature must reference a map."); //$NON-NLS-1$
103:                 }
104:•                if (!EReference.class.isInstance(keyFeature)) {
105:                         throw new IllegalMapTypeException(
106:                                 "The keys of the map referenced by the given structural feature must be referenced EClasses."); //$NON-NLS-1$
107:                 }
108:•                if (!EClass.class.isAssignableFrom(((EReference) keyFeature).getEReferenceType().getInstanceClass())) {
109:                         throw new IllegalMapTypeException(
110:                                 "The keys of the map referenced by the given structural feature must be referenced EClasses."); //$NON-NLS-1$
111:                 }
112:         }
113:
114:         /**
115:          * Checks basic required properties of the given {@link EStructuralFeature}.
116:          *
117:          * @param structuralFeature The {@link EStructuralFeature} to check
118:          * @throws IllegalMapTypeException if something's wrong with the feature
119:          */
120:         private void checkStructuralFeature(EStructuralFeature structuralFeature) throws IllegalMapTypeException {
121:•                if (structuralFeature.getEType() == null) {
122:                         throw new IllegalMapTypeException(
123:                                 "The EType of the given structural feature was null."); //$NON-NLS-1$
124:                 }
125:•                if (structuralFeature.getEType().getInstanceClassName() == null) {
126:                         throw new IllegalMapTypeException(
127:                                 "The InstanceClassName of the given structural feature's EType was null."); //$NON-NLS-1$
128:                 }
129:•                if (!structuralFeature.getEType().getInstanceClassName().equals("java.util.Map$Entry")) { //$NON-NLS-1$
130:                         throw new IllegalMapTypeException(
131:                                 "The given structural feature must reference a map."); //$NON-NLS-1$
132:                 }
133:•                if (structuralFeature.getLowerBound() != 0 || structuralFeature.getUpperBound() != -1) {
134:                         throw new IllegalMapTypeException(
135:                                 "The given structural feature must reference a map."); //$NON-NLS-1$
136:                 }
137:         }
138: }