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

com.ning.maven.plugins.dependencyversionscheck.version.Version Maven / Gradle / Ivy

Go to download

The maven-dependency-versions-check-plugin is a Maven plugin that verifies that the resolved versions of dependencies are at least the versions specified by the dependencies (or their dependencies etc.) if not higher. More specifically, it will check that * The resolved version of every dependency declared explicitly in the current POM is the same or a newer one than what was stated. If the resolved version has a higher major version number than the declared version, then the plugin will issue a warning if configured to do so. Note that enforced declared versions are ignored by the plugin. * For every explicitly declared dependency in the current POM, all its dependency versions are met. I.e. the resolved versions for all dependencies in that dependency's POM are the same or higher than the one stated in that dependency's POM. This is basically the same check as the one above, but using the dependency's POM! Also, if the current POM has exclusions specified for the dependency, then these transitive dependencies are ignored when checking this particular dependency.

There is a newer version: 2.0.4
Show newest version
/*
 * Copyright 2010 Ning, Inc.
 *
 * Ning licenses this file to you 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 com.ning.maven.plugins.dependencyversionscheck.version;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

/**
 * Represents an artifact version.
 */
public final class Version implements Comparable
{
    // Flags for a version element.
    public static final long ALL_NUMBERS =         0x001;
    public static final long ALL_LETTERS =         0x002;
    public static final long ALL_OTHER =           0x004;
    public static final long STARTS_WITH_NUMBERS = 0x008;
    public static final long STARTS_WITH_LETTERS = 0x010;
    public static final long STARTS_WITH_OTHER =   0x020;
    public static final long ENDS_WITH_NUMBERS =   0x040;
    public static final long ENDS_WITH_LETTERS =   0x080;
    public static final long ENDS_WITH_OTHER =     0x100;
    public static final long NUMBERS =             0x200;
    public static final long LETTERS =             0x400;
    public static final long OTHER =               0x800;

    // Flags for a version element divider.
    public static final long DOT_DIVIDER =        0x01;
    public static final long MINUS_DIVIDER =      0x02;
    public static final long UNDERSCORE_DIVIDER = 0x04;
    public static final long OTHER_DIVIDER =      0x08;
    public static final long END_OF_VERSION =     0x10;

    private final String rawVersion;
    private final String selectedVersion;

    private final String [] rawElements;
    private final VersionElement [] versionElements;

    private final int elementCount;

    public Version(final String versionStr)
    {
        this(versionStr, versionStr);
    }

    public Version(final String rawVersion, final String selectedVersion)
    {
        if (StringUtils.isBlank(rawVersion) || StringUtils.isBlank(selectedVersion)) {
            throw new NullPointerException("Version cannot be null");
        }

        this.selectedVersion = selectedVersion;
        this.rawVersion = rawVersion;

        rawElements = StringUtils.splitPreserveAllTokens(selectedVersion, "-._");
        versionElements = new VersionElement[rawElements.length];

        // Position of the next splitter to look at.
        int charPos = 0;

        // Position to store the result in.
        int resultPos = 0;

        for (int i = 0; i < rawElements.length; i++) {
            final String rawElement = rawElements[i];

            long divider = 0L;
            final char dividerChar;

            charPos += rawElement.length();
            if (charPos < selectedVersion.length()) {
                dividerChar = selectedVersion.charAt(charPos);
                charPos++;

                if (dividerChar == '.') {
                    divider |= DOT_DIVIDER;
                }
                else if (dividerChar == '-') {
                    divider |= MINUS_DIVIDER;
                }
                else if (dividerChar == '_') {
                    divider |= UNDERSCORE_DIVIDER;
                }
                else {
                    divider |= OTHER_DIVIDER;
                }

            }
            else {
                dividerChar = 0;
                divider |= END_OF_VERSION;
            }

            final String element = StringUtils.trimToEmpty(rawElement);

            if (!StringUtils.isBlank(element)) {
                long flags = ALL_NUMBERS|ALL_LETTERS|ALL_OTHER;
                final char firstChar = element.charAt(0);
                final char lastChar =  element.charAt(element.length()-1);

                if (Character.isDigit(firstChar)) {
                    flags |= STARTS_WITH_NUMBERS;
                } else if (Character.isLetter(firstChar)) {
                    flags |= STARTS_WITH_LETTERS;
                }
                else {
                    flags |= STARTS_WITH_OTHER;
                }

                if (Character.isDigit(lastChar)) {
                    flags |= ENDS_WITH_NUMBERS;
                } else if (Character.isLetter(lastChar)) {
                    flags |= ENDS_WITH_LETTERS;
                }
                else {
                    flags |= ENDS_WITH_OTHER;
                }

                for (int j = 0; j < element.length(); j++) {

                    if (Character.isDigit(element.charAt(j))) {
                        flags &= ~(ALL_LETTERS|ALL_OTHER);
                        flags |= NUMBERS;
                    } else if (Character.isLetter(element.charAt(j))) {
                        flags &= ~(ALL_NUMBERS|ALL_OTHER);
                        flags |= LETTERS;
                    }
                    else {
                        flags &=  ~(ALL_LETTERS|ALL_NUMBERS);
                        flags |= OTHER;
                    }
                }

                versionElements[resultPos++] = new VersionElement(element, flags, divider, dividerChar);
            }
        }

        this.elementCount = resultPos;
    }

    public String[] getRawElements()
    {
        return rawElements;
    }

    public VersionElement[] getVersionElements()
    {
        return versionElements;
    }

    public int getElementCount()
    {
        return elementCount;
    }

    public String getRawVersion()
    {
        return rawVersion;
    }

    public String getSelectedVersion()
    {
        return selectedVersion;
    }

    public String toString()
    {
        return getSelectedVersion();
    }

    public boolean equals(final Object other)
    {
        if (!(other instanceof Version))
            return false;
        Version castOther = (Version) other;
        return new EqualsBuilder().append(rawVersion, castOther.rawVersion)
            .append(selectedVersion, castOther.selectedVersion)
            .append(rawElements, castOther.rawElements)
            .append(versionElements, castOther.versionElements)
            .append(elementCount, castOther.elementCount)
            .isEquals();
    }



    private transient int hashCode;

    public int hashCode()
    {
        if (hashCode == 0) {
            hashCode = new HashCodeBuilder().append(rawVersion).append(selectedVersion).append(rawElements).append(versionElements).append(elementCount).toHashCode();
        }
        return hashCode;
    }

    public int compareTo(Object o) {
        return selectedVersion.compareTo(((Version) o).getSelectedVersion());
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy