2 * jSite - Version.java - Copyright © 2006–2011 David Roden
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 package de.todesbaum.jsite.main;
22 * Container for version information.
24 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
26 public class Version implements Comparable<Version> {
28 /** The components of the version information. */
29 private final int[] components;
32 * Creates a new version container with the given components.
35 * The version components
37 public Version(int... components) {
38 this.components = new int[components.length];
39 System.arraycopy(components, 0, this.components, 0, components.length);
43 * Returns the number of version components.
45 * @return The number of version components
48 return components.length;
52 * Returns the version component with the given index.
55 * The index of the version component
56 * @return The version component
58 public int getComponent(int index) {
59 return components[index];
63 * Parses a version from the given string.
65 * @param versionString
66 * The version string to parse
67 * @return The parsed version, or <code>null</code> if the string could not
70 public static Version parse(String versionString) {
71 String[] componentStrings = versionString.split("\\.");
72 int[] components = new int[componentStrings.length];
74 for (String componentString : componentStrings) {
76 components[++index] = Integer.parseInt(componentString);
77 } catch (NumberFormatException nfe1) {
81 return new Version(components);
88 public String toString() {
89 StringBuilder versionString = new StringBuilder();
90 for (int component : components) {
91 if (versionString.length() != 0) {
92 versionString.append('.');
94 versionString.append(component);
96 return versionString.toString();
102 public int compareTo(Version version) {
103 int lessComponents = Math.min(components.length, version.components.length);
104 for (int index = 0; index < lessComponents; index++) {
105 if (version.components[index] == components[index]) {
108 return components[index] - version.components[index];
110 return components.length - version.components.length;