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