Reformat source code, new line length for comments (79), some trailing whitespace...
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / Version.java
1 /*
2  * jFCPlib - Version.java - Copyright © 2008 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 net.pterodactylus.fcp;
20
21 import java.util.StringTokenizer;
22
23 /**
24  * Container for the “lastGoodVersion” field.
25  *
26  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
27  */
28 public class Version {
29
30         /** The name of the node implementation. */
31         private final String nodeName;
32
33         /** The tree version of the node. */
34         private final String treeVersion;
35
36         /** The protocol version of the node. */
37         private final String protocolVersion;
38
39         /** The build number of the node. */
40         private final int buildNumber;
41
42         /**
43          * Creates a new Version from the given string. The string consists of the
44          * four required fields node name, tree version, protocol version, and
45          * build number, separated by a comma.
46          *
47          * @param version
48          *            The version string
49          * @throws NullPointerException
50          *             if <code>version</code> is <code>null</code>
51          * @throws IllegalArgumentException
52          *             if <code>version</code> is not in the right format
53          */
54         public Version(String version) {
55                 if (version == null) {
56                         throw new NullPointerException("version must not be null");
57                 }
58                 StringTokenizer versionTokens = new StringTokenizer(version, ",");
59                 if (versionTokens.countTokens() != 4) {
60                         throw new IllegalArgumentException("version must consist of four fields");
61                 }
62                 this.nodeName = versionTokens.nextToken();
63                 this.treeVersion = versionTokens.nextToken();
64                 this.protocolVersion = versionTokens.nextToken();
65                 try {
66                         this.buildNumber = Integer.valueOf(versionTokens.nextToken());
67                 } catch (NumberFormatException nfe1) {
68                         throw new IllegalArgumentException("last part of version must be numeric", nfe1);
69                 }
70         }
71
72         /**
73          * Creates a new Version from the given parts.
74          *
75          * @param nodeName
76          *            The name of the node implementation
77          * @param treeVersion
78          *            The tree version
79          * @param protocolVersion
80          *            The protocol version
81          * @param buildNumber
82          *            The build number of the node
83          */
84         public Version(String nodeName, String treeVersion, String protocolVersion, int buildNumber) {
85                 this.nodeName = nodeName;
86                 this.treeVersion = treeVersion;
87                 this.protocolVersion = protocolVersion;
88                 this.buildNumber = buildNumber;
89         }
90
91         /**
92          * Returns the name of the node implementation.
93          *
94          * @return The node name
95          */
96         public String getNodeName() {
97                 return nodeName;
98         }
99
100         /**
101          * The tree version of the node.
102          *
103          * @return The tree version of the node
104          */
105         public String getTreeVersion() {
106                 return treeVersion;
107         }
108
109         /**
110          * The protocol version of the node
111          *
112          * @return The protocol version of the node
113          */
114         public String getProtocolVersion() {
115                 return protocolVersion;
116         }
117
118         /**
119          * The build number of the node.
120          *
121          * @return The build number of the node
122          */
123         public int getBuildNumber() {
124                 return buildNumber;
125         }
126
127         /**
128          * @see java.lang.Object#toString()
129          */
130         @Override
131         public String toString() {
132                 return nodeName + "," + treeVersion + "," + protocolVersion + "," + buildNumber;
133         }
134
135 }