2 * jSite - a tool for uploading websites into Freenet
3 * Copyright (C) 2006 David Roden
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 package de.todesbaum.jsite.main;
23 * Container for version information.
25 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
27 public class Version implements Comparable<Version> {
29 /** The components of the version information. */
30 private final int[] components;
33 * Creates a new version container with the given components.
36 * The version components
38 public Version(int... components) {
39 this.components = new int[components.length];
40 System.arraycopy(components, 0, this.components, 0, components.length);
44 * Returns the number of version components.
46 * @return The number of version components
49 return components.length;
53 * Returns the version component with the given index.
56 * The index of the version component
57 * @return The version component
59 public int getComponent(int index) {
60 return components[index];
64 * Parses a version from the given string.
66 * @param versionString
67 * The version string to parse
68 * @return The parsed version, or <code>null</code> if the string could not
71 public static Version parse(String versionString) {
72 String[] componentStrings = versionString.split("\\.");
73 int[] components = new int[componentStrings.length];
75 for (String componentString : componentStrings) {
77 components[++index] = Integer.parseInt(componentString);
78 } catch (NumberFormatException nfe1) {
82 return new Version(components);
89 public String toString() {
90 StringBuilder versionString = new StringBuilder();
91 for (int component : components) {
92 if (versionString.length() != 0) {
93 versionString.append('.');
95 versionString.append(component);
97 return versionString.toString();
103 public int compareTo(Version version) {
104 int lessComponents = Math.min(components.length, version.components.length);
105 for (int index = 0; index < lessComponents; index++) {
106 if (version.components[index] == components[index]) {
109 return components[index] - version.components[index];
111 return components.length - version.components.length;