Add missing @Override annotations.
[jSite.git] / src / de / todesbaum / jsite / main / Version.java
index 28690b9..373703c 100644 (file)
@@ -1,6 +1,5 @@
 /*
- * jSite - a tool for uploading websites into Freenet
- * Copyright (C) 2006 David Roden
+ * jSite - Version.java - Copyright © 2006–2012 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
@@ -24,18 +23,92 @@ package de.todesbaum.jsite.main;
  *
  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
  */
-public class Version {
+public class Version implements Comparable<Version> {
 
-       /** The version. */
-       private static final String VERSION = "0.6.3";
+       /** The components of the version information. */
+       private final int[] components;
 
        /**
-        * Returns the version.
+        * Creates a new version container with the given components.
         *
-        * @return The version
+        * @param components
+        *            The version components
         */
-       public static final String getVersion() {
-               return VERSION;
+       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;
+       }
+
+       /**
+        * 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 <code>null</code> 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();
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       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;
        }
 
 }