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.util.version;
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.VersionRange;
26  import org.eclipse.aether.version.VersionScheme;
27  
28  import static java.util.Objects.requireNonNull;
29  
30  /**
31   * A version scheme support class. A new implementation should extend this class and would receive full support for
32   * ranges and constraints. The new implementation should implement {@link org.eclipse.aether.version.Version} and
33   * the one missing method in this class, the {@link #parseVersion(String)}.
34   *
35   * @since 2.0.0
36   */
37  abstract class VersionSchemeSupport implements VersionScheme {
38      @Override
39      public GenericVersionRange parseVersionRange(final String range) throws InvalidVersionSpecificationException {
40          return new GenericVersionRange(this, range);
41      }
42  
43      @Override
44      public GenericVersionConstraint parseVersionConstraint(final String constraint)
45              throws InvalidVersionSpecificationException {
46          String process = requireNonNull(constraint, "constraint cannot be null");
47  
48          Collection<VersionRange> ranges = new ArrayList<>();
49  
50          while (process.startsWith("[") || process.startsWith("(")) {
51              int index1 = process.indexOf(')');
52              int index2 = process.indexOf(']');
53  
54              int index = index2;
55              if (index2 < 0 || (index1 >= 0 && index1 < index2)) {
56                  index = index1;
57              }
58  
59              if (index < 0) {
60                  throw new InvalidVersionSpecificationException(constraint, "Unbounded version range " + constraint);
61              }
62  
63              VersionRange range = parseVersionRange(process.substring(0, index + 1));
64              ranges.add(range);
65  
66              process = process.substring(index + 1).trim();
67  
68              if (process.startsWith(",")) {
69                  process = process.substring(1).trim();
70              }
71          }
72  
73          if (!process.isEmpty() && !ranges.isEmpty()) {
74              throw new InvalidVersionSpecificationException(
75                      constraint, "Invalid version range " + constraint + ", expected [ or ( but got " + process);
76          }
77  
78          GenericVersionConstraint result;
79          if (ranges.isEmpty()) {
80              result = new GenericVersionConstraint(parseVersion(constraint));
81          } else {
82              result = new GenericVersionConstraint(UnionVersionRange.from(ranges));
83          }
84  
85          return result;
86      }
87  }