-
Notifications
You must be signed in to change notification settings - Fork 3
/
BasicLookupEngine.java
149 lines (130 loc) · 4.07 KB
/
BasicLookupEngine.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*******************************************************************************
* Copyright (c) 2015 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.acceleo.query.runtime.lookup.basic;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.acceleo.query.runtime.ILookupEngine;
import org.eclipse.acceleo.query.runtime.IReadOnlyQueryEnvironment;
import org.eclipse.acceleo.query.runtime.IService;
import org.eclipse.acceleo.query.runtime.ServiceRegistrationResult;
import org.eclipse.acceleo.query.validation.type.IType;
/**
* Lookup engine are used to retrieve services from a name and a set of arguments.
*
* @author <a href="mailto:romain.guider@obeo.fr">Romain Guider</a>
*/
public class BasicLookupEngine implements ILookupEngine {
/**
* The {@link IReadOnlyQueryEnvironment}.
*/
protected final IReadOnlyQueryEnvironment queryEnvironment;
/**
* The {@link ServiceStore}.
*/
private final ServiceStore services;
/**
* Constructor.
*
* @param queryEnvironment
* the {@link IReadOnlyQueryEnvironment}
* @since 4.0.0
*/
public BasicLookupEngine(IReadOnlyQueryEnvironment queryEnvironment) {
this.queryEnvironment = queryEnvironment;
this.services = new ServiceStore(queryEnvironment);
}
/**
* Gets the {@link ServiceStore}.
*
* @return the {@link ServiceStore}
*/
protected ServiceStore getServices() {
return services;
}
/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.runtime.ILookupEngine#getRegisteredServices()
*/
public Set<IService<?>> getRegisteredServices() {
Set<IService<?>> result = services.getServices();
return result;
}
@Override
public IService<?> lookup(String name, IType[] argumentTypes) {
List<IService<?>> multiMethod = services.getMultiService(name, argumentTypes.length);
if (multiMethod == null) {
return null;
} else {
IService<?> result = null;
for (IService<?> service : multiMethod) {
if (service.matches(queryEnvironment, argumentTypes)) {
if (result == null || service.getPriority() > result.getPriority() || (service
.getPriority() == result.getPriority() && service.isLowerOrEqualParameterTypes(
queryEnvironment, result))) {
result = service;
}
}
}
return result;
}
}
/**
* Registers the given {@link IService}.
*
* @param service
* the {@link IService} to register
* @return the {@link ServiceRegistrationResult}
*/
public ServiceRegistrationResult registerService(IService<?> service) {
final ServiceRegistrationResult result = services.add(service);
return result;
}
/**
* Removes the given {@link IService}.
*
* @param service
* the {@link IService} to remove.
* @return the removed {@link IService} if any, <code>null</code> otherwise
*/
public IService<?> removeService(IService<?> service) {
final IService<?> result = services.remove(service);
return result;
}
@Override
public Set<IService<?>> getServices(Set<IType> receiverTypes) {
final Set<IService<?>> result = new LinkedHashSet<IService<?>>();
final Set<IService<?>> storedServices = services.getServices();
for (IType type : receiverTypes) {
if (type != null) {
for (IService<?> service : storedServices) {
for (IType parameterType : service.getParameterTypes(queryEnvironment).get(0)) {
if (parameterType.isAssignableFrom(type)) {
result.add(service);
break;
}
}
}
}
}
return result;
}
/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.runtime.ILookupEngine#isRegisteredService(org.eclipse.acceleo.query.runtime.IService)
*/
@Override
public boolean isRegisteredService(IService<?> service) {
return services.isRegistered(service);
}
}