Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2006-2007 the original author or authors.
   *
   * 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.codehaus.gmavenplus.model;
 
Container for Version information in the form of major.minor.revision-tag.

Author(s):
Jason Dillon
Keegan Witt
Since:
1.0-beta-1
 
 public class Version implements Comparable<Version> {
    
The version major number.
 
     private int major;
    
The version minor number.
 
     private int minor;
    
The version revision.
 
     private int revision;
    
The version tag.
 
     private String tag;

    
Constructs a new version object with the specified parameters.

Parameters:
newMajor The version major number
newMinor The version minor number
newRevision The version revision number
newTag The version tag string
 
     public Version(final int newMajorfinal int newMinorfinal int newRevisionfinal String newTag) {
         if (newMajor < 0 || newMinor < 0 || newRevision < 0) {
             // note we don't check the tag since it can be null
             throw new IllegalArgumentException("Major must be >= 0 and minor >= 0 and revision >= 0.");
         }
 
          = newMajor;
          = newMinor;
          = newRevision;
         if (newTag == null || newTag.length() != 0) {
              = newTag;
         } else if (newTag.length() == 0) {
              = null;
         }
     }

    
Constructs a new Version object with the specified parameters.

Parameters:
newMajor The version major number
newMinor The version minor number
newRevision The version revision number
 
     public Version(final int newMajorfinal int newMinorfinal int newRevision) {
         this(newMajornewMinornewRevisionnull);
     }

    
Constructs a new Version object with the specified parameters.

Parameters:
newMajor The version major number
newMinor The version minor number
 
     public Version(final int newMajorfinal int newMinor) {
         this(newMajornewMinor, 0);
     }

    
Constructs a new Version object with the specified parameters.

Parameters:
newMajor The version major number
 
     public Version(final int newMajor) {
         this(newMajor, 0);
    }

    
Parses a new Version object from a string.

Parameters:
version The version string to parse
Returns:
The version parsed from the string
    public static Version parseFromString(final String version) {
        if (version == null || version.length() == 0) {
            throw new IllegalArgumentException("Version must not be null or empty.");
        }
        String[] split = version.split("[._-]", 4);
        try {
            int tagIdx = 3;
            int major = Integer.parseInt(split[0]);
            int minor = 0;
            int revision = 0;
            StringBuilder tag = new StringBuilder();
            if (split.length >= 2) {
                try {
                    minor = Integer.parseInt(split[1]);
                } catch (NumberFormatException nfe) {
                    // version string must not have specified a minor version, leave minor as 0 and append to tag instead
                    tag.append(split[1]);
                    tagIdx = 1;
                    tag.append("-");
                }
            }
            if (split.length >= 3) {
                try {
                    revision = Integer.parseInt(split[2]);
                } catch (NumberFormatException nfe) {
                    // version string must not have specified a revision version, leave revision as 0 and append to tag instead
                    tag.append(split[2]);
                    tagIdx = 2;
                    tag.append("-");
                }
            }
            if (split.length >= 4) {
                for (int i = tagIdxi < split.lengthi++) {
                    if (i > tagIdx) {
                        tag.append("-");
                    }
                    tag.append(split[i]);
                }
            }
            return new Version(majorminorrevisiontag.toString());
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("Major, minor, and revision must be integers."e);
        }
    }

    
Returns a hash code for this object.

Returns:
The hash code for this object
See also:
java.lang.Object.hashCode()
    public final int hashCode() {
        return Arrays.hashCode(new Object[] {});
    }

    
Determines whether the specified object is equal to this object.

Parameters:
obj The object to compare to this object
Returns:
true if the specified object is equal to this object, false otherwise
See also:
java.lang.Object.equals(java.lang.Object)
    public final boolean equals(final Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        if (obj.getClass() != getClass()) {
            return false;
        }
        return compareTo((Versionobj) == 0;
    }

    
Returns a String representation of this object.

Returns:
The String representation of this object
See also:
java.lang.Object.toString()
    public final String toString() {
        StringBuilder buff = new StringBuilder();
        buff.append()
                .append(".").append()
                .append(".").append();
        if ( != null) {
            buff.append("-").append();
        }
        return buff.toString();
    }

    
Compares two versions objects. Note that if the major, minor, and revision are all the same, tags are compared with String.compareTo(). Having no tag is considered a newer version than a version with a tag.

Parameters:
version The version to compare this version to
Returns:
0 if the version is equal to this version, 1 if the version is greater than this version, or -1 if the version is lower than this version.
    public final int compareTo(final Version version) {
        return compareTo(versiontrue);
    }

    
Compares two versions objects. Note that if the major, minor, and revision are all the same, tags are compared with String.compareTo().

Parameters:
version The version to compare this version to
noTagsAreNewer Whether versions with no tag are considered newer than those that have tags
Returns:
0 if the version is equal to this version, 1 if the version is greater than this version, or -1 if the version is lower than this version.
    public final int compareTo(final Version versionfinal boolean noTagsAreNewer) {
        // "beta" is replaced with " beta" to make sure RCs are considered newer than betas (by moving beta to back of order)
        int comp =  < version.major ? -1 : ( == version.major ? 0 : 1);
        if (comp == 0) {
            comp =  < version.minor ? -1 : ( == version.minor ? 0 : 1);
        }
        if (comp == 0) {
            comp =  < version.revision ? -1 : ( == version.revision ? 0 : 1);
        }
        if (comp == 0 &&  != null && version.tag != null) {
            return .replace("beta"" beta").compareTo(version.tag.replace("beta"" beta"));
        } else if (comp == 0 &&  == null && version.tag != null) {
            return noTagsAreNewer ? 1 : -1;
        } else if (comp == 0 &&  != null && version.tag == null) {
            return noTagsAreNewer ? -1 : 1;
        } else {
            return comp;
        }
    }

    
Gets the version major number.

Returns:
The major version number
    public int getMajor() {
        return ;
    }

    
Sets the version major number.

Parameters:
newMajor The major version number to set
Returns:
This object (for fluent invocation)
    public Version setMajor(final int newMajor) {
         = newMajor;
        return this;
    }

    
Gets the version minor number.

Returns:
The version minor number
    public int getMinor() {
        return ;
    }

    
Sets the version minor number.

Parameters:
newMinor The version minor number to set
Returns:
This object (for fluent invocation)
    public Version setMinor(final int newMinor) {
         = newMinor;
        return this;
    }

    
Gets the version revision number.

Returns:
The version revision number
    public int getRevision() {
        return ;
    }

    
Sets the version revision number.

Parameters:
newRevision The revision number to set
Returns:
This object (for fluent invocation)
    public Version setRevision(final int newRevision) {
         = newRevision;
        return this;
    }

    
Gets the version tag string.

Returns:
The version tag string
    public String getTag() {
        return ;
    }

    
Sets the version tag string.

Parameters:
newTag The version tag string to set
Returns:
This object (for fluent invocation)
    public Version setTag(final String newTag) {
         = newTag;
        return this;
    }
New to GrepCode? Check out our FAQ X