View Javadoc
1   package org.eclipse.aether.internal.test.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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  import static java.util.Objects.requireNonNull;
32  
33  /**
34   * A version scheme using a generic version syntax.
35   */
36  final class TestVersionScheme
37      implements VersionScheme
38  {
39  
40      public Version parseVersion( final String version )
41      {
42          requireNonNull( version, "version cannot be null" );
43          return new TestVersion( version );
44      }
45  
46      public VersionRange parseVersionRange( final String range )
47          throws InvalidVersionSpecificationException
48      {
49          requireNonNull( range, "range cannot be null" );
50          return new TestVersionRange( range );
51      }
52  
53      public VersionConstraint parseVersionConstraint( final String constraint )
54          throws InvalidVersionSpecificationException
55      {
56          requireNonNull( constraint, "constraint cannot be null" );
57          Collection<VersionRange> ranges = new ArrayList<>();
58  
59          String process = constraint;
60  
61          while ( process.startsWith( "[" ) || process.startsWith( "(" ) )
62          {
63              int index1 = process.indexOf( ')' );
64              int index2 = process.indexOf( ']' );
65  
66              int index = index2;
67              if ( index2 < 0 || ( index1 >= 0 && index1 < index2 ) )
68              {
69                  index = index1;
70              }
71  
72              if ( index < 0 )
73              {
74                  throw new InvalidVersionSpecificationException( constraint, "Unbounded version range " + constraint );
75              }
76  
77              VersionRange range = parseVersionRange( process.substring( 0, index + 1 ) );
78              ranges.add( range );
79  
80              process = process.substring( index + 1 ).trim();
81  
82              if ( process.length() > 0 && process.startsWith( "," ) )
83              {
84                  process = process.substring( 1 ).trim();
85              }
86          }
87  
88          if ( process.length() > 0 && !ranges.isEmpty() )
89          {
90              throw new InvalidVersionSpecificationException( constraint, "Invalid version range " + constraint
91                  + ", expected [ or ( but got " + process );
92          }
93  
94          VersionConstraint result;
95          if ( ranges.isEmpty() )
96          {
97              result = new TestVersionConstraint( parseVersion( constraint ) );
98          }
99          else
100         {
101             result = new TestVersionConstraint( ranges.iterator().next() );
102         }
103 
104         return result;
105     }
106 
107     @Override
108     public boolean equals( final Object obj )
109     {
110         if ( this == obj )
111         {
112             return true;
113         }
114 
115         return obj != null && getClass().equals( obj.getClass() );
116     }
117 
118     @Override
119     public int hashCode()
120     {
121         return getClass().hashCode();
122     }
123 
124 }