Update year in copyright headers.
[jSite.git] / src / main / java / de / todesbaum / jsite / main / Version.java
1 /*
2  * jSite - Version.java - Copyright © 2006–2014 David Roden
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 package de.todesbaum.jsite.main;
20
21 /**
22  * Container for version information.
23  *
24  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
25  */
26 public class Version implements Comparable<Version> {
27
28         /** The components of the version information. */
29         private final int[] components;
30
31         /**
32          * Creates a new version container with the given components.
33          *
34          * @param components
35          *            The version components
36          */
37         public Version(int... components) {
38                 this.components = new int[components.length];
39                 System.arraycopy(components, 0, this.components, 0, components.length);
40         }
41
42         /**
43          * Returns the number of version components.
44          *
45          * @return The number of version components
46          */
47         public int size() {
48                 return components.length;
49         }
50
51         /**
52          * Returns the version component with the given index.
53          *
54          * @param index
55          *            The index of the version component
56          * @return The version component
57          */
58         public int getComponent(int index) {
59                 return components[index];
60         }
61
62         /**
63          * Parses a version from the given string.
64          *
65          * @param versionString
66          *            The version string to parse
67          * @return The parsed version, or <code>null</code> if the string could not
68          *         be parsed
69          */
70         public static Version parse(String versionString) {
71                 String[] componentStrings = versionString.split("\\.");
72                 int[] components = new int[componentStrings.length];
73                 int index = -1;
74                 for (String componentString : componentStrings) {
75                         try {
76                                 components[++index] = Integer.parseInt(componentString);
77                         } catch (NumberFormatException nfe1) {
78                                 return null;
79                         }
80                 }
81                 return new Version(components);
82         }
83
84         /**
85          * {@inheritDoc}
86          */
87         @Override
88         public String toString() {
89                 StringBuilder versionString = new StringBuilder();
90                 for (int component : components) {
91                         if (versionString.length() != 0) {
92                                 versionString.append('.');
93                         }
94                         versionString.append(component);
95                 }
96                 return versionString.toString();
97         }
98
99         /**
100          * {@inheritDoc}
101          */
102         @Override
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 }