Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 94ce0c4e9f5fa553908b54c5a3e175ef6ca64479 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
75
76
77
78
79
80
81
82
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
124
125
126
127
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
/*****************************************************************************
 * Copyright (c) 2008 CEA LIST.
 *
 *    
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Chokri Mraidha (CEA LIST) Chokri.Mraidha@cea.fr - Initial API and implementation
 *  Patrick Tessier (CEA LIST) Patrick.Tessier@cea.fr - modification
 *
 *****************************************************************************/
package org.eclipse.papyrus.profile.definition;

import java.util.NoSuchElementException;
import java.util.StringTokenizer;

/**
 * Representation of the version number for a profile
 */
public class Version {

	/** major version number */
	protected int major;

	/** minor version number */
	protected int minor;

	/** micro version number */
	protected int micro;

	/** separator for the version string */
	private final static String SEPARATOR = ".";

	/** The empty version "0.0.0". Equivalent to calling <code>new Version(0,0,0)</code> */
	public static final Version emptyVersion = new Version(0, 0, 0);

	/**
	 * Creates a new Version
	 * 
	 * @param major
	 *        the major version value (should be positive)
	 * @param minor
	 *        the minor version value (should be positive)
	 * @param micro
	 *        the micro version value (should be positive)
	 */
	public Version(int major, int minor, int micro) {
		this.major = major;
		this.minor = minor;
		this.micro = micro;
	}

	/**
	 * Creates a new Version, parsing a string value
	 * 
	 * @param value
	 *        the string representing the version
	 */
	public Version(String value) throws IllegalArgumentException {
		try {
			StringTokenizer st = new StringTokenizer(value, SEPARATOR, true);
			major = Integer.parseInt(st.nextToken());

			if(st.hasMoreTokens()) {
				st.nextToken(); // consume delimiter
				minor = Integer.parseInt(st.nextToken());

				if(st.hasMoreTokens()) {
					st.nextToken(); // consume delimiter
					micro = Integer.parseInt(st.nextToken());

					if(st.hasMoreTokens()) {
						throw new IllegalArgumentException("invalid format");
					}
				}
			}
		} catch (NoSuchElementException e) {
			throw new IllegalArgumentException("invalid format");
		}
	}

	/**
	 * Returns the major version number
	 * 
	 * @return The major version number
	 */
	public int getMajor() {
		return major;
	}

	/**
	 * Returns the minor version number
	 * 
	 * @return The minor version number
	 */
	public int getMinor() {
		return minor;
	}

	/**
	 * Returns the micro version number
	 * 
	 * @return The micro version number
	 */
	public int getMicro() {
		return micro;
	}

	/**
	 * Updates the version numbers
	 * 
	 * @param major
	 *        the new major value
	 * @param minor
	 *        the new minor value
	 * @param micro
	 *        the new micro value
	 */
	public void updateVersion(int major, int minor, int micro) {
		this.major = major;
		this.minor = minor;
		this.micro = micro;
	}

	// org.osgi.framework.Version
	/**
	 * Creates a version given the specific String
	 * 
	 * @param version
	 *        the string to parse
	 * @return the version value corresponding to the String
	 */
	public static Version parseVersion(String version) throws IllegalArgumentException {
		if(version == null) {
			return emptyVersion;
		}

		version = version.trim();
		if(version.length() == 0) {
			return emptyVersion;
		}
		return new Version(version);
	}

	/**
	 * Returns the string corresponding to the version
	 */
	@Override
	public String toString() {
		return major + SEPARATOR + minor + SEPARATOR + micro;
	}
}

Back to the top