Skip to content

Package: IndexSegmentConverter

IndexSegmentConverter

nameinstructionbranchcomplexitylinemethod
IndexSegmentConverter()
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%
checkAndConvertSegment(VDomainModelReferenceSegment)
M: 10 C: 21
68%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 2 C: 6
75%
M: 0 C: 1
100%
checkListType(EStructuralFeature)
M: 5 C: 4
44%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 2 C: 2
50%
M: 0 C: 1
100%
convertToListProperty(VDomainModelReferenceSegment, EClass, EditingDomain)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
convertToValueProperty(VDomainModelReferenceSegment, EClass, EditingDomain)
M: 22 C: 40
65%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 4 C: 11
73%
M: 0 C: 1
100%
getSetting(VDomainModelReferenceSegment, EObject)
M: 29 C: 26
47%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 5 C: 7
58%
M: 0 C: 1
100%
isApplicable(VDomainModelReferenceSegment)
M: 0 C: 11
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%

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.index;
15:
16: import org.eclipse.emf.databinding.IEMFValueProperty;
17: import org.eclipse.emf.databinding.internal.EMFValuePropertyDecorator;
18: import org.eclipse.emf.ecore.EClass;
19: import org.eclipse.emf.ecore.EObject;
20: import org.eclipse.emf.ecore.EReference;
21: import org.eclipse.emf.ecore.EStructuralFeature;
22: import org.eclipse.emf.ecore.EStructuralFeature.Setting;
23: import org.eclipse.emf.ecp.common.spi.asserts.Assert;
24: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReferenceSegment;
25: import org.eclipse.emf.edit.domain.EditingDomain;
26: import org.eclipse.emfforms.internal.core.services.databinding.SegmentConverterValueResultImpl;
27: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
28: import org.eclipse.emfforms.spi.core.services.databinding.emf.DomainModelReferenceSegmentConverterEMF;
29: import org.eclipse.emfforms.spi.core.services.databinding.emf.SegmentConverterListResultEMF;
30: import org.eclipse.emfforms.spi.core.services.databinding.emf.SegmentConverterValueResultEMF;
31: import org.eclipse.emfforms.spi.view.indexsegment.model.VIndexDomainModelReferenceSegment;
32: import org.osgi.service.component.annotations.Component;
33:
34: /**
35: * Converts {@link VIndexDomainModelReferenceSegment VIndexDomainModelReferenceSegments} to value and list properties,
36: * and to {@link Setting settings}.
37: *
38: * @author Lucas Koehler
39: *
40: */
41: @SuppressWarnings("restriction")
42: @Component(name = "IndexSegmentConverter")
43: public class IndexSegmentConverter implements DomainModelReferenceSegmentConverterEMF {
44:
45:         @Override
46:         public double isApplicable(VDomainModelReferenceSegment segment) {
47:                 Assert.create(segment).notNull();
48:•                if (segment instanceof VIndexDomainModelReferenceSegment) {
49:                         return 10d;
50:                 }
51:                 return NOT_APPLICABLE;
52:         }
53:
54:         @Override
55:         public SegmentConverterValueResultEMF convertToValueProperty(VDomainModelReferenceSegment segment,
56:                 EClass segmentRoot, EditingDomain editingDomain) throws DatabindingFailedException {
57:                 final VIndexDomainModelReferenceSegment indexSegment = checkAndConvertSegment(segment);
58:
59:                 final EStructuralFeature structuralFeature = segmentRoot
60:                         .getEStructuralFeature(indexSegment.getDomainModelFeature());
61:•                if (structuralFeature == null) {
62:                         throw new DatabindingFailedException(String.format(
63:                                 "The segment's feature could not be resolved for the given EClass. The segment was %1$s. The EClass was %2$s.", //$NON-NLS-1$
64:                                 segment, segmentRoot));
65:                 }
66:
67:                 checkListType(structuralFeature);
68:
69:                 final EMFIndexedValueProperty indexProperty = new EMFIndexedValueProperty(editingDomain,
70:                         indexSegment.getIndex(), structuralFeature);
71:                 final IEMFValueProperty resultProperty = new EMFValuePropertyDecorator(indexProperty, structuralFeature);
72:•                if (EReference.class.isInstance(structuralFeature)) {
73:                         return new SegmentConverterValueResultImpl(resultProperty,
74:                                 ((EReference) structuralFeature).getEReferenceType());
75:                 }
76:                 return new SegmentConverterValueResultImpl(resultProperty, null);
77:         }
78:
79:         @Override
80:         public SegmentConverterListResultEMF convertToListProperty(VDomainModelReferenceSegment segment, EClass segmentRoot,
81:                 EditingDomain editingDomain) throws DatabindingFailedException {
82:                 throw new UnsupportedOperationException(
83:                         "A VIndexDomainModelReferenceSegment cannot be converted to a list property, only to a value property."); //$NON-NLS-1$
84:         }
85:
86:         @Override
87:         public Setting getSetting(VDomainModelReferenceSegment segment, EObject eObject) throws DatabindingFailedException {
88:                 final VIndexDomainModelReferenceSegment indexSegment = checkAndConvertSegment(segment);
89:
90:                 final EStructuralFeature structuralFeature = eObject.eClass()
91:                         .getEStructuralFeature(indexSegment.getDomainModelFeature());
92:•                if (structuralFeature == null) {
93:                         throw new DatabindingFailedException(String.format(
94:                                 "The given EOject does not contain the segment's feature. The segment was %1$s. The EObject was %2$s.", //$NON-NLS-1$
95:                                 segment, eObject));
96:                 }
97:•                if (structuralFeature.getEType() == null) {
98:                         throw new DatabindingFailedException(
99:                                 String.format("The eType of the feature %1$s is null.", structuralFeature.getName())); //$NON-NLS-1$
100:                 }
101:
102:                 checkListType(structuralFeature);
103:
104:                 return new IndexedSetting(eObject, structuralFeature, indexSegment.getIndex());
105:         }
106:
107:         private VIndexDomainModelReferenceSegment checkAndConvertSegment(VDomainModelReferenceSegment segment)
108:                 throws DatabindingFailedException {
109:                 Assert.create(segment).notNull();
110:                 Assert.create(segment).ofClass(VIndexDomainModelReferenceSegment.class);
111:
112:                 final VIndexDomainModelReferenceSegment indexSegment = (VIndexDomainModelReferenceSegment) segment;
113:
114:•                if (indexSegment.getDomainModelFeature() == null) {
115:                         throw new DatabindingFailedException("The segment's domain model feature must not be null."); //$NON-NLS-1$
116:                 }
117:•                if (indexSegment.getDomainModelFeature().isEmpty()) {
118:                         throw new DatabindingFailedException("The segment's domain model feature must not be an empty string."); //$NON-NLS-1$
119:                 }
120:
121:                 return indexSegment;
122:         }
123:
124:         /**
125:          * Checks whether the given structural feature references a proper list to generate a value or list property.
126:          *
127:          * @param structuralFeature The feature to check
128:          * @throws IllegalListTypeException if the structural feature doesn't reference a proper list.
129:          */
130:         private void checkListType(EStructuralFeature structuralFeature) throws IllegalListTypeException {
131:•                if (!structuralFeature.isMany()) {
132:                         throw new IllegalListTypeException(
133:                                 "The VIndexDomainModelReferenceSegment's domainModelFeature must reference a list."); //$NON-NLS-1$
134:                 }
135:         }
136: }