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

Matt Tucker's avatar
Matt Tucker committed
17 18
package org.jivesoftware.util;

19 20 21
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
22

Matt Tucker's avatar
Matt Tucker committed
23
/**
24
 * Holds version information for Openfire.
Matt Tucker's avatar
Matt Tucker committed
25 26 27
 *
 * @author Iain Shigeoka
 */
28 29 30
public final class Version implements Comparable<Version> {

    private static final Pattern PATTERN = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)(?:\\s+(\\w+))?(?:\\s+(\\d+))?");
Matt Tucker's avatar
Matt Tucker committed
31 32 33 34

    /**
     * The major number (ie 1.x.x).
     */
35
    private final int major;
Matt Tucker's avatar
Matt Tucker committed
36 37 38 39

    /**
     * The minor version number (ie x.1.x).
     */
40
    private final int minor;
Matt Tucker's avatar
Matt Tucker committed
41 42 43 44

    /**
     * The micro version number (ie x.x.1).
     */
45
    private final int micro;
Matt Tucker's avatar
Matt Tucker committed
46 47 48 49

    /**
     * A status release number or -1 to indicate none.
     */
50
    private final int statusVersion;
Matt Tucker's avatar
Matt Tucker committed
51 52 53 54

    /**
     * The release state of the product (Release, Release Candidate).
     */
55
    private final ReleaseStatus status;
Matt Tucker's avatar
Matt Tucker committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

    /**
     * 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;
74
        this.status = status == null ? ReleaseStatus.Release : status;
Matt Tucker's avatar
Matt Tucker committed
75
        this.statusVersion = statusVersion;
76 77 78 79 80 81 82
    }
    
    /**
     * Create a new version from a simple version string (e.g. "3.9.3")
     * 
     * @param source the version string
     */
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    public Version(CharSequence source) {
        if (source != null) {
            Matcher matcher = PATTERN.matcher(source);
            if (matcher.matches()) {
                major = Integer.parseInt(matcher.group(1));
                minor = Integer.parseInt(matcher.group(2));
                micro = Integer.parseInt(matcher.group(3));
                String status = matcher.group(4);
                if (status != null) {
                    switch (status.toLowerCase()) {
                        case "rc":
                            this.status = ReleaseStatus.Release_Candidate;
                            break;
                        case "beta":
                            this.status = ReleaseStatus.Beta;
                            break;
                        case "alpha":
                            this.status = ReleaseStatus.Alpha;
                            break;
                        default:
                            this.status = ReleaseStatus.Release;
                    }
                } else {
                    this.status = ReleaseStatus.Release;
                }
                String statusVersion = matcher.group(5);
                if (statusVersion != null) {
                    this.statusVersion = Integer.parseInt(statusVersion);
                } else {
                    this.statusVersion = -1;
                }
            } else {
                this.major = this.minor = this.micro = 0;
                this.statusVersion = -1;
                this.status = ReleaseStatus.Release;
            }
        } else {
            this.major = this.minor = this.micro = 0;
            this.statusVersion = -1;
            this.status = ReleaseStatus.Release;
        }
Matt Tucker's avatar
Matt Tucker committed
124 125 126
    }

    /**
127
     * Returns the version number of this instance of Openfire as a
Matt Tucker's avatar
Matt Tucker committed
128 129 130 131 132
     * String (ie major.minor.revision).
     *
     * @return The version as a string
     */
    public String getVersionString() {
133
    	if (versionString == null) {
134 135 136 137 138 139
            StringBuilder sb = new StringBuilder();
            sb.append(major).append('.').append(minor).append('.').append(micro);
            if (status != ReleaseStatus.Release) {
                sb.append(' ').append(status);
                if (statusVersion >= 0) {
                    sb.append(' ').append(statusVersion);
140 141
                }
            }
142
            versionString = sb.toString();
143
    	}
Matt Tucker's avatar
Matt Tucker committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
        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;
    }

    /**
184
     * Obtain the status release number for this product. For example, if
Matt Tucker's avatar
Matt Tucker committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198
     * 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 {
199 200 201 202
        Release(""), Release_Candidate("RC"), Beta("Beta"), Alpha("Alpha");

        private String status;

203
        ReleaseStatus(String status) {
204 205 206
            this.status = status;
        }

207 208
        @Override
		public String toString() {
209 210
            return status;
        }
Matt Tucker's avatar
Matt Tucker committed
211
    }
212 213 214 215 216 217 218 219 220 221
    
    /**
     * Convenience method for comparing versions
     * 
     * @param otherVersion a verion to comapr against
     */
    public boolean isNewerThan(Version otherVersion) {
    	return this.compareTo(otherVersion) > 0;
    }

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    @Override
    public int compareTo(Version that) {
        if (that == null) {
            return 1;
        }
        int result = Integer.compare(getMajor(), that.getMajor());
        if (result == 0) {
            result = Integer.compare(getMinor(), that.getMinor());
            if (result == 0) {
                result = Integer.compare(getMicro(), that.getMicro());
                if (result == 0) {
                    result = that.getStatus().compareTo(getStatus());
                    if (result == 0) {
                        result = Integer.compare(getStatusVersion(), that.getStatusVersion());
                    }
                }
            }
        }
        return result;
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) {
            return true;
        }
        if (!(o instanceof Version)) {
            return false;
        }
        Version other = (Version) o;

        return Objects.equals(major, other.major)
                && Objects.equals(minor, other.minor)
                && Objects.equals(micro, other.micro)
                && Objects.equals(statusVersion, other.statusVersion)
                && Objects.equals(status, other.status);
    }

    @Override
    public int hashCode() {
        return Objects.hash(major, minor, micro, statusVersion, status);
    }

    @Override
    public String toString() {
        return getVersionString();
    }
Matt Tucker's avatar
Matt Tucker committed
269
}