Package biz.massivedynamics.versioneer.version

Source Code of biz.massivedynamics.versioneer.version.AbstractVersion

/*
* The MIT License
*
* Copyright 2012 Massive Dynamics.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package biz.massivedynamics.versioneer.version;

import biz.massivedynamics.versioneer.version.impl.GenericVersion;
import biz.massivedynamics.versioneer.version.impl.NumericVersion;
import biz.massivedynamics.versioneer.version.impl.ThreePartVersion;
import biz.massivedynamics.versioneer.version.impl.TwoPartVersion;

/**
* An abstract class that defines the skeleton of a version <br /> <br /> All
* version classes must have this class as their parent
*
* @author Cruz Bishop
* @since 0.8.0.0
*/
public abstract class AbstractVersion implements Comparable<AbstractVersion>, Cloneable {

    /**
     * Creates a new abstract version
     *
     * @since 0.8.0.0
     */
    public AbstractVersion() {
        this(VersionType.STABLE, "");
    }

    /**
     * Creates a new abstract version
     *
     * @param type The version type to use
     * @since 0.8.0.0
     */
    public AbstractVersion(VersionType type) {
        this(type, "");
    }

    /**
     * Creates a new abstract version
     *
     * @param type The version type to use
     * @param codeName The code name to use
     * @since 1.1.1.0
     */
    public AbstractVersion(VersionType type, String codeName) {
        this.setType(type);
        this.setCodeName(codeName);
    }
    /**
     * The type of this version
     *
     * @since 0.8.0.0
     */
    private VersionType type;

    /**
     * Gets this version's type
     *
     * @return The type
     * @since 0.8.0.0
     */
    public final VersionType getType() {
        return this.type;
    }

    /**
     * Sets this version's type
     *
     * @param type The type to set
     * @since 0.8.0.0
     */
    public final void setType(VersionType type) {
        this.type = type;
    }
    /**
     * The code name of this version
     *
     * @since 1.1.1.0
     */
    private String codeName;

    /**
     * Gets the code name of this version
     *
     * @return The code name
     * @since 1.1.1.0
     */
    public final String getCodeName() {
        return this.codeName;
    }

    /**
     * Sets the code name of this version
     *
     * @param codeName The code name
     * @since 1.1.1.0
     */
    public final void setCodeName(String codeName) {
        this.codeName = codeName;
    }

    /**
     * Converts this abstract version to a generic version
     *
     * @return The generic representation of this version
     * @since 0.8.0.0
     */
    public abstract GenericVersion toGenericVersion();

    /**
     * Convert this version to a string Doing this to ensure that it is ALWAYS
     * done!
     *
     * @return A string
     * @since 0.8.0.0
     */
    @Override
    public abstract String toString();

    /**
     * Compares this version with another version for equality
     *
     * @param obj The other object, preferably a version!
     * @return True if equal, otherwise false
     * @since 1.1.1.0
     */
    @Override
    public final boolean equals(Object obj) {
        if (!(obj instanceof AbstractVersion)) {
            return false;
        }

        //Set up the other version
        GenericVersion version = ((AbstractVersion) obj).toGenericVersion();

        //And compare
        return ((this.compareTo(version)) == 0);
    }

    /**
     * Gets this version's hashcode
     *
     * @return The hashcode
     * @since 1.1.0.0
     */
    @Override
    public abstract int hashCode();

    /**
     * Clones this version
     *
     * @return The clone
     * @since 1.1.0.0
     */
    @Override
    public abstract AbstractVersion clone();

    /**
     * Gets a version from a string
     *
     * @param versionAsString The version as a string
     * @return The version
     * @throws InvalidVersionException The version could not be decoded
     * @throws IllegalArgumentException The version type could not be decoded
     * @throws NumberFormatException There was an invalid version number (1/3, for example, instead of 13)
     * @since 1.2.0.0
     */
    public static AbstractVersion get(String versionAsString) throws InvalidVersionException, IllegalArgumentException, NumberFormatException {
        //Set up an initial (null) abstract version
        AbstractVersion version = null;

        //Check to see if the string is empty
        if (versionAsString.isEmpty()) {
            throw new InvalidVersionException("Can't create a blank version!");
        }

        //Set up a split
        String[] versionParts = versionAsString.split("\\.");

        //Switch by the number of version parts
        switch (versionParts.length) {
            case 4: {
                int major = Integer.valueOf(versionParts[0]);
                int minor = Integer.valueOf(versionParts[1]);
                int build = Integer.valueOf(versionParts[2]);
                int revision;
                if (versionParts[3].contains("-") || versionParts[3].contains(" ")) {
                    revision = Integer.valueOf(versionParts[3].split("[ -]")[0]);
                } else {
                    revision = Integer.valueOf(versionParts[3]);
                }

                version = new GenericVersion(major, minor, build, revision);
            }
            break;

            case 3: {
                int major = Integer.valueOf(versionParts[0]);
                int minor = Integer.valueOf(versionParts[1]);
                int build;
                if (versionParts[2].contains("-") || versionParts[2].contains(" ")) {
                    build = Integer.valueOf(versionParts[2].split("[ -]")[0]);
                } else {
                    build = Integer.valueOf(versionParts[2]);
                }

                version = new ThreePartVersion(major, minor, build);
            }
            break;

            case 2: {
                int major = Integer.valueOf(versionParts[0]);
                int minor;
                if (versionParts[1].contains("-") || versionParts[1].contains(" ")) {
                    minor = Integer.valueOf(versionParts[1].split("[ -]")[0]);
                } else {
                    minor = Integer.valueOf(versionParts[1]);
                }

                version = new TwoPartVersion(major, minor);
            }
            break;

            case 1: {
                int _version;
                if (versionParts[0].contains("-") || versionParts[0].contains(" ")) {
                    _version = Integer.valueOf(versionParts[0].split("[ -]")[0]);
                } else {
                    _version = Integer.valueOf(versionParts[0]);
                }

                version = new NumericVersion(_version);
            }
            break;
        }

        //Check the version before we start on the rest
        if (version == null) {
            throw new InvalidVersionException("Version \"" + versionAsString + "\" could not be decoded");
        }
       
        //Get the last part
        String lastPart = versionParts[versionParts.length - 1];
       
        //Okay, the version numbers are all done. Let's start on types and names
        if (lastPart.contains("-")) {
           
            //We're getting the type
            String versionTypeString;
           
            //See if there's a breaking space
            if (lastPart.contains(" ")) {
                versionTypeString = lastPart.substring(lastPart.indexOf("-") + 1, lastPart.indexOf(" "));
            } else {
                versionTypeString = lastPart.substring(lastPart.indexOf("-") + 1, lastPart.length());
            }
           
            version.setType(VersionType.valueOf(versionTypeString));
        }
       
        //Okay, type is done. Let's get the name!
        if (lastPart.contains(" ")) {
           
            //We're getting the code name
            if (lastPart.contains("'")) {
                version.setCodeName(lastPart.substring(lastPart.indexOf("'") + 1, lastPart.lastIndexOf("'")));
            } else if (lastPart.contains("\"")) {
                version.setCodeName(lastPart.substring(lastPart.indexOf("\"") + 1, lastPart.lastIndexOf("\"")));
            }
           
        }
       
        //Everything is done! Let's return the version
        return version;
    }
}
TOP

Related Classes of biz.massivedynamics.versioneer.version.AbstractVersion

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.