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
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 public final class GenericVersionScheme
57 implements VersionScheme
58 {
59
60
61
62
63 public GenericVersionScheme()
64 {
65 }
66
67 public Version parseVersion( final String version )
68 throws InvalidVersionSpecificationException
69 {
70 return new GenericVersion( version );
71 }
72
73 public VersionRange parseVersionRange( final String range )
74 throws InvalidVersionSpecificationException
75 {
76 return new GenericVersionRange( range );
77 }
78
79 public VersionConstraint parseVersionConstraint( final String constraint )
80 throws InvalidVersionSpecificationException
81 {
82 Collection<VersionRange> ranges = new ArrayList<VersionRange>();
83
84 String process = constraint;
85
86 while ( process.startsWith( "[" ) || process.startsWith( "(" ) )
87 {
88 int index1 = process.indexOf( ')' );
89 int index2 = process.indexOf( ']' );
90
91 int index = index2;
92 if ( index2 < 0 || ( index1 >= 0 && index1 < index2 ) )
93 {
94 index = index1;
95 }
96
97 if ( index < 0 )
98 {
99 throw new InvalidVersionSpecificationException( constraint, "Unbounded version range " + constraint );
100 }
101
102 VersionRange range = parseVersionRange( process.substring( 0, index + 1 ) );
103 ranges.add( range );
104
105 process = process.substring( index + 1 ).trim();
106
107 if ( process.length() > 0 && process.startsWith( "," ) )
108 {
109 process = process.substring( 1 ).trim();
110 }
111 }
112
113 if ( process.length() > 0 && !ranges.isEmpty() )
114 {
115 throw new InvalidVersionSpecificationException( constraint, "Invalid version range " + constraint
116 + ", expected [ or ( but got " + process );
117 }
118
119 VersionConstraint result;
120 if ( ranges.isEmpty() )
121 {
122 result = new GenericVersionConstraint( parseVersion( constraint ) );
123 }
124 else
125 {
126 result = new GenericVersionConstraint( UnionVersionRange.from( ranges ) );
127 }
128
129 return result;
130 }
131
132 @Override
133 public boolean equals( final Object obj )
134 {
135 if ( this == obj )
136 {
137 return true;
138 }
139
140 return obj != null && getClass().equals( obj.getClass() );
141 }
142
143 @Override
144 public int hashCode()
145 {
146 return getClass().hashCode();
147 }
148
149 }