new version container
[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 version. */
30         private static final Version VERSION = new Version(0, 6, 2);
31
32         /** The components of the version information. */
33         private final int[] components;
34
35         /**
36          * Creates a new version container with the given components.
37          *
38          * @param components
39          *            The version components
40          */
41         public Version(int... components) {
42                 this.components = new int[components.length];
43                 System.arraycopy(components, 0, this.components, 0, components.length);
44         }
45
46         /**
47          * Returns the number of version components.
48          *
49          * @return The number of version components
50          */
51         public int size() {
52                 return components.length;
53         }
54
55         /**
56          * Returns the version component with the given index.
57          *
58          * @param index
59          *            The index of the version component
60          * @return The version component
61          */
62         public int getComponent(int index) {
63                 return components[index];
64         }
65
66         /**
67          * Returns the version.
68          *
69          * @return The version
70          */
71         public static final String getVersion() {
72                 return VERSION.toString();
73         }
74
75         /**
76          * {@inheritDoc}
77          */
78         @Override
79         public String toString() {
80                 StringBuilder versionString = new StringBuilder();
81                 for (int component : components) {
82                         if (versionString.length() != 0) {
83                                 versionString.append('.');
84                         }
85                         versionString.append(component);
86                 }
87                 return versionString.toString();
88         }
89
90         /**
91          * {@inheritDoc}
92          */
93         public int compareTo(Version version) {
94                 int lessComponents = Math.min(components.length, version.components.length);
95                 for (int index = 0; index < lessComponents; index++) {
96                         if (version.components[index] == components[index]) {
97                                 continue;
98                         }
99                         return components[index] - version.components[index];
100                 }
101                 return components.length - version.components.length;
102         }
103
104 }