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.impl;
20  
21  import org.eclipse.aether.RepositorySystemSession;
22  import org.eclipse.aether.impl.VersionRangeResolver;
23  import org.eclipse.aether.resolution.VersionRangeRequest;
24  import org.eclipse.aether.resolution.VersionRangeResolutionException;
25  import org.eclipse.aether.resolution.VersionRangeResult;
26  import org.eclipse.aether.util.version.GenericVersionScheme;
27  import org.eclipse.aether.version.InvalidVersionSpecificationException;
28  import org.eclipse.aether.version.Version;
29  import org.eclipse.aether.version.VersionConstraint;
30  import org.eclipse.aether.version.VersionScheme;
31  
32  import static java.util.Objects.requireNonNull;
33  
34  /**
35   */
36  public class StubVersionRangeResolver implements VersionRangeResolver {
37  
38      private final VersionScheme versionScheme = new GenericVersionScheme();
39  
40      public VersionRangeResult resolveVersionRange(RepositorySystemSession session, VersionRangeRequest request)
41              throws VersionRangeResolutionException {
42          requireNonNull(session, "session cannot be null");
43          requireNonNull(request, "request cannot be null");
44          VersionRangeResult result = new VersionRangeResult(request);
45          try {
46              VersionConstraint constraint =
47                      versionScheme.parseVersionConstraint(request.getArtifact().getVersion());
48              result.setVersionConstraint(constraint);
49              if (constraint.getRange() == null) {
50                  result.addVersion(constraint.getVersion());
51              } else {
52                  for (int i = 1; i < 10; i++) {
53                      Version ver = versionScheme.parseVersion(Integer.toString(i));
54                      if (constraint.containsVersion(ver)) {
55                          result.addVersion(ver);
56                          if (!request.getRepositories().isEmpty()) {
57                              result.setRepository(ver, request.getRepositories().get(0));
58                          }
59                      }
60                  }
61              }
62          } catch (InvalidVersionSpecificationException e) {
63              result.addException(e);
64              throw new VersionRangeResolutionException(result);
65          }
66  
67          return result;
68      }
69  }