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