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