Skip to content

Package: DerivedAttributeAdapter

DerivedAttributeAdapter

nameinstructionbranchcomplexitylinemethod
DerivedAttributeAdapter(EObject, EStructuralFeature)
M: 0 C: 26
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
DerivedAttributeAdapter(EObject, EStructuralFeature, EStructuralFeature)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
DerivedAttributeAdapter(EObject, EStructuralFeature, EStructuralFeature, EStructuralFeature)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
DerivedAttributeAdapter(EObject, EStructuralFeature, EStructuralFeature, EStructuralFeature, EStructuralFeature)
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
addLocalDependency(EStructuralFeature)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
addNavigatedDependency(EStructuralFeature, EStructuralFeature)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
notifyChanged(Notification)
M: 54 C: 21
28%
M: 10 C: 6
38%
M: 7 C: 3
30%
M: 16 C: 6
27%
M: 0 C: 1
100%
notifyDerivedAttributeChange()
M: 0 C: 24
100%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 5
100%
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.spi.categorization.model;
15:
16: import java.util.ArrayList;
17: import java.util.List;
18:
19: import org.eclipse.emf.common.notify.Adapter;
20: import org.eclipse.emf.common.notify.Notification;
21: import org.eclipse.emf.common.notify.impl.AdapterImpl;
22: import org.eclipse.emf.ecore.EObject;
23: import org.eclipse.emf.ecore.EStructuralFeature;
24: import org.eclipse.emf.ecore.InternalEObject;
25: import org.eclipse.emf.ecore.impl.ENotificationImpl;
26: import org.eclipse.emf.ecore.util.EContentAdapter;
27:
28: /**
29: * @author Eugen Neufeld
30: *
31: */
32: public class DerivedAttributeAdapter extends AdapterImpl {
33:         private final InternalEObject source;
34:         private final EStructuralFeature derivedFeature;
35:
36:         private final List<EStructuralFeature> localFeatures = new ArrayList<EStructuralFeature>();
37:
38:         // TODO this lot could be put into a subclass and put in a list to allow for
39:         // multiple navigated dependencies
40:         private EStructuralFeature dependantFeature;
41:         private EStructuralFeature navigationFeature;
42:
43:         private final Adapter dependantAdapter = new EContentAdapter() {
44:
45:                 @Override
46:                 public void notifyChanged(Notification msg) {
47:
48:                         if (msg.getEventType() == Notification.SET
49:                                 && (msg.getFeature().equals(dependantFeature) || dependantFeature == null)) {
50:                                 notifyDerivedAttributeChange();
51:                         }
52:                 }
53:         };
54:
55:         /**
56:          * Convenience constructor for a local and navigated dependency.
57:          *
58:          * @param source the source {@link EObject}
59:          * @param derivedFeature the derived {@link EStructuralFeature}
60:          * @param navigationFeature the feature used for navigation
61:          * @param dependantFeature the {@link EStructuralFeature} being derived
62:          * @param localFeature the local {@link EStructuralFeature} being derived
63:          */
64:         public DerivedAttributeAdapter(EObject source, EStructuralFeature derivedFeature,
65:                 EStructuralFeature navigationFeature, EStructuralFeature dependantFeature, EStructuralFeature localFeature) {
66:                 this(source, derivedFeature);
67:                 addNavigatedDependency(navigationFeature, dependantFeature);
68:                 addLocalDependency(localFeature);
69:         }
70:
71:         /**
72:          * Convenience constructor for a navigated dependency.
73:          *
74:          * @param source the source {@link EObject}
75:          * @param derivedFeature the derived {@link EStructuralFeature}
76:          * @param navigationFeature the feature used for navigation
77:          * @param dependantFeature the {@link EStructuralFeature} being derived
78:          */
79:         public DerivedAttributeAdapter(EObject source, EStructuralFeature derivedFeature,
80:                 EStructuralFeature navigationFeature, EStructuralFeature dependantFeature) {
81:                 this(source, derivedFeature);
82:                 addNavigatedDependency(navigationFeature, dependantFeature);
83:         }
84:
85:         /**
86:          * Convenience constructor for a local dependency.
87:          *
88:          * @param source the source {@link EObject}
89:          * @param derivedFeature the derived {@link EStructuralFeature}
90:          * @param localFeature the local {@link EStructuralFeature} being derived
91:          */
92:         public DerivedAttributeAdapter(EObject source, EStructuralFeature derivedFeature,
93:                 EStructuralFeature localFeature) {
94:                 this(source, derivedFeature);
95:                 addLocalDependency(localFeature);
96:         }
97:
98:         /**
99:          * Constructor with source object and the derived feature.
100:          *
101:          * @param source the source {@link EObject}
102:          * @param derivedFeature the derived {@link EStructuralFeature}
103:          */
104:         public DerivedAttributeAdapter(EObject source, EStructuralFeature derivedFeature) {
105:                 super();
106:                 this.source = (InternalEObject) source;
107:                 this.derivedFeature = derivedFeature;
108:                 source.eAdapters().add(this);
109:         }
110:
111:         /**
112:          * Adds a navigated dependency for a derived feature.
113:          *
114:          * @param navigationFeature the feature used for navigation
115:          * @param dependantFeature the {@link EStructuralFeature} being derived
116:          */
117:         public void addNavigatedDependency(EStructuralFeature navigationFeature,
118:                 EStructuralFeature dependantFeature) {
119:                 this.dependantFeature = dependantFeature;
120:                 this.navigationFeature = navigationFeature;
121:         }
122:
123:         /**
124:          * Adds a local dependency for a derived feature.
125:          *
126:          * @param localFeature the local {@link EStructuralFeature} being derived
127:          */
128:         public void addLocalDependency(EStructuralFeature localFeature) {
129:                 localFeatures.add(localFeature);
130:         }
131:
132:         @Override
133:         public void notifyChanged(Notification notification) {
134:•                if (notification == null || notification.getFeature() == null) {
135:                         return;
136:                 }
137:•                if (notification.getFeature().equals(navigationFeature)) {
138:•                        switch (notification.getEventType()) {
139:                         // TODO support ADD_MANY/REMOVE_MANY?
140:                         case Notification.ADD:
141:                                 final EObject added = (EObject) notification.getNewValue();
142:                                 added.eAdapters().add(dependantAdapter);
143:                                 break;
144:                         case Notification.SET:
145:                                 final EObject newValue = (EObject) notification.getNewValue();
146:                                 final EObject oldValue = (EObject) notification.getOldValue();
147:•                                if (oldValue != null) {
148:                                         oldValue.eAdapters().remove(dependantAdapter);
149:                                 }
150:•                                if (newValue != null) {
151:                                         newValue.eAdapters().add(dependantAdapter);
152:                                 }
153:                                 break;
154:                         case Notification.REMOVE:
155:                                 final EObject removed = (EObject) notification.getOldValue();
156:                                 removed.eAdapters().remove(dependantAdapter);
157:                                 break;
158:                         default:
159:                                 return; // No notification
160:                         }
161:                         notifyDerivedAttributeChange();
162:•                } else if (localFeatures.contains(notification.getFeature())) {
163:                         notifyDerivedAttributeChange();
164:                 }
165:         }
166:
167:         private void notifyDerivedAttributeChange() {
168:•                if (source.eNotificationRequired()) {
169:                         source.eNotify(new ENotificationImpl(source, Notification.SET,
170:                                 derivedFeature, null, source.eGet(derivedFeature, true,
171:                                         true)));
172:                 }
173:         }
174: }