View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * A version scheme using a generic version syntax.
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.length() > 0 && process.startsWith(",")) {
73                  process = process.substring(1).trim();
74              }
75          }
76  
77          if (process.length() > 0 && !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 }