2b80c36aa93c8737ae1e606da71f629c59f64dcd
[jSite.git] / src / de / todesbaum / jsite / main / Version.java
1 /*
2  * jSite - a tool for uploading websites into Freenet
3  * Copyright (C) 2006 David Roden
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 package de.todesbaum.jsite.main;
21
22 /**
23  * Container for version information.
24  *
25  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
26  */
27 public class Version implements Comparable<Version> {
28
29         /** The components of the version information. */
30         private final int[] components;
31
32         /**
33          * Creates a new version container with the given components.
34          *
35          * @param components
36          *            The version components
37          */
38         public Version(int... components) {
39                 this.components = new int[components.length];
40                 System.arraycopy(components, 0, this.components, 0, components.length);
41         }
42
43         /**
44          * Returns the number of version components.
45          *
46          * @return The number of version components
47          */
48         public int size() {
49                 return components.length;
50         }
51
52         /**
53          * Returns the version component with the given index.
54          *
55          * @param index
56          *            The index of the version component
57          * @return The version component
58          */
59         public int getComponent(int index) {
60                 return components[index];
61         }
62
63         /**
64          * Parses a version from the given string.
65          *
66          * @param versionString
67          *            The version string to parse
68          * @return The parsed version, or <code>null</code> if the string could not
69          *         be parsed
70          */
71         public static Version parse(String versionString) {
72                 String[] componentStrings = versionString.split("\\.");
73                 int[] components = new int[componentStrings.length];
74                 int index = -1;
75                 for (String componentString : componentStrings) {
76                         try {
77                                 components[++index] = Integer.parseInt(componentString);
78                         } catch (NumberFormatException nfe1) {
79                                 return null;
80                         }
81                 }
82                 return new Version(components);
83         }
84
85         /**
86          * {@inheritDoc}
87          */
88         @Override
89         public String toString() {
90                 StringBuilder versionString = new StringBuilder();
91                 for (int component : components) {
92                         if (versionString.length() != 0) {
93                                 versionString.append('.');
94                         }
95                         versionString.append(component);
96                 }
97                 return versionString.toString();
98         }
99
100         /**
101          * {@inheritDoc}
102          */
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]) {
107                                 continue;
108                         }
109                         return components[index] - version.components[index];
110                 }
111                 return components.length - version.components.length;
112         }
113
114 }