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