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

import java.util.Objects;
import java.util.regex.Pattern;

/**
 * @author Tony Vaagenes
 * @author ollivir
 */
public class ExportPackageAnnotation {

    private final int major;
    private final int minor;
    private final int micro;
    private final String qualifier;

    private static final Pattern QUALIFIER_PATTERN = Pattern.compile("[\\p{Alpha}\\p{Digit}_-]*");

    public ExportPackageAnnotation(int major, int minor, int micro, String qualifier) {
        this.major = major;
        this.minor = minor;
        this.micro = micro;
        this.qualifier = qualifier;

        requireNonNegative(major, "major");
        requireNonNegative(minor, "minor");
        requireNonNegative(micro, "micro");
        if (! QUALIFIER_PATTERN.matcher(qualifier).matches()) {
            throw new IllegalArgumentException(
                    exportPackageError(String.format("qualifier must follow the format (alpha|digit|'_'|'-')* but was '%s'.", qualifier)));
        }
    }

    public String osgiVersion() {
        return String.format("%d.%d.%d", major, minor, micro) + (qualifier.isEmpty() ? "" : "." + qualifier);
    }

    private static String exportPackageError(String msg) {
        return "ExportPackage anntotation: " + msg;
    }

    private static void requireNonNegative(int i, String fieldName) {
        if (i < 0) {
            throw new IllegalArgumentException(exportPackageError(String.format("%s must be non-negative but was %d.", fieldName, i)));
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;
        ExportPackageAnnotation that = (ExportPackageAnnotation) o;
        return major == that.major && minor == that.minor && micro == that.micro && Objects.equals(qualifier, that.qualifier);
    }

    @Override
    public int hashCode() {
        return Objects.hash(major, minor, micro, qualifier);
    }

}