1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  package org.eclipse.aether.internal.test.util;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  
24  import org.eclipse.aether.version.InvalidVersionSpecificationException;
25  import org.eclipse.aether.version.Version;
26  import org.eclipse.aether.version.VersionConstraint;
27  import org.eclipse.aether.version.VersionRange;
28  import org.eclipse.aether.version.VersionScheme;
29  
30  import static java.util.Objects.requireNonNull;
31  
32  
33  
34  
35  final class TestVersionScheme implements VersionScheme {
36  
37      public Version parseVersion(final String version) {
38          requireNonNull(version, "version cannot be null");
39          return new TestVersion(version);
40      }
41  
42      public VersionRange parseVersionRange(final String range) throws InvalidVersionSpecificationException {
43          requireNonNull(range, "range cannot be null");
44          return new TestVersionRange(range);
45      }
46  
47      public VersionConstraint parseVersionConstraint(final String constraint)
48              throws InvalidVersionSpecificationException {
49          requireNonNull(constraint, "constraint cannot be null");
50          Collection<VersionRange> ranges = new ArrayList<>();
51  
52          String process = constraint;
53  
54          while (process.startsWith("[") || process.startsWith("(")) {
55              int index1 = process.indexOf(')');
56              int index2 = process.indexOf(']');
57  
58              int index = index2;
59              if (index2 < 0 || (index1 >= 0 && index1 < index2)) {
60                  index = index1;
61              }
62  
63              if (index < 0) {
64                  throw new InvalidVersionSpecificationException(constraint, "Unbounded version range " + constraint);
65              }
66  
67              VersionRange range = parseVersionRange(process.substring(0, index + 1));
68              ranges.add(range);
69  
70              process = process.substring(index + 1).trim();
71  
72              if (process.startsWith(",")) {
73                  process = process.substring(1).trim();
74              }
75          }
76  
77          if (!process.isEmpty() && !ranges.isEmpty()) {
78              throw new InvalidVersionSpecificationException(
79                      constraint, "Invalid version range " + constraint + ", expected [ or ( but got " + process);
80          }
81  
82          VersionConstraint result;
83          if (ranges.isEmpty()) {
84              result = new TestVersionConstraint(parseVersion(constraint));
85          } else {
86              result = new TestVersionConstraint(ranges.iterator().next());
87          }
88  
89          return result;
90      }
91  
92      @Override
93      public boolean equals(final Object obj) {
94          if (this == obj) {
95              return true;
96          }
97  
98          return obj != null && getClass().equals(obj.getClass());
99      }
100 
101     @Override
102     public int hashCode() {
103         return getClass().hashCode();
104     }
105 }