Package: ServiceStore

ServiceStore

nameinstructionbranchcomplexitylinemethod
ServiceStore(IReadOnlyQueryEnvironment)
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
add(IService)
M: 110 C: 0
0%
M: 22 C: 0
0%
M: 12 C: 0
0%
M: 22 C: 0
0%
M: 1 C: 0
0%
getMultiService(String, int)
M: 16 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getOrCreateMultiService(IService)
M: 44 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
getServices()
M: 35 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
isRegistered(IService)
M: 21 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
remove(IService)
M: 56 C: 0
0%
M: 14 C: 0
0%
M: 8 C: 0
0%
M: 17 C: 0
0%
M: 1 C: 0
0%
size()
M: 34 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2015 Obeo.
3: * All rights reserved. This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v1.0
5: * which accompanies this distribution, and is available at
6: * http://www.eclipse.org/legal/epl-v10.html
7: *
8: * Contributors:
9: * Obeo - initial API and implementation
10: *******************************************************************************/
11: package org.eclipse.acceleo.query.runtime.lookup.basic;
12:
13: import java.util.ArrayList;
14: import java.util.HashMap;
15: import java.util.LinkedHashSet;
16: import java.util.List;
17: import java.util.Map;
18: import java.util.Set;
19:
20: import org.eclipse.acceleo.query.runtime.IReadOnlyQueryEnvironment;
21: import org.eclipse.acceleo.query.runtime.IService;
22: import org.eclipse.acceleo.query.runtime.ServiceRegistrationResult;
23:
24: /**
25: * A store for {@link IService}.
26: *
27: * @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
28: */
29: public class ServiceStore {
30:
31:         /**
32:          * The {@link IReadOnlyQueryEnvironment}.
33:          */
34:         private final IReadOnlyQueryEnvironment queryEnvironment;
35:
36:         /**
37:          * Maps of multiservices : {@link IService#getNumberOfParameters() number of parameters} ->
38:          * {@link IService#getName() name} -> {@link IService} ordered by descending
39:          * {@link IService#getPriority() priority}.
40:          */
41:         private final Map<Integer, Map<String, List<IService>>> services = new HashMap<Integer, Map<String, List<IService>>>();
42:
43:         /**
44:          * Constructor.
45:          *
46:          * @param queryEnvironment
47:          * the {@link IReadOnlyQueryEnvironment}
48:          */
49:         public ServiceStore(IReadOnlyQueryEnvironment queryEnvironment) {
50:                 this.queryEnvironment = queryEnvironment;
51:         }
52:
53:         /**
54:          * Gets the {@link List} of {@link IService} for the given {@link IService#getName() name} and number of
55:          * {@link IService#getNumberOfParameters() parameters}.
56:          *
57:          * @param methodName
58:          * the {@link IService#getName() name}
59:          * @param argc
60:          * the number of {@link IService#getNumberOfParameters() parameters}
61:          * @return the {@link List} of {@link IService} for the given {@link IService#getName() name} and number
62:          * of {@link IService#getNumberOfParameters() parameters}
63:          */
64:
65:         public List<IService> getMultiService(String methodName, int argc) {
66:                 Map<String, List<IService>> argcServices = services.get(argc);
67:•                if (argcServices == null) {
68:                         return null;
69:                 } else {
70:                         return argcServices.get(methodName);
71:                 }
72:         }
73:
74:         /**
75:          * Gets or creates the {@link List} of {@link IService} to store the given {@link IService}.
76:          *
77:          * @param service
78:          * the {@link IService} to store
79:          * @return the {@link List} of {@link IService} to store the given {@link IService}
80:          */
81:         private List<IService> getOrCreateMultiService(IService service) {
82:                 final int argc = service.getNumberOfParameters();
83:                 final String serviceName = service.getName();
84:                 Map<String, List<IService>> argcServices = services.get(argc);
85:•                if (argcServices == null) {
86:                         argcServices = new HashMap<String, List<IService>>();
87:                         services.put(argc, argcServices);
88:                 }
89:                 List<IService> result = argcServices.get(serviceName);
90:•                if (result == null) {
91:                         result = new ArrayList<IService>();
92:                         argcServices.put(serviceName, result);
93:                 }
94:                 return result;
95:         }
96:
97:         /**
98:          * Gets the number of stored {@link IService}.
99:          *
100:          * @return the number of stored {@link IService}
101:          */
102:         public int size() {
103:                 int result = 0;
104:
105:•                for (Map<String, List<IService>> byArgC : services.values()) {
106:•                        for (List<IService> byName : byArgC.values()) {
107:                                 result += byName.size();
108:                         }
109:                 }
110:
111:                 return result;
112:         }
113:
114:         /**
115:          * Removes the given {@link IService}.
116:          *
117:          * @param service
118:          * the {@link IService} to remove
119:          * @return the removed {@link IService} if any, <code>null</code> otherwise
120:          */
121:         public IService remove(IService service) {
122:                 final IService result;
123:
124:•                if (service == null) {
125:                         return null;
126:                 }
127:
128:                 final int argc = service.getNumberOfParameters();
129:                 final Map<String, List<IService>> argcServices = services.get(argc);
130:•                if (argcServices != null) {
131:                         final String serviceName = service.getName();
132:                         final List<IService> servicesList = argcServices.get(serviceName);
133:•                        if (servicesList != null && servicesList.remove(service)) {
134:                                 result = service;
135:•                                if (servicesList.isEmpty() && argcServices.remove(serviceName) != null
136:•                                                && argcServices.isEmpty()) {
137:                                         services.remove(argc);
138:                                 }
139:                         } else {
140:                                 result = null;
141:                         }
142:                 } else {
143:                         result = null;
144:                 }
145:
146:                 return result;
147:         }
148:
149:         /**
150:          * Adds the given {@link IService} to the store.
151:          *
152:          * @param service
153:          * the {@link IService} to add
154:          * @return the {@link ServiceRegistrationResult}
155:          */
156:         public ServiceRegistrationResult add(IService service) {
157:                 final ServiceRegistrationResult result = new ServiceRegistrationResult();
158:
159:                 final List<IService> multiService = getOrCreateMultiService(service);
160:•                for (IService existingService : multiService) {
161:•                        if (service.getPriority() > existingService.getPriority()) {
162:•                                if (service.isLowerOrEqualParameterTypes(queryEnvironment, existingService)
163:•                                                || existingService.isLowerOrEqualParameterTypes(queryEnvironment, service)) {
164:                                         result.addMasked(service, existingService);
165:                                 }
166:•                        } else if (service.getPriority() == existingService.getPriority()) {
167:•                                if (service.isEqualParameterTypes(queryEnvironment, existingService)) {
168:                                         result.addDuplicated(service, existingService);
169:•                                } else if (service.isLowerOrEqualParameterTypes(queryEnvironment, existingService)) {
170:                                         result.addMasked(service, existingService);
171:•                                } else if (existingService.isLowerOrEqualParameterTypes(queryEnvironment, service)) {
172:                                         result.addIsMaskedBy(service, existingService);
173:                                 }
174:                         } else {
175:•                                if (service.isLowerOrEqualParameterTypes(queryEnvironment, existingService)
176:•                                                || existingService.isLowerOrEqualParameterTypes(queryEnvironment, service)) {
177:                                         result.addIsMaskedBy(service, existingService);
178:                                 }
179:                         }
180:                 }
181:
182:•                if (!multiService.contains(service)) {
183:                         result.getRegistered().add(service);
184:                         multiService.add(service);
185:                 }
186:
187:                 return result;
188:         }
189:
190:         /**
191:          * Gets the {@link Set} of {@link ServiceStore#add(IService) stored} {@link IService}.
192:          *
193:          * @return the {@link Set} of {@link ServiceStore#add(IService) stored} {@link IService}
194:          */
195:         public Set<IService> getServices() {
196:                 final Set<IService> result = new LinkedHashSet<IService>();
197:
198:•                for (Map<String, List<IService>> byArgC : services.values()) {
199:•                        for (List<IService> byName : byArgC.values()) {
200:                                 result.addAll(byName);
201:                         }
202:                 }
203:
204:                 return result;
205:         }
206:
207:         /**
208:          * Tells if the given {@link IService} is registered.
209:          *
210:          * @param service
211:          * the {@link IService} to check
212:          * @return <code>true</code> if the given {@link IService} is registered, <code>false</code> otherwise
213:          */
214:         public boolean isRegistered(IService service) {
215:•                if (service == null) {
216:                         return false;
217:                 }
218:
219:                 final List<IService> multiService = getMultiService(service.getName(), service
220:                                 .getNumberOfParameters());
221:
222:•                return multiService != null && multiService.contains(service);
223:         }
224: }