1 package org.eclipse.aether.util.version;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.Collection;
24
25 import org.eclipse.aether.version.InvalidVersionSpecificationException;
26 import org.eclipse.aether.version.Version;
27 import org.eclipse.aether.version.VersionConstraint;
28 import org.eclipse.aether.version.VersionRange;
29 import org.eclipse.aether.version.VersionScheme;
30
31 import static java.util.Objects.requireNonNull;
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 public final class GenericVersionScheme
59 implements VersionScheme
60 {
61
62
63
64
65 public GenericVersionScheme()
66 {
67 }
68
69 public Version parseVersion( final String version )
70 throws InvalidVersionSpecificationException
71 {
72 requireNonNull( version, "version cannot be null" );
73 return new GenericVersion( version );
74 }
75
76 public VersionRange parseVersionRange( final String range )
77 throws InvalidVersionSpecificationException
78 {
79 requireNonNull( range, "range cannot be null" );
80 return new GenericVersionRange( range );
81 }
82
83 public VersionConstraint parseVersionConstraint( final String constraint )
84 throws InvalidVersionSpecificationException
85 {
86 requireNonNull( constraint, "constraint cannot be null" );
87 Collection<VersionRange> ranges = new ArrayList<>();
88
89 String process = constraint;
90
91 while ( process.startsWith( "[" ) || process.startsWith( "(" ) )
92 {
93 int index1 = process.indexOf( ')' );
94 int index2 = process.indexOf( ']' );
95
96 int index = index2;
97 if ( index2 < 0 || ( index1 >= 0 && index1 < index2 ) )
98 {
99 index = index1;
100 }
101
102 if ( index < 0 )
103 {
104 throw new InvalidVersionSpecificationException( constraint, "Unbounded version range " + constraint );
105 }
106
107 VersionRange range = parseVersionRange( process.substring( 0, index + 1 ) );
108 ranges.add( range );
109
110 process = process.substring( index + 1 ).trim();
111
112 if ( process.length() > 0 && process.startsWith( "," ) )
113 {
114 process = process.substring( 1 ).trim();
115 }
116 }
117
118 if ( process.length() > 0 && !ranges.isEmpty() )
119 {
120 throw new InvalidVersionSpecificationException( constraint, "Invalid version range " + constraint
121 + ", expected [ or ( but got " + process );
122 }
123
124 VersionConstraint result;
125 if ( ranges.isEmpty() )
126 {
127 result = new GenericVersionConstraint( parseVersion( constraint ) );
128 }
129 else
130 {
131 result = new GenericVersionConstraint( UnionVersionRange.from( ranges ) );
132 }
133
134 return result;
135 }
136
137 @Override
138 public boolean equals( final Object obj )
139 {
140 if ( this == obj )
141 {
142 return true;
143 }
144
145 return obj != null && getClass().equals( obj.getClass() );
146 }
147
148 @Override
149 public int hashCode()
150 {
151 return getClass().hashCode();
152 }
153
154 }