1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package io.earcam.instrumental.archive.osgi.auto;
20
21 import java.util.ArrayList;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.stream.Collectors;
25
26 import io.earcam.instrumental.archive.osgi.PackageBundleMapper;
27 import io.earcam.instrumental.module.osgi.BundleInfo;
28 import io.earcam.instrumental.module.osgi.Clause;
29 import io.earcam.instrumental.module.osgi.ClauseParameters.ClauseParameter;
30
31
32
33
34
35
36
37 public abstract class AbstractPackageBundleMapper implements PackageBundleMapper {
38
39
40
41
42
43
44
45
46 protected abstract List<BundleInfo> bundles();
47
48
49
50
51
52
53
54
55
56 protected List<Clause> allAvailableExports()
57 {
58 return bundles().stream()
59 .map(BundleInfo::exportPackage)
60 .flatMap(List::stream)
61 .collect(Collectors.toList());
62 }
63
64
65 @Override
66 public List<Clause> importsFor(Iterator<String> requiredPackages)
67 {
68 List<Clause> availableExports = allAvailableExports();
69 List<Clause> matchedExports = new ArrayList<>();
70
71 while(requiredPackages.hasNext()) {
72 String requiredPackage = requiredPackages.next();
73
74 if(requiredPackage.startsWith("java.")) {
75 requiredPackages.remove();
76 continue;
77 }
78
79 for(Clause export : availableExports) {
80
81 if(export.uniqueNames().contains(requiredPackage)) {
82
83 String version = export.parameters().attributeOrDefault("version", "0.0.0");
84 matchedExports.add(new Clause(requiredPackage, ClauseParameter.attribute("version", version)));
85 requiredPackages.remove();
86 break;
87 }
88 }
89 }
90 return matchedExports;
91 }
92 }