001package org.eclipse.aether.util.version;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.ArrayList;
023import java.util.Collection;
024
025import org.eclipse.aether.version.InvalidVersionSpecificationException;
026import org.eclipse.aether.version.Version;
027import org.eclipse.aether.version.VersionConstraint;
028import org.eclipse.aether.version.VersionRange;
029import org.eclipse.aether.version.VersionScheme;
030
031import static java.util.Objects.requireNonNull;
032
033/**
034 * A version scheme using a generic version syntax and common sense sorting.
035 * <p>
036 * This scheme accepts versions of any form, interpreting a version as a sequence of numeric and alphabetic segments.
037 * The characters '-', '_', and '.' as well as the mere transitions from digit to letter and vice versa delimit the
038 * version segments. Delimiters are treated as equivalent.
039 * </p>
040 * <p>
041 * Numeric segments are compared mathematically, alphabetic segments are compared lexicographically and
042 * case-insensitively. However, the following qualifier strings are recognized and treated specially: "alpha" = "a" &lt;
043 * "beta" = "b" &lt; "milestone" = "m" &lt; "cr" = "rc" &lt; "snapshot" &lt; "final" = "ga" &lt; "sp". All of those
044 * well-known qualifiers are considered smaller/older than other strings. An empty segment/string is equivalent to 0.
045 * </p>
046 * <p>
047 * In addition to the above mentioned qualifiers, the tokens "min" and "max" may be used as final version segment to
048 * denote the smallest/greatest version having a given prefix. For example, "1.2.min" denotes the smallest version in
049 * 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
050 * for "[M.N.min, M.N.max]".
051 * </p>
052 * <p>
053 * Numbers and strings are considered incomparable against each other. Where version segments of different kind would
054 * collide, comparison will instead assume that the previous segments are padded with trailing 0 or "ga" segments,
055 * respectively, until the kind mismatch is resolved, e.g. "1-alpha" = "1.0.0-alpha" &lt; "1.0.1-ga" = "1.0.1".
056 * </p>
057 */
058public final class GenericVersionScheme
059    implements VersionScheme
060{
061
062    /**
063     * Creates a new instance of the version scheme for parsing versions.
064     */
065    public GenericVersionScheme()
066    {
067    }
068
069    public Version parseVersion( final String version )
070        throws InvalidVersionSpecificationException
071    {
072        requireNonNull( version, "version cannot be null" );
073        return new GenericVersion( version );
074    }
075
076    public VersionRange parseVersionRange( final String range )
077        throws InvalidVersionSpecificationException
078    {
079        requireNonNull( range, "range cannot be null" );
080        return new GenericVersionRange( range );
081    }
082
083    public VersionConstraint parseVersionConstraint( final String constraint )
084        throws InvalidVersionSpecificationException
085    {
086        requireNonNull( constraint, "constraint cannot be null" );
087        Collection<VersionRange> ranges = new ArrayList<>();
088
089        String process = constraint;
090
091        while ( process.startsWith( "[" ) || process.startsWith( "(" ) )
092        {
093            int index1 = process.indexOf( ')' );
094            int index2 = process.indexOf( ']' );
095
096            int index = index2;
097            if ( index2 < 0 || ( index1 >= 0 && index1 < index2 ) )
098            {
099                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}