Project

General

Profile

1 2079 jones
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2004 Regents of the University of California and the
4
 *             National Center for Ecological Analysis and Synthesis
5
 *
6
 *   '$Author$'
7
 *     '$Date$'
8
 * '$Revision$'
9
 *
10
 * This program is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
 */
24
package edu.ucsb.nceas.metacat;
25
26 4080 daigle
import java.util.Vector;
27 2079 jones
28 4080 daigle
import edu.ucsb.nceas.utilities.StringUtil;
29
30
31 2079 jones
/**
32
 * @author jones
33
 *
34
 * Version represents the current version information for this Metacat instance.
35
 */
36 4080 daigle
public class Version implements Comparable<Version>
37 2079 jones
{
38 4080 daigle
	protected static int allowedVersionLevels = 4;
39
	protected int[] subversions = {0, 0, 0, 0};
40
	protected String versionString = null;
41
42
	/**
43
	 * Create a Version object, setting sub version levels based on the given
44
	 * version id.
45
	 *
46
	 * @param versionID
47
	 */
48
    public Version(String versionID) {
49
    	setSubversions(versionID);
50
    	versionString = versionID;
51
    }
52
53 2079 jones
    /**
54 4080 daigle
	 * Compare this Version object to another. Use the sub version levels to do
55
	 * the comparison. In that way, we can make sure that something like version
56
	 * 1.0.0.1 is less than 2.0.1.
57
	 *
58
	 * @param anotherVersion
59
	 * @return -1 if this version is less than given version, 0 if they are
60
	 *         equal and 1 if this version is greater than given version.
61
	 */
62
	public int compareTo(Version anotherVersion) {
63
    	for (int i = 0; i < allowedVersionLevels; i++) {
64
    		if (this.getSubversion(i) < ((Version)anotherVersion).getSubversion(i)) {
65
    			return -1;
66
    		} else if (this.getSubversion(i) > ((Version)anotherVersion).getSubversion(i)) {
67
    			return 1;
68
    		}
69
    	}
70
		return 0;
71 2079 jones
    }
72 4080 daigle
73
	public int getSubversion(int level) {
74
		return subversions[level];
75
	}
76
77
	/**
78
	 * Set each level (up to 4) of sub version based on the given version id.
79
	 *
80
	 * @param versionID
81
	 *            a string representing a dotted version notation
82
	 */
83
    private void setSubversions(String versionID) throws NumberFormatException {
84
    	Vector<String> subversionStrings = StringUtil.toVector(versionID, '.');
85
    	for (int i = 0; i < subversionStrings.size() && i < allowedVersionLevels; i++) {
86
    		int subversion = Integer.parseInt(subversionStrings.elementAt(i));
87
    		this.subversions[i] = subversion;
88
    	}
89
    }
90 2079 jones
91 4080 daigle
    public String getVersionString() {
92
    	return versionString;
93 2079 jones
    }
94
}