Skip to content

Package: EMFFormsViewServiceManagerImpl

EMFFormsViewServiceManagerImpl

nameinstructionbranchcomplexitylinemethod
EMFFormsViewServiceManagerImpl()
M: 0 C: 23
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
addEMFFormsScopedServiceProvider(EMFFormsViewServiceFactory)
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%
createGlobalImmediateService(Class, EMFFormsViewContext)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
createGlobalLazyService(Class, EMFFormsViewContext)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
createLocalImmediateService(Class, EMFFormsViewContext)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
createLocalLazyService(Class, EMFFormsViewContext)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getAllGlobalImmediateServiceTypes()
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%
getAllLocalImmediateServiceTypes()
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%
getAllTypes(Collection)
M: 0 C: 45
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
getServiceOptional(Class, Map, EMFFormsViewContext)
M: 0 C: 14
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getServiceProviderMap(EMFFormsViewServiceFactory)
M: 5 C: 44
90%
M: 3 C: 13
81%
M: 3 C: 6
67%
M: 2 C: 12
86%
M: 0 C: 1
100%
removeEMFFormsScopedServiceProvider(EMFFormsViewServiceFactory)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2015 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.emfforms.internal.core.services.scoped;
15:
16: import java.util.Collection;
17: import java.util.Comparator;
18: import java.util.LinkedHashMap;
19: import java.util.LinkedHashSet;
20: import java.util.Map;
21: import java.util.Set;
22: import java.util.SortedSet;
23: import java.util.TreeSet;
24:
25: import org.eclipse.emfforms.common.Optional;
26: import org.eclipse.emfforms.spi.core.services.view.EMFFormsViewContext;
27: import org.eclipse.emfforms.spi.core.services.view.EMFFormsViewServiceFactory;
28: import org.eclipse.emfforms.spi.core.services.view.EMFFormsViewServiceManager;
29: import org.eclipse.emfforms.spi.core.services.view.EMFFormsViewServicePolicy;
30: import org.eclipse.emfforms.spi.core.services.view.EMFFormsViewServiceScope;
31: import org.osgi.service.component.annotations.Component;
32: import org.osgi.service.component.annotations.Reference;
33: import org.osgi.service.component.annotations.ReferenceCardinality;
34: import org.osgi.service.component.annotations.ReferencePolicy;
35:
36: /**
37: * The implementation of the EMFFormsScopedServicesFactory.
38: *
39: * @author Eugen Neufeld
40: *
41: */
42: @Component
43: public class EMFFormsViewServiceManagerImpl implements EMFFormsViewServiceManager {
44:
45:         private final Map<Class<?>, EMFFormsViewServiceFactory<?>> localImmediateMap = new LinkedHashMap<Class<?>, EMFFormsViewServiceFactory<?>>();
46:         private final Map<Class<?>, EMFFormsViewServiceFactory<?>> localLazyMap = new LinkedHashMap<Class<?>, EMFFormsViewServiceFactory<?>>();
47:         private final Map<Class<?>, EMFFormsViewServiceFactory<?>> globalImmediateMap = new LinkedHashMap<Class<?>, EMFFormsViewServiceFactory<?>>();
48:         private final Map<Class<?>, EMFFormsViewServiceFactory<?>> globalLazyMap = new LinkedHashMap<Class<?>, EMFFormsViewServiceFactory<?>>();
49:
50:         /**
51:          * Called by OSGi whenever a new provider is available.
52:          *
53:          * @param provider The newly available provider
54:          */
55:         @Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
56:         protected void addEMFFormsScopedServiceProvider(EMFFormsViewServiceFactory<?> provider) {
57:                 final Map<Class<?>, EMFFormsViewServiceFactory<?>> serviceProviderMap = getServiceProviderMap(provider);
58:                 double currentPrio = Double.NEGATIVE_INFINITY;
59:
60:•                if (serviceProviderMap.containsKey(provider.getType())) {
61:                         currentPrio = serviceProviderMap.get(provider.getType()).getPriority();
62:                 }
63:
64:•                if (currentPrio < provider.getPriority()) {
65:                         serviceProviderMap.put(provider.getType(), provider);
66:                 }
67:         }
68:
69:         /**
70:          * Called by OSGi whenever a provider is removed.
71:          *
72:          * @param provider The removed provider
73:          */
74:         protected void removeEMFFormsScopedServiceProvider(EMFFormsViewServiceFactory<?> provider) {
75:                 getServiceProviderMap(provider).remove(provider.getType());
76:         }
77:
78:         private Map<Class<?>, EMFFormsViewServiceFactory<?>> getServiceProviderMap(
79:                 EMFFormsViewServiceFactory<?> provider) {
80:•                if (provider.getPolicy() == EMFFormsViewServicePolicy.IMMEDIATE
81:•                        && provider.getScope() == EMFFormsViewServiceScope.GLOBAL) {
82:                         return globalImmediateMap;
83:•                } else if (provider.getPolicy() == EMFFormsViewServicePolicy.LAZY
84:•                        && provider.getScope() == EMFFormsViewServiceScope.GLOBAL) {
85:                         return globalLazyMap;
86:•                } else if (provider.getPolicy() == EMFFormsViewServicePolicy.IMMEDIATE
87:•                        && provider.getScope() == EMFFormsViewServiceScope.LOCAL) {
88:                         return localImmediateMap;
89:•                } else if (provider.getPolicy() == EMFFormsViewServicePolicy.LAZY
90:•                        && provider.getScope() == EMFFormsViewServiceScope.LOCAL) {
91:                         return localLazyMap;
92:                 }
93:                 throw new IllegalStateException(
94:                         "This should never be reached as we check for all possible cases before.s Only when there is one more combination of policy and scope that we missed this can happen!"); //$NON-NLS-1$
95:         }
96:
97:         @SuppressWarnings("unchecked")
98:         private <T> Optional<T> getServiceOptional(Class<T> type,
99:                 Map<Class<?>, EMFFormsViewServiceFactory<?>> classFactoryMap, EMFFormsViewContext emfFormsViewContext) {
100:                 final EMFFormsViewServiceFactory<?> serviceProvider = classFactoryMap.get(type);
101:•                if (serviceProvider != null) {
102:                         return (Optional<T>) Optional.ofNullable(serviceProvider.createService(emfFormsViewContext));
103:                 }
104:                 return Optional.empty();
105:         }
106:
107:         /**
108:          * {@inheritDoc}
109:          *
110:          * @see org.eclipse.emfforms.spi.core.services.view.EMFFormsViewServiceManager#createLocalImmediateService(java.lang.Class,EMFFormsViewContext)
111:          */
112:         @Override
113:         public <T> Optional<T> createLocalImmediateService(Class<T> type, EMFFormsViewContext emfFormsViewContext) {
114:                 return getServiceOptional(type, localImmediateMap, emfFormsViewContext);
115:         }
116:
117:         /**
118:          * {@inheritDoc}
119:          *
120:          * @see org.eclipse.emfforms.spi.core.services.view.EMFFormsViewServiceManager#createLocalLazyService(java.lang.Class,EMFFormsViewContext)
121:          */
122:         @Override
123:         public <T> Optional<T> createLocalLazyService(Class<T> type, EMFFormsViewContext emfFormsViewContext) {
124:                 return getServiceOptional(type, localLazyMap, emfFormsViewContext);
125:         }
126:
127:         /**
128:          * {@inheritDoc}
129:          *
130:          * @see org.eclipse.emfforms.spi.core.services.view.EMFFormsViewServiceManager#createGlobalImmediateService(java.lang.Class,EMFFormsViewContext)
131:          */
132:         @Override
133:         public <T> Optional<T> createGlobalImmediateService(Class<T> type, EMFFormsViewContext emfFormsViewContext) {
134:                 return getServiceOptional(type, globalImmediateMap, emfFormsViewContext);
135:         }
136:
137:         /**
138:          * {@inheritDoc}
139:          *
140:          * @see org.eclipse.emfforms.spi.core.services.view.EMFFormsViewServiceManager#createGlobalLazyService(java.lang.Class,EMFFormsViewContext)
141:          */
142:         @Override
143:         public <T> Optional<T> createGlobalLazyService(Class<T> type, EMFFormsViewContext emfFormsViewContext) {
144:                 return getServiceOptional(type, globalLazyMap, emfFormsViewContext);
145:         }
146:
147:         private Set<Class<?>> getAllTypes(Collection<EMFFormsViewServiceFactory<?>> factorySet) {
148:                 final SortedSet<EMFFormsViewServiceFactory<?>> sortedFactories = new TreeSet<EMFFormsViewServiceFactory<?>>(
149:                         new Comparator<EMFFormsViewServiceFactory<?>>() {
150:
151:                                 @Override
152:                                 public int compare(EMFFormsViewServiceFactory<?> factory0, EMFFormsViewServiceFactory<?> factory1) {
153:                                         final int result = Double.compare(factory0.getPriority(), factory1.getPriority());
154:                                         if (result == 0) {
155:                                                 return 1;
156:                                         }
157:                                         return result;
158:                                 }
159:                         });
160:•                for (final EMFFormsViewServiceFactory<?> factory : factorySet) {
161:                         sortedFactories.add(factory);
162:                 }
163:                 final Set<Class<?>> result = new LinkedHashSet<Class<?>>();
164:•                for (final EMFFormsViewServiceFactory<?> factory : sortedFactories) {
165:                         result.add(factory.getType());
166:                 }
167:                 return result;
168:         }
169:
170:         /**
171:          * {@inheritDoc}
172:          *
173:          * @see org.eclipse.emfforms.spi.core.services.view.EMFFormsViewServiceManager#getAllGlobalImmediateServiceTypes()
174:          */
175:         @Override
176:         public Set<Class<?>> getAllGlobalImmediateServiceTypes() {
177:                 // return globalImmediateMap.keySet();
178:                 return getAllTypes(globalImmediateMap.values());
179:         }
180:
181:         /**
182:          * {@inheritDoc}
183:          *
184:          * @see org.eclipse.emfforms.spi.core.services.view.EMFFormsViewServiceManager#getAllLocalImmediateServiceTypes()
185:          */
186:         @Override
187:         public Set<Class<?>> getAllLocalImmediateServiceTypes() {
188:                 // return localImmediateMap.keySet();
189:                 return getAllTypes(localImmediateMap.values());
190:         }
191:
192: }