Version.java 5.76 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4
/**
 * $Revision$
 * $Date$
 *
5
 * Copyright (C) 2004-2008 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
6
 *
7 8 9 10 11 12 13 14 15 16 17
 * 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.
Matt Tucker's avatar
Matt Tucker committed
18
 */
19

Matt Tucker's avatar
Matt Tucker committed
20 21
package org.jivesoftware.util;

22 23
import java.util.StringTokenizer;

Matt Tucker's avatar
Matt Tucker committed
24
/**
25
 * Holds version information for Openfire.
Matt Tucker's avatar
Matt Tucker committed
26 27 28
 *
 * @author Iain Shigeoka
 */
29
public class Version implements Comparable<Version> {
Matt Tucker's avatar
Matt Tucker committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

    /**
     * The major number (ie 1.x.x).
     */
    private int major;

    /**
     * The minor version number (ie x.1.x).
     */
    private int minor;

    /**
     * The micro version number (ie x.x.1).
     */
    private int micro;

    /**
     * A status release number or -1 to indicate none.
     */
    private int statusVersion;

    /**
     * The release state of the product (Release, Release Candidate).
     */
    private ReleaseStatus status;

    /**
     * Cached version string information
     */
    private String versionString;

    /**
     * Create a new version information object.
     *
     * @param major the major release number.
     * @param minor the minor release number.
     * @param micro the micro release number.
     * @param status the status of the release.
     */
    public Version(int major, int minor, int micro, ReleaseStatus status, int statusVersion) {
        this.major = major;
        this.minor = minor;
        this.micro = micro;
        this.status = status;
        this.statusVersion = statusVersion;
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    }
    
    /**
     * Create a new version from a simple version string (e.g. "3.9.3")
     * 
     * @param source the version string
     */
    public Version(String source) {
    	// initialize the defaults
    	major = minor = micro = 0;
    	status = ReleaseStatus.Release;
    	statusVersion = -1;
    	
    	if (source != null) {
        	StringTokenizer parser = new StringTokenizer(source, ".");
    		try {
    			major = parser.hasMoreTokens() ? Integer.parseInt(parser.nextToken()) : 0;
    			minor = parser.hasMoreTokens() ? Integer.parseInt(parser.nextToken()) : 0;
    			micro = parser.hasMoreTokens() ? Integer.parseInt(parser.nextToken()) : 0;
    		}
    		catch (NumberFormatException nfe) {
    			// ignore bad version
    		}
    	}  	
Matt Tucker's avatar
Matt Tucker committed
99 100 101
    }

    /**
102
     * Returns the version number of this instance of Openfire as a
Matt Tucker's avatar
Matt Tucker committed
103 104 105 106 107
     * String (ie major.minor.revision).
     *
     * @return The version as a string
     */
    public String getVersionString() {
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    	if (versionString == null) {
            if (status != null) {
                if (status == ReleaseStatus.Release) {
                    versionString = major + "." + minor + "." + micro;
                }
                else {
                    if (statusVersion >= 0) {
                        versionString = major + "." + minor + "." + micro + " " + status.toString() +
                                " " + statusVersion;
                    }
                    else {
                        versionString = major + "." + minor + "." + micro + " " + status.toString();
                    }
                }
            }
            else {
                versionString = major + "." + minor + "." + micro;
            }
    		
    	}
Matt Tucker's avatar
Matt Tucker committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
        return versionString;
    }

    /**
     * Returns the release status of this product.
     *
     * @return the release status of this product.
     */
    public ReleaseStatus getStatus() {
        return status;
    }

    /**
     * Obtain the major release number for this product.
     *
     * @return The major release number 1.x.x
     */
    public int getMajor() {
        return major;
    }

    /**
     * Obtain the minor release number for this product.
     *
     * @return The minor release number x.1.x
     */
    public int getMinor() {
        return minor;
    }

    /**
     * Obtain the micro release number for this product.
     *
     * @return The micro release number x.x.1
     */
    public int getMicro() {
        return micro;
    }

    /**
168
     * Obtain the status release number for this product. For example, if
Matt Tucker's avatar
Matt Tucker committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182
     * the release status is <strong>alpha</strong> the release may be <strong>5</strong>
     * resulting in a release status of <strong>Alpha 5</strong>.
     *
     * @return The status version or -1 if none is set.
     */
    public int getStatusVersion() {
        return statusVersion;
    }

    /**
     * A class to represent the release status of the server. Product releases
     * are indicated by type safe enum constants.
     */
    public enum ReleaseStatus {
183 184 185 186 187 188 189 190
        Release(""), Release_Candidate("RC"), Beta("Beta"), Alpha("Alpha");

        private String status;

        private ReleaseStatus(String status) {
            this.status = status;
        }

191 192
        @Override
		public String toString() {
193 194
            return status;
        }
Matt Tucker's avatar
Matt Tucker committed
195
    }
196 197 198 199 200 201 202 203 204 205 206 207
    
    /**
     * Convenience method for comparing versions
     * 
     * @param otherVersion a verion to comapr against
     */
    public boolean isNewerThan(Version otherVersion) {
    	return this.compareTo(otherVersion) > 0;
    }

	@Override
	public int compareTo(Version that) {
208 209 210
		if (that == null) {
			return 1;
		}
211 212 213 214 215 216
		
		long thisVersion = (this.getMicro()*10) + (this.getMinor()*1000) + (this.getMajor()*100000);
		long thatVersion = (that.getMicro()*10) + (that.getMinor()*1000) + (that.getMajor()*100000);
		
		return thisVersion == thatVersion ? 0 : thisVersion > thatVersion ? 1 : -1;
	}
Matt Tucker's avatar
Matt Tucker committed
217
}