1 package org.eclipse.aether.util.version;
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 and common sense sorting.
35 * <p>
36 * This scheme accepts versions of any form, interpreting a version as a sequence of numeric and alphabetic segments.
37 * The characters '-', '_', and '.' as well as the mere transitions from digit to letter and vice versa delimit the
38 * version segments. Delimiters are treated as equivalent.
39 * </p>
40 * <p>
41 * Numeric segments are compared mathematically, alphabetic segments are compared lexicographically and
42 * case-insensitively. However, the following qualifier strings are recognized and treated specially: "alpha" = "a" <
43 * "beta" = "b" < "milestone" = "m" < "cr" = "rc" < "snapshot" < "final" = "ga" < "sp". All of those
44 * well-known qualifiers are considered smaller/older than other strings. An empty segment/string is equivalent to 0.
45 * </p>
46 * <p>
47 * In addition to the above mentioned qualifiers, the tokens "min" and "max" may be used as final version segment to
48 * denote the smallest/greatest version having a given prefix. For example, "1.2.min" denotes the smallest version in
49 * the 1.2 line, "1.2.max" denotes the greatest version in the 1.2 line. A version range of the form "[M.N.*]" is short
50 * for "[M.N.min, M.N.max]".
51 * </p>
52 * <p>
53 * Numbers and strings are considered incomparable against each other. Where version segments of different kind would
54 * collide, comparison will instead assume that the previous segments are padded with trailing 0 or "ga" segments,
55 * respectively, until the kind mismatch is resolved, e.g. "1-alpha" = "1.0.0-alpha" < "1.0.1-ga" = "1.0.1".
56 * </p>
57 */
58 public final class GenericVersionScheme
59 implements VersionScheme
60 {
61
62 /**
63 * Creates a new instance of the version scheme for parsing versions.
64 */
65 public GenericVersionScheme()
66 {
67 }
68
69 public Version parseVersion( final String version )
70 throws InvalidVersionSpecificationException
71 {
72 requireNonNull( version, "version cannot be null" );
73 return new GenericVersion( version );
74 }
75
76 public VersionRange parseVersionRange( final String range )
77 throws InvalidVersionSpecificationException
78 {
79 requireNonNull( range, "range cannot be null" );
80 return new GenericVersionRange( range );
81 }
82
83 public VersionConstraint parseVersionConstraint( final String constraint )
84 throws InvalidVersionSpecificationException
85 {
86 requireNonNull( constraint, "constraint cannot be null" );
87 Collection<VersionRange> ranges = new ArrayList<>();
88
89 String process = constraint;
90
91 while ( process.startsWith( "[" ) || process.startsWith( "(" ) )
92 {
93 int index1 = process.indexOf( ')' );
94 int index2 = process.indexOf( ']' );
95
96 int index = index2;
97 if ( index2 < 0 || ( index1 >= 0 && index1 < index2 ) )
98 {
99 index = index1;
100 }
101
102 if ( index < 0 )
103 {
104 throw new InvalidVersionSpecificationException( constraint, "Unbounded version range " + constraint );
105 }
106
107 VersionRange range = parseVersionRange( process.substring( 0, index + 1 ) );
108 ranges.add( range );
109
110 process = process.substring( index + 1 ).trim();
111
112 if ( process.length() > 0 && process.startsWith( "," ) )
113 {
114 process = process.substring( 1 ).trim();
115 }
116 }
117
118 if ( process.length() > 0 && !ranges.isEmpty() )
119 {
120 throw new InvalidVersionSpecificationException( constraint, "Invalid version range " + constraint
121 + ", expected [ or ( but got " + process );
122 }
123
124 VersionConstraint result;
125 if ( ranges.isEmpty() )
126 {
127 result = new GenericVersionConstraint( parseVersion( constraint ) );
128 }
129 else
130 {
131 result = new GenericVersionConstraint( UnionVersionRange.from( ranges ) );
132 }
133
134 return result;
135 }
136
137 @Override
138 public boolean equals( final Object obj )
139 {
140 if ( this == obj )
141 {
142 return true;
143 }
144
145 return obj != null && getClass().equals( obj.getClass() );
146 }
147
148 @Override
149 public int hashCode()
150 {
151 return getClass().hashCode();
152 }
153
154 }