1 package org.eclipse.aether.internal.test.util;
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 final class TestVersionScheme
37 implements VersionScheme
38 {
39
40 public Version parseVersion( final String version )
41 {
42 requireNonNull( version, "version cannot be null" );
43 return new TestVersion( version );
44 }
45
46 public VersionRange parseVersionRange( final String range )
47 throws InvalidVersionSpecificationException
48 {
49 requireNonNull( range, "range cannot be null" );
50 return new TestVersionRange( range );
51 }
52
53 public VersionConstraint parseVersionConstraint( final String constraint )
54 throws InvalidVersionSpecificationException
55 {
56 requireNonNull( constraint, "constraint cannot be null" );
57 Collection<VersionRange> ranges = new ArrayList<>();
58
59 String process = constraint;
60
61 while ( process.startsWith( "[" ) || process.startsWith( "(" ) )
62 {
63 int index1 = process.indexOf( ')' );
64 int index2 = process.indexOf( ']' );
65
66 int index = index2;
67 if ( index2 < 0 || ( index1 >= 0 && index1 < index2 ) )
68 {
69 index = index1;
70 }
71
72 if ( index < 0 )
73 {
74 throw new InvalidVersionSpecificationException( constraint, "Unbounded version range " + constraint );
75 }
76
77 VersionRange range = parseVersionRange( process.substring( 0, index + 1 ) );
78 ranges.add( range );
79
80 process = process.substring( index + 1 ).trim();
81
82 if ( process.length() > 0 && process.startsWith( "," ) )
83 {
84 process = process.substring( 1 ).trim();
85 }
86 }
87
88 if ( process.length() > 0 && !ranges.isEmpty() )
89 {
90 throw new InvalidVersionSpecificationException( constraint, "Invalid version range " + constraint
91 + ", expected [ or ( but got " + process );
92 }
93
94 VersionConstraint result;
95 if ( ranges.isEmpty() )
96 {
97 result = new TestVersionConstraint( parseVersion( constraint ) );
98 }
99 else
100 {
101 result = new TestVersionConstraint( ranges.iterator().next() );
102 }
103
104 return result;
105 }
106
107 @Override
108 public boolean equals( final Object obj )
109 {
110 if ( this == obj )
111 {
112 return true;
113 }
114
115 return obj != null && getClass().equals( obj.getClass() );
116 }
117
118 @Override
119 public int hashCode()
120 {
121 return getClass().hashCode();
122 }
123
124 }