blob: 17ba38927a965d7b0f9edc9aecfd0c2f4c940008 (
plain)
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
|
package ATCCodes.services;
import ATCCodes.AgentCode;
import org.apache.commons.lang.SystemUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ResourceLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* Abstract {@link AgentCodeService} for use with files that will load information into memory
*
* @author Paul-Christian Volkmer
* @since 0.1.0
*/
public abstract class FileBasedAgentCodeService implements AgentCodeService {
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
protected final List<AgentCode> codeList = new ArrayList<>();
FileBasedAgentCodeService(final ResourceLoader resourceLoader) {
this.codeList.addAll(parseFile(resourceLoader));
}
static String getFilePath(final String filename) {
String pluginPathPart = "onkostar/files/onkostar/plugins/onkostar-plugin-atccodes";
if (SystemUtils.IS_OS_WINDOWS) {
return String.format("file:///c:/%s/%s", pluginPathPart, filename);
} else if (SystemUtils.IS_OS_LINUX) {
return String.format("file:///opt/%s/%s", pluginPathPart, filename);
}
return filename;
}
protected abstract List<AgentCode> parseFile(final ResourceLoader resourceLoader);
/**
* Queries source for agents code starting with or name containing query string.
* If size is zero, all available results will be returned.
*
* @param query The query string
* @param size Maximal amount of responses
* @return A list with agent codes
*/
@Override
public List<AgentCode> findAgentCodes(final String query, final int size) {
var resultStream = this.codeList.stream().filter(agentCode ->
agentCode.getCode().toLowerCase().startsWith(query.toLowerCase())
|| agentCode.getName().toLowerCase().contains(query.toLowerCase())
);
if (size > 0) {
return resultStream.limit(size).collect(Collectors.toList());
}
return resultStream.collect(Collectors.toList());
}
}
|