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
031/**
032 * A version scheme using a generic version syntax and common sense sorting.
033 * <p>
034 * This scheme accepts versions of any form, interpreting a version as a sequence of numeric and alphabetic segments.
035 * The characters '-', '_', and '.' as well as the mere transitions from digit to letter and vice versa delimit the
036 * version segments. Delimiters are treated as equivalent.
037 * </p>
038 * <p>
039 * Numeric segments are compared mathematically, alphabetic segments are compared lexicographically and
040 * case-insensitively. However, the following qualifier strings are recognized and treated specially: "alpha" = "a" &lt;
041 * "beta" = "b" &lt; "milestone" = "m" &lt; "cr" = "rc" &lt; "snapshot" &lt; "final" = "ga" &lt; "sp". All of those
042 * well-known qualifiers are considered smaller/older than other strings. An empty segment/string is equivalent to 0.
043 * </p>
044 * <p>
045 * In addition to the above mentioned qualifiers, the tokens "min" and "max" may be used as final version segment to
046 * denote the smallest/greatest version having a given prefix. For example, "1.2.min" denotes the smallest version in
047 * 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
048 * for "[M.N.min, M.N.max]".
049 * </p>
050 * <p>
051 * Numbers and strings are considered incomparable against each other. Where version segments of different kind would
052 * collide, comparison will instead assume that the previous segments are padded with trailing 0 or "ga" segments,
053 * respectively, until the kind mismatch is resolved, e.g. "1-alpha" = "1.0.0-alpha" &lt; "1.0.1-ga" = "1.0.1".
054 * </p>
055 */
056public final class GenericVersionScheme
057    implements VersionScheme
058{
059
060    /**
061     * Creates a new instance of the version scheme for parsing versions.
062     */
063    public GenericVersionScheme()
064    {
065    }
066
067    public Version parseVersion( final String version )
068        throws InvalidVersionSpecificationException
069    {
070        return new GenericVersion( version );
071    }
072
073    public VersionRange parseVersionRange( final String range )
074        throws InvalidVersionSpecificationException
075    {
076        return new GenericVersionRange( range );
077    }
078
079    public VersionConstraint parseVersionConstraint( final String constraint )
080        throws InvalidVersionSpecificationException
081    {
082        Collection<VersionRange> ranges = new ArrayList<VersionRange>();
083
084        String process = constraint;
085
086        while ( process.startsWith( "[" ) || process.startsWith( "(" ) )
087        {
088            int index1 = process.indexOf( ')' );
089            int index2 = process.indexOf( ']' );
090
091            int index = index2;
092            if ( index2 < 0 || ( index1 >= 0 && index1 < index2 ) )
093            {
094                index = index1;
095            }
096
097            if ( index < 0 )
098            {
099                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}