Debian Codegraph @

maven-packager-utils/src/main/java/org/debian/maven/packager/DebianDependency.java

raw
1 package org.debian.maven.packager;
2
3 import java.util.regex.Pattern;
4
5 /**
6 * A dependency on a Debian package, with an optional minimum version.
7 *
8 * @since 1.8
9 */
10 public class DebianDependency implements Comparable<DebianDependency> {
11
12 /**
13 * Pattern for valid package names according to the Debian Policy 5.6.1.
14 *
15 * Package names must consist only of lower case letters (a-z), digits
16 * (0-9), plus (+) and minus (-) signs, and periods (.). They must be at
17 * least two characters long and must start with an alphanumeric character.
18 */
19 private static final Pattern DEBIAN_PACKAGE_NAME_PATTERN = Pattern.compile("^[a-z0-9][a-z0-9+-.]+$");
20
21 /** The name of the Debian package */
22 private final String packageName;
23
24 /** The minimum version required (blank if not specified) */
25 private final String minimumVersion;
26
27 /**
28 * Creates a dependency on the specified package.
29 *
30 * @param packageName the name of the Debian package
31 * @param minimumVersion the minimum version required, empty if none
32 * @throws IllegalArgumentException if the package name or the minimum version is not valid
33 */
34 public DebianDependency(String packageName, String minimumVersion) throws IllegalArgumentException {
35 // check the validity of the package name
36 if (packageName == null || !DEBIAN_PACKAGE_NAME_PATTERN.matcher(packageName).matches()) {
37 throw new IllegalArgumentException("Invalid package name: " + packageName);
38 }
39
40 if (minimumVersion == null) {
41 throw new IllegalArgumentException("Invalid minimum version: " + minimumVersion);
42 }
43
44 this.packageName = packageName;
45 this.minimumVersion = minimumVersion;
46 }
47
48 /**
49 * Creates a non version dependency on the specified package.
50 *
51 * @param packageName the name of the Debian package
52 * @throws IllegalArgumentException if the package name is not valid
53 */
54 public DebianDependency(String packageName) throws IllegalArgumentException {
55 this(packageName, "");
56 }
57
58 /**
59 * Returns the name of the Debian package.
60 */
61 public String getPackageName() {
62 return packageName;
63 }
64
65 /**
66 * Returns the string representation of the dependency using the syntax
67 * of the Debian control files:
68 *
69 * <pre>
70 * foo (>= 1.0)
71 * </pre>
72 */
73 public String toString() {
74 return minimumVersion.isEmpty() ? packageName : packageName + " (>= " + minimumVersion + ")";
75 }
76
77 @Override
78 public int hashCode() {
79 return packageName.hashCode();
80 }
81
82 @Override
83 public boolean equals(Object obj) {
84 if (this == obj) {
85 return true;
86 }
87 if (!(obj instanceof DebianDependency)) {
88 return false;
89 }
90
91 DebianDependency that = (DebianDependency) obj;
92
93 if (!minimumVersion.equals(that.minimumVersion)) {
94 return false;
95 }
96 if (!packageName.equals(that.packageName)) {
97 return false;
98 }
99
100 return true;
101 }
102
103 @Override
104 public int compareTo(DebianDependency other) {
105 if (equals(other)) {
106 return 0;
107 }
108
109 return toString().compareTo(other.toString());
110 }
111 }