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