1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.util.version;
20
21 import java.util.Objects;
22
23 import org.eclipse.aether.version.Version;
24 import org.eclipse.aether.version.VersionConstraint;
25 import org.eclipse.aether.version.VersionRange;
26
27 import static java.util.Objects.requireNonNull;
28
29
30
31
32 final class GenericVersionConstraint implements VersionConstraint {
33
34 private final VersionRange range;
35
36 private final Version version;
37
38
39
40
41
42
43 GenericVersionConstraint(VersionRange range) {
44 this.range = requireNonNull(range, "version range cannot be null");
45 this.version = null;
46 }
47
48
49
50
51
52
53 GenericVersionConstraint(Version version) {
54 this.version = requireNonNull(version, "version cannot be null");
55 this.range = null;
56 }
57
58 @Override
59 public VersionRange getRange() {
60 return range;
61 }
62
63 @Override
64 public Version getVersion() {
65 return version;
66 }
67
68 @Override
69 public boolean containsVersion(Version version) {
70 if (range == null) {
71 return version.equals(this.version);
72 } else {
73 return range.containsVersion(version);
74 }
75 }
76
77 @Override
78 public String toString() {
79 return String.valueOf((range == null) ? version : range);
80 }
81
82 @Override
83 public boolean equals(Object obj) {
84 if (this == obj) {
85 return true;
86 }
87 if (obj == null || !getClass().equals(obj.getClass())) {
88 return false;
89 }
90
91 GenericVersionConstraint that = (GenericVersionConstraint) obj;
92
93 return Objects.equals(range, that.range) && Objects.equals(version, that.getVersion());
94 }
95
96 @Override
97 public int hashCode() {
98 int hash = 17;
99 hash = hash * 31 + hash(getRange());
100 hash = hash * 31 + hash(getVersion());
101 return hash;
102 }
103
104 private static int hash(Object obj) {
105 return obj != null ? obj.hashCode() : 0;
106 }
107 }