aboutsummaryrefslogtreecommitdiffstats
path: root/bundle-plugin/src/main/java/com/yahoo/container/plugin/osgi/ExportPackages.java
blob: b6e54dbe94caf72c067fac17378fcc7099da845b (plain) (blame)
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.plugin.osgi;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Tony Vaagenes
 * @author ollivir
 */
public class ExportPackages {
    public static class Export {
        private final List<String> packageNames;
        private final List<Parameter> parameters;

        public Export(List<String> packageNames, List<Parameter> parameters) {
            this.packageNames = packageNames;
            this.parameters = parameters;
        }

        public Optional<String> version() {
            for (Parameter par : parameters) {
                if ("version".equals(par.getName())) {
                    return Optional.of(par.getValue());
                }
            }
            return Optional.empty();
        }

        public List<String> getPackageNames() {
            return packageNames;
        }

        public List<Parameter> getParameters() {
            return parameters;
        }

    }

    public static class Parameter {
        private final String name;
        private final String value;

        public Parameter(String name, String value) {
            this.name = name;
            this.value = value;
        }

        public String getName() {
            return name;
        }

        public String getValue() {
            return value;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Parameter parameter = (Parameter) o;
            return Objects.equals(name, parameter.name) && Objects.equals(value, parameter.value);
        }

        @Override
        public int hashCode() {
            return Objects.hash(name, value);
        }
    }

    public static Set<String> packageNames(Collection<Export> exports) {
        return exports.stream()
                .map(Export::getPackageNames)
                .flatMap(Collection::stream)
                .collect(Collectors.toSet());
    }

    public static Map<String, Export> exportsByPackageName(Collection<Export> exports) {
        Map<String, Export> ret = new HashMap<>();
        for (Export export : exports) {
            for (String packageName : export.getPackageNames()) {
                //ensure that earlier exports of a package overrides later exports.
                ret.computeIfAbsent(packageName, ign -> export);
            }
        }
        return ret;
    }
}