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