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  /**
32   * A version scheme using a generic version syntax.
33   */
34  final class TestVersionScheme
35      implements VersionScheme
36  {
37  
38      public Version parseVersion( final String version )
39      {
40          return new TestVersion( version );
41      }
42  
43      public VersionRange parseVersionRange( final String range )
44          throws InvalidVersionSpecificationException
45      {
46          return new TestVersionRange( range );
47      }
48  
49      public VersionConstraint parseVersionConstraint( final String constraint )
50          throws InvalidVersionSpecificationException
51      {
52          Collection<VersionRange> ranges = new ArrayList<>();
53  
54          String process = constraint;
55  
56          while ( process.startsWith( "[" ) || process.startsWith( "(" ) )
57          {
58              int index1 = process.indexOf( ')' );
59              int index2 = process.indexOf( ']' );
60  
61              int index = index2;
62              if ( index2 < 0 || ( index1 >= 0 && index1 < index2 ) )
63              {
64                  index = index1;
65              }
66  
67              if ( index < 0 )
68              {
69                  throw new InvalidVersionSpecificationException( constraint, "Unbounded version range " + constraint );
70              }
71  
72              VersionRange range = parseVersionRange( process.substring( 0, index + 1 ) );
73              ranges.add( range );
74  
75              process = process.substring( index + 1 ).trim();
76  
77              if ( process.length() > 0 && process.startsWith( "," ) )
78              {
79                  process = process.substring( 1 ).trim();
80              }
81          }
82  
83          if ( process.length() > 0 && !ranges.isEmpty() )
84          {
85              throw new InvalidVersionSpecificationException( constraint, "Invalid version range " + constraint
86                  + ", expected [ or ( but got " + process );
87          }
88  
89          VersionConstraint result;
90          if ( ranges.isEmpty() )
91          {
92              result = new TestVersionConstraint( parseVersion( constraint ) );
93          }
94          else
95          {
96              result = new TestVersionConstraint( ranges.iterator().next() );
97          }
98  
99          return result;
100     }
101 
102     @Override
103     public boolean equals( final Object obj )
104     {
105         if ( this == obj )
106         {
107             return true;
108         }
109 
110         return obj != null && getClass().equals( obj.getClass() );
111     }
112 
113     @Override
114     public int hashCode()
115     {
116         return getClass().hashCode();
117     }
118 
119 }