X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fde%2Ftodesbaum%2Fjsite%2Fmain%2FVersion.java;h=2775e7907037a693768f8a1e33e72eab7fd8cfa4;hb=a6bda7a82aa1b2cfd0313fb28d3dcca68ca516c5;hp=88f39a063387562eb7c1f37ea50d18c06a289b26;hpb=7c940c099999e0aef711475943b4e8b844c84b7c;p=jSite.git diff --git a/src/de/todesbaum/jsite/main/Version.java b/src/de/todesbaum/jsite/main/Version.java index 88f39a0..2775e79 100644 --- a/src/de/todesbaum/jsite/main/Version.java +++ b/src/de/todesbaum/jsite/main/Version.java @@ -1,6 +1,5 @@ /* - * jSite - a tool for uploading websites into Freenet - * Copyright (C) 2006 David Roden + * jSite - Version.java - Copyright © 2006–2011 David Roden * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,21 +20,94 @@ package de.todesbaum.jsite.main; /** * Container for version information. - * + * * @author David ‘Bombe’ Roden <bombe@freenetproject.org> */ -public class Version { +public class Version implements Comparable { + + /** The components of the version information. */ + private final int[] components; + + /** + * Creates a new version container with the given components. + * + * @param components + * The version components + */ + public Version(int... components) { + this.components = new int[components.length]; + System.arraycopy(components, 0, this.components, 0, components.length); + } - /** The version. */ - private static final String VERSION = "0.4.12.2"; + /** + * Returns the number of version components. + * + * @return The number of version components + */ + public int size() { + return components.length; + } + + /** + * Returns the version component with the given index. + * + * @param index + * The index of the version component + * @return The version component + */ + public int getComponent(int index) { + return components[index]; + } + + /** + * Parses a version from the given string. + * + * @param versionString + * The version string to parse + * @return The parsed version, or null if the string could not + * be parsed + */ + public static Version parse(String versionString) { + String[] componentStrings = versionString.split("\\."); + int[] components = new int[componentStrings.length]; + int index = -1; + for (String componentString : componentStrings) { + try { + components[++index] = Integer.parseInt(componentString); + } catch (NumberFormatException nfe1) { + return null; + } + } + return new Version(components); + } + + /** + * {@inheritDoc} + */ + @Override + public String toString() { + StringBuilder versionString = new StringBuilder(); + for (int component : components) { + if (versionString.length() != 0) { + versionString.append('.'); + } + versionString.append(component); + } + return versionString.toString(); + } /** - * Returns the version. - * - * @return The version + * {@inheritDoc} */ - public static final String getVersion() { - return VERSION; + public int compareTo(Version version) { + int lessComponents = Math.min(components.length, version.components.length); + for (int index = 0; index < lessComponents; index++) { + if (version.components[index] == components[index]) { + continue; + } + return components[index] - version.components[index]; + } + return components.length - version.components.length; } }