Package: BasicLookupEngine

BasicLookupEngine

nameinstructionbranchcomplexitylinemethod
BasicLookupEngine(IReadOnlyQueryEnvironment)
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%
getRegisteredServices()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getServices()
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%
getServices(Set)
M: 48 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
isRegisteredService(IService)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lookup(String, IType[])
M: 52 C: 0
0%
M: 14 C: 0
0%
M: 8 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
registerService(IService)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
removeService(IService)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 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.LinkedHashSet;
14: import java.util.List;
15: import java.util.Set;
16:
17: import org.eclipse.acceleo.query.runtime.ILookupEngine;
18: import org.eclipse.acceleo.query.runtime.IReadOnlyQueryEnvironment;
19: import org.eclipse.acceleo.query.runtime.IService;
20: import org.eclipse.acceleo.query.runtime.ServiceRegistrationResult;
21: import org.eclipse.acceleo.query.validation.type.IType;
22:
23: /**
24: * Lookup engine are used to retrieve services from a name and a set of arguments.
25: *
26: * @author <a href="mailto:romain.guider@obeo.fr">Romain Guider</a>
27: */
28: public class BasicLookupEngine implements ILookupEngine {
29:
30:         /**
31:          * The {@link IReadOnlyQueryEnvironment}.
32:          */
33:         protected IReadOnlyQueryEnvironment queryEnvironment;
34:
35:         /**
36:          * The {@link ServiceStore}.
37:          */
38:         private final ServiceStore services;
39:
40:         /**
41:          * Constructor.
42:          *
43:          * @param queryEnvironment
44:          * the {@link IReadOnlyQueryEnvironment}
45:          * @since 4.0.0
46:          */
47:         public BasicLookupEngine(IReadOnlyQueryEnvironment queryEnvironment) {
48:                 this.queryEnvironment = queryEnvironment;
49:                 this.services = new ServiceStore(queryEnvironment);
50:         }
51:
52:         /**
53:          * Gets the {@link ServiceStore}.
54:          *
55:          * @return the {@link ServiceStore}
56:          */
57:         protected ServiceStore getServices() {
58:                 return services;
59:         }
60:
61:         /**
62:          * {@inheritDoc}
63:          *
64:          * @see org.eclipse.acceleo.query.runtime.ILookupEngine#getRegisteredServices()
65:          */
66:         public Set<IService> getRegisteredServices() {
67:                 Set<IService> result = services.getServices();
68:
69:                 return result;
70:         }
71:
72:         @Override
73:         public IService lookup(String name, IType[] argumentTypes) {
74:                 List<IService> multiMethod = services.getMultiService(name, argumentTypes.length);
75:•                if (multiMethod == null) {
76:                         return null;
77:                 } else {
78:                         IService result = null;
79:•                        for (IService service : multiMethod) {
80:•                                if (service.matches(queryEnvironment, argumentTypes)) {
81:•                                        if (result == null
82:•                                                        || service.getPriority() > result.getPriority()
83:•                                                        || (service.getPriority() == result.getPriority() && service
84:•                                                                        .isLowerOrEqualParameterTypes(queryEnvironment, result))) {
85:                                                 result = service;
86:                                         }
87:                                 }
88:                         }
89:                         return result;
90:                 }
91:         }
92:
93:         /**
94:          * Registers the given {@link IService}.
95:          *
96:          * @param service
97:          * the {@link IService} to register
98:          * @return the {@link ServiceRegistrationResult}
99:          */
100:         public ServiceRegistrationResult registerService(IService service) {
101:                 final ServiceRegistrationResult result = services.add(service);
102:
103:                 return result;
104:         }
105:
106:         /**
107:          * Removes the given {@link IService}.
108:          *
109:          * @param service
110:          * the {@link IService} to remove.
111:          * @return the removed {@link IService} if any, <code>null</code> otherwise
112:          */
113:         public IService removeService(IService service) {
114:                 final IService result = services.remove(service);
115:
116:                 return result;
117:         }
118:
119:         @Override
120:         public Set<IService> getServices(Set<IType> receiverTypes) {
121:                 final Set<IService> result = new LinkedHashSet<IService>();
122:
123:                 final Set<IService> storedServices = services.getServices();
124:•                for (IType type : receiverTypes) {
125:•                        if (type != null) {
126:•                                for (IService service : storedServices) {
127:•                                        if (service.getParameterTypes(queryEnvironment).get(0).isAssignableFrom(type)) {
128:                                                 result.add(service);
129:                                         }
130:                                 }
131:                         }
132:                 }
133:
134:                 return result;
135:         }
136:
137:         /**
138:          * {@inheritDoc}
139:          *
140:          * @see org.eclipse.acceleo.query.runtime.ILookupEngine#isRegisteredService(org.eclipse.acceleo.query.runtime.IService)
141:          */
142:         @Override
143:         public boolean isRegisteredService(IService service) {
144:                 return services.isRegistered(service);
145:         }
146:
147: }