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