X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=inline;f=src%2Fde%2Ftodesbaum%2Fjsite%2Fmain%2FVersion.java;h=2b80c36aa93c8737ae1e606da71f629c59f64dcd;hb=a3448c0d411f40efbc68244cb2d7d504e5429733;hp=1c791dd3a64b99b3bf1f26ad1093cf58a8cf8225;hpb=5e106418b1485cb753d19a99ff478ef01f1c9007;p=jSite.git
diff --git a/src/de/todesbaum/jsite/main/Version.java b/src/de/todesbaum/jsite/main/Version.java
index 1c791dd..2b80c36 100644
--- a/src/de/todesbaum/jsite/main/Version.java
+++ b/src/de/todesbaum/jsite/main/Version.java
@@ -20,15 +20,95 @@
package de.todesbaum.jsite.main;
/**
- * @author David Roden
- * @version $Id$
+ * 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);
+ }
+
+ /**
+ * Returns the number of version components.
+ *
+ * @return The number of version components
+ */
+ public int size() {
+ return components.length;
+ }
- private static final String VERSION = "0.4.12";
+ /**
+ * 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();
+ }
- public static final String getVersion() {
- return VERSION;
+ /**
+ * {@inheritDoc}
+ */
+ 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;
}
}