Skip to content

Package: LeafConditionSettingIterator

LeafConditionSettingIterator

nameinstructionbranchcomplexitylinemethod
LeafConditionSettingIterator(LeafCondition, EObject, boolean)
M: 0 C: 88
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 22
100%
M: 0 C: 1
100%
dispose()
M: 1 C: 31
97%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 1 C: 9
90%
M: 0 C: 1
100%
getBundleContext()
M: 0 C: 12
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getChildIterator(EStructuralFeature.Setting)
M: 0 C: 38
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
getDatabinding()
M: 0 C: 20
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getReportService()
M: 0 C: 20
100%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 4
100%
M: 0 C: 1
100%
getUsedValueDomainModelReferences()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
hasNext()
M: 0 C: 21
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
isDisposed()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
next()
M: 0 C: 24
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
organizeStack()
M: 0 C: 156
100%
M: 0 C: 16
100%
M: 0 C: 9
100%
M: 0 C: 38
100%
M: 0 C: 1
100%
remove()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2016 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: * jfaltermeier - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.spi.rule.model.impl;
15:
16: import java.util.ArrayList;
17: import java.util.Collection;
18: import java.util.Collections;
19: import java.util.Iterator;
20: import java.util.LinkedHashMap;
21: import java.util.LinkedHashSet;
22: import java.util.LinkedList;
23: import java.util.List;
24: import java.util.Map;
25: import java.util.Set;
26: import java.util.Stack;
27:
28: import org.eclipse.emf.ecore.EObject;
29: import org.eclipse.emf.ecore.EStructuralFeature;
30: import org.eclipse.emf.ecore.EStructuralFeature.Setting;
31: import org.eclipse.emf.ecore.util.EcoreUtil;
32: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
33: import org.eclipse.emf.ecp.view.spi.rule.model.LeafCondition;
34: import org.eclipse.emfforms.spi.common.report.AbstractReport;
35: import org.eclipse.emfforms.spi.common.report.ReportService;
36: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
37: import org.eclipse.emfforms.spi.core.services.databinding.emf.EMFFormsDatabindingEMF;
38: import org.osgi.framework.BundleContext;
39: import org.osgi.framework.FrameworkUtil;
40: import org.osgi.framework.ServiceReference;
41:
42: /**
43: * Iterator that returns all settings of the condition's {@link LeafCondition#getDomainModelReference()} and
44: * {@link LeafCondition#getValueDomainModelReference()}.
45: *
46: * @author jfaltermeier
47: * @since 1.5
48: *
49: */
50: public class LeafConditionSettingIterator implements Iterator<Setting> {
51:
52:         /**
53:          * Map from a setting iterator to the children of the last returned setting of the iterator.
54:          */
55:         private final Map<Iterator<Setting>, Iterator<EObject>> iteratorToCurrentChildrenMap;
56:         private int expectedStackSize;
57:         private final Stack<Iterator<Setting>> iterators;
58:         private final boolean includeNonValueRelatedSettings;
59:         private final Set<VDomainModelReference> dmrs;
60:
61:         private final List<Setting> nonValueRelSettings = new LinkedList<EStructuralFeature.Setting>();
62:         private final LeafCondition condition;
63:
64:         private BundleContext bundleContext;
65:         private EMFFormsDatabindingEMF databinding;
66:         private ServiceReference<EMFFormsDatabindingEMF> databindingServiceReference;
67:         private ReportService reportService;
68:         private ServiceReference<ReportService> reportServiceReference;
69:
70:         private boolean isDisposed;
71:
72:         /**
73:          * Constructs a new {@link LeafConditionSettingIterator}.
74:          *
75:          * @param condition the leaf condition
76:          * @param domainModel The root domain object of the given {@link LeafCondition}
77:          * @param includeNonValueRelatedSettings whether to include non value related settings
78:          * @since 1.9
79:          */
80:         public LeafConditionSettingIterator(LeafCondition condition, EObject domainModel,
81:                 boolean includeNonValueRelatedSettings) {
82:                 isDisposed = false;
83:
84:                 this.condition = condition;
85:                 dmrs = new LinkedHashSet<VDomainModelReference>();
86:                 this.includeNonValueRelatedSettings = includeNonValueRelatedSettings;
87:                 iteratorToCurrentChildrenMap = new LinkedHashMap<Iterator<Setting>, Iterator<EObject>>();
88:                 expectedStackSize = 1;
89:•                if (condition.getValueDomainModelReference() != null) {
90:                         expectedStackSize = expectedStackSize + 1;
91:                 }
92:                 iterators = new Stack<Iterator<Setting>>();
93:                 final VDomainModelReference dmr = condition.getDomainModelReference();
94:•                if (dmr != null) {
95:                         try {
96:                                 final Setting setting = getDatabinding().getSetting(dmr, domainModel);
97:                                 final Iterator<Setting> iterator = Collections.singleton(setting).iterator();
98:                                 iterators.push(iterator);
99:                         } catch (final DatabindingFailedException ex) {
100:                                 getReportService().report(new AbstractReport(ex,
101:                                         String.format("Could not get the setting iterator for DMR: %s and domain model: %s", //$NON-NLS-1$
102:                                                 condition.getDomainModelReference(), domainModel)));
103:                         }
104:                 }
105:                 // TODO: remove after segments are introduced.
106:                 // iterators.push(condition.getDomainModelReference().getIterator());
107:
108:                 organizeStack();
109:         }
110:
111:         /**
112:          * @return The {@link EMFFormsDatabindingEMF} service.
113:          *
114:          */
115:         private EMFFormsDatabindingEMF getDatabinding() {
116:•                if (databinding == null) {
117:                         databindingServiceReference = getBundleContext().getServiceReference(EMFFormsDatabindingEMF.class);
118:                         databinding = getBundleContext().getService(databindingServiceReference);
119:                 }
120:                 return databinding;
121:         }
122:
123:         /**
124:          * @return The {@link ReportService}
125:          */
126:         private ReportService getReportService() {
127:•                if (reportService == null) {
128:                         reportServiceReference = getBundleContext().getServiceReference(ReportService.class);
129:                         reportService = getBundleContext().getService(reportServiceReference);
130:                 }
131:                 return reportService;
132:         }
133:
134:         /**
135:          * @return The {@link BundleContext} for this class
136:          */
137:         private BundleContext getBundleContext() {
138:•                if (bundleContext == null) {
139:                         bundleContext = FrameworkUtil.getBundle(getClass()).getBundleContext();
140:                 }
141:                 return bundleContext;
142:         }
143:
144:         private void organizeStack() {
145:•                if (iterators.size() == 0) {
146:                         // end
147:                         return;
148:                 }
149:
150:•                if (iterators.size() == expectedStackSize && iterators.peek().hasNext()) {
151:                         // all is ok
152:                         return;
153:                 }
154:
155:•                if (iterators.size() == expectedStackSize) {
156:                         // top iterator has reached end.
157:                         // since top iterator does not have entries in iteratorToCurrentChildrenMap,
158:                         // this is treated as a special case
159:                         iterators.pop();
160:                         organizeStack();
161:                         return;
162:                 }
163:
164:                 // iterators.size() != expectedStackSize
165:                 final Iterator<EObject> domainIterator = iteratorToCurrentChildrenMap.get(iterators.peek());
166:•                if (domainIterator == null) {
167:                         // move on to next setting
168:•                        if (iterators.peek().hasNext()) {
169:                                 final Setting setting = iterators.peek().next();
170:•                                if (includeNonValueRelatedSettings) {
171:                                         nonValueRelSettings.add(setting);
172:                                 }
173:                                 final Iterator<EObject> childIterator = getChildIterator(setting);
174:                                 iteratorToCurrentChildrenMap.put(iterators.peek(), childIterator);
175:                                 organizeStack();
176:                                 return;
177:                         }
178:
179:                         // top iterator at end, pop
180:                         iterators.pop();
181:                         organizeStack();
182:                         return;
183:                 }
184:
185:•                if (domainIterator.hasNext()) {
186:                         final EObject nextDomain = domainIterator.next();
187:
188:                         try {
189:                                 final Setting setting = getDatabinding().getSetting(condition.getValueDomainModelReference(),
190:                                         nextDomain);
191:                                 final Iterator<Setting> iterator = Collections.singleton(setting).iterator();
192:                                 iterators.push(iterator);
193:                         } catch (final DatabindingFailedException ex) {
194:                                 getReportService().report(new AbstractReport(ex,
195:                                         String.format("Could not get the setting iterator for DMR: %s and domain model: %s", //$NON-NLS-1$
196:                                                 condition.getValueDomainModelReference(), nextDomain)));
197:                         }
198:
199:                         final VDomainModelReference valueDMR = EcoreUtil.copy(condition.getValueDomainModelReference());
200:                         // TODO: remove after segments are introduced.
201:                         // valueDMR.init(nextDomain);
202:
203:                         dmrs.add(valueDMR);
204:
205:                         // TODO: remove after segments are introduced.
206:                         // iterators.push(valueDMR.getIterator());
207:                         organizeStack();
208:                         return;
209:                 }
210:
211:                 // domain iterator at end
212:                 // remove from map
213:                 iteratorToCurrentChildrenMap.remove(iterators.peek());
214:                 organizeStack();
215:                 return;
216:         }
217:
218:         @SuppressWarnings("unchecked")
219:         private Iterator<EObject> getChildIterator(Setting setting) {
220:                 final List<EObject> children = new ArrayList<EObject>();
221:•                if (setting.getEStructuralFeature().isMany()) {
222:                         children.addAll((Collection<? extends EObject>) setting.get(true));
223:                 } else {
224:                         children.add((EObject) setting.get(true));
225:                 }
226:                 final Iterator<EObject> iterator = children.iterator();
227:•                while (iterator.hasNext()) {
228:•                        if (iterator.next() == null) {
229:                                 iterator.remove();
230:                         }
231:                 }
232:                 return children.iterator();
233:         }
234:
235:         /**
236:          * {@inheritDoc}
237:          *
238:          * @see java.util.Iterator#hasNext()
239:          */
240:         @Override
241:         public boolean hasNext() {
242:•                if (includeNonValueRelatedSettings && !nonValueRelSettings.isEmpty()) {
243:                         return true;
244:                 }
245:•                if (iterators.isEmpty()) {
246:                         return false;
247:                 }
248:                 return iterators.peek().hasNext();
249:
250:         }
251:
252:         /**
253:          * {@inheritDoc}
254:          *
255:          * @see java.util.Iterator#next()
256:          */
257:         @Override
258:         public Setting next() {
259:•                if (includeNonValueRelatedSettings && !nonValueRelSettings.isEmpty()) {
260:                         return nonValueRelSettings.remove(0);
261:                 }
262:                 final Setting result = iterators.peek().next();
263:                 organizeStack();
264:                 return result;
265:         }
266:
267:         /**
268:          * {@inheritDoc}
269:          *
270:          * @see java.util.Iterator#remove()
271:          */
272:         @Override
273:         public void remove() {
274:                 throw new UnsupportedOperationException();
275:         }
276:
277:         /**
278:          * <br>
279:          * Returns the used {@link VDomainModelReference VDomainModelReferences}.</br>
280:          * <b>This map will contain all entries when the iterator has reached its end.</b>
281:          *
282:          * @return the map
283:          */
284:         public Set<VDomainModelReference> getUsedValueDomainModelReferences() {
285:                 return dmrs;
286:         }
287:
288:         /**
289:          * Disposes this {@link LeafConditionSettingIterator}.
290:          *
291:          * @since 1.9
292:          */
293:         public void dispose() {
294:•                if (isDisposed) {
295:                         return;
296:                 }
297:                 isDisposed = true;
298:
299:•                if (databindingServiceReference != null) {
300:                         databinding = null;
301:                         getBundleContext().ungetService(databindingServiceReference);
302:                 }
303:•                if (reportServiceReference != null) {
304:                         reportService = null;
305:                         getBundleContext().ungetService(reportServiceReference);
306:                 }
307:
308:         }
309:
310:         /**
311:          * @return <code>true</code> if this {@link LeafConditionSettingIterator} is disposed, <code>false</code> otherwise
312:          * @since 1.9
313:          */
314:         public boolean isDisposed() {
315:                 return isDisposed;
316:         }
317:
318: }