All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.maven.graph.common.ref.ProjectVersionRef Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * Copyright 2012 John Casey
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package org.apache.maven.graph.common.ref;

import java.io.Serializable;

import org.apache.maven.graph.common.version.InvalidVersionSpecificationException;
import org.apache.maven.graph.common.version.SingleVersion;
import org.apache.maven.graph.common.version.VersionSpec;
import org.apache.maven.graph.common.version.VersionUtils;

public class ProjectVersionRef
    extends ProjectRef
    implements VersionedRef, Serializable
{

    private static final long serialVersionUID = 1L;

    // NEVER null
    private final VersionSpec versionSpec;

    public ProjectVersionRef( final ProjectRef ref, final VersionSpec versionSpec )
    {
        this( ref.getGroupId(), ref.getArtifactId(), versionSpec );
    }

    public ProjectVersionRef( final ProjectRef ref, final String versionSpec )
        throws InvalidVersionSpecificationException
    {
        this( ref.getGroupId(), ref.getArtifactId(), versionSpec );
    }

    public ProjectVersionRef( final String groupId, final String artifactId, final VersionSpec versionSpec )
    {
        super( groupId, artifactId );
        if ( versionSpec == null )
        {
            throw new NullPointerException( "Version cannot be null for '" + groupId + ":" + artifactId + "'" );
        }

        this.versionSpec = versionSpec;
    }

    public ProjectVersionRef( final String groupId, final String artifactId, final String versionSpec )
        throws InvalidVersionSpecificationException
    {
        super( groupId, artifactId );
        if ( versionSpec == null || versionSpec.trim()
                                               .length() < 1 )
        {
            throw new InvalidVersionSpecificationException( versionSpec, "Version ('" + versionSpec
                + "') cannot be null or empty for '" + groupId + ":" + artifactId + "'" );
        }

        this.versionSpec = VersionUtils.createFromSpec( versionSpec );
    }

    public ProjectVersionRef asProjectVersionRef()
    {
        return getClass().equals( ProjectVersionRef.class ) ? this : new ProjectVersionRef( getGroupId(),
                                                                                            getArtifactId(),
                                                                                            getVersionSpec() );
    }

    public ProjectRef asProjectRef()
    {
        return new ProjectRef( getGroupId(), getArtifactId() );
    }

    public boolean isRelease()
    {
        return versionSpec.isConcrete();
    }

    public boolean isSpecificVersion()
    {
        return versionSpec.isSingle();
    }

    public boolean matchesVersion( final SingleVersion version )
    {
        return versionSpec.contains( version );
    }

    public ProjectVersionRef selectVersion( final SingleVersion version )
    {
        if ( versionSpec.equals( version ) )
        {
            return this;
        }

        if ( !versionSpec.contains( version ) )
        {
            throw new IllegalArgumentException( "Specified version: " + version.renderStandard()
                + " is not contained in spec: " + versionSpec.renderStandard() );
        }

        return newRef( getGroupId(), getArtifactId(), version );
    }

    protected ProjectVersionRef newRef( final String groupId, final String artifactId, final SingleVersion version )
    {
        return new ProjectVersionRef( groupId, artifactId, version );
    }

    public VersionSpec getVersionSpec()
    {
        return versionSpec;
    }

    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = super.hashCode();
        result = prime * result + ( ( getVersionSpec() == null ) ? 0 : getVersionSpec().hashCode() );
        return result;
    }

    public boolean versionlessEquals( final ProjectVersionRef other )
    {
        if ( this == other )
        {
            return true;
        }

        return super.equals( other );
    }

    @Override
    public boolean equals( final Object obj )
    {
        if ( this == obj )
        {
            return true;
        }
        if ( !super.equals( obj ) )
        {
            return false;
        }
        if ( getClass() != obj.getClass() )
        {
            return false;
        }
        final ProjectVersionRef other = (ProjectVersionRef) obj;
        if ( getVersionSpec() == null )
        {
            if ( other.getVersionSpec() != null )
            {
                return false;
            }
        }
        else if ( !getVersionSpec().equals( other.getVersionSpec() ) )
        {
            return false;
        }
        return true;
    }

    @Override
    public String toString()
    {
        return String.format( "%s:%s:%s", getGroupId(), getArtifactId(), getVersionSpec().renderStandard() );
    }

    public boolean isCompound()
    {
        return !versionSpec.isSingle();
    }

    public boolean isSnapshot()
    {
        return !isCompound() && !isRelease();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy