Source code: com/flexstor/common/version/VersionString.java
1 /*
2 * VersionString.java
3 *
4 * Copyright $Date: 2003/08/11 02:22:50 $ FLEXSTOR.net Inc.
5 *
6 * This work is licensed for use and distribution under license terms found at
7 * http://www.flexstor.org/license.html
8 *
9 */
10
11 package com.flexstor.common.version;
12
13 import java.util.StringTokenizer;
14
15 /**
16 * VersionString is the class that represents a Version Number string providing
17 * functionality to operate over the version value, including
18 * validation, comparation, increasing and decreasing.
19 * This class should be used to wrap a Version String value wherever
20 * the value needs to be processed.
21 * Classes like com.flexstor.common.awt.table.Table has been provided
22 * with support for sorting VersionString objects.
23 *
24 * @author Jose Hernandez
25 * @version 3.0
26 */
27 public class VersionString
28 {
29 /** Holds every segment value in the Version String. */
30 private int[] naTokens = null;
31
32 /**
33 * @exception InvalidVersionStringException if argument does not represent a valid
34 * version number string.
35 */
36 public VersionString( String sVersion )
37 throws InvalidVersionStringException
38 {
39 if ( sVersion == null )
40 throw new InvalidVersionStringException( "Null argument in constructor." );
41
42 if ( sVersion == "" )
43 throw new InvalidVersionStringException( "Empty argument in constructor." );
44
45 for ( int i = 0; i < sVersion.length(); i++ )
46 switch ( sVersion.charAt( i ) )
47 {
48 case '0':
49 case '1':
50 case '2':
51 case '3':
52 case '4':
53 case '5':
54 case '6':
55 case '7':
56 case '8':
57 case '9':
58 case '.':
59 continue;
60
61 default:
62 throw new InvalidVersionStringException( "Invalid character." );
63 }
64
65 if ( sVersion.indexOf( "." ) == -1 )
66 sVersion += ".0";
67 else
68 {
69 if ( sVersion.startsWith( "." ) )
70 throw new InvalidVersionStringException( "Root version missing." );
71
72 if ( sVersion.endsWith( "." ) )
73 sVersion += "0";
74
75 if ( sVersion.indexOf( ".." ) != -1 )
76 throw new InvalidVersionStringException( "Branch version missing." );
77 }
78
79 StringTokenizer st = new StringTokenizer( sVersion, "." );
80
81 naTokens = new int[st.countTokens()];
82
83 int i = 0;
84 while ( st.hasMoreTokens() )
85 {
86 naTokens[i] = Integer.parseInt( st.nextToken() );
87 i++;
88 }
89 }
90
91 /**
92 * Retrieves a list of the segment values in the Version String.
93 *
94 * @return an array of segment values.
95 */
96 private int[] getTokens()
97 {
98 return naTokens;
99 }
100
101 /**
102 * Compares two VersionString objects.
103 *
104 * @param vsVersion the instance of VersionString to compare with.
105 * @return 0 if both Version are equal;
106 * -1 if this Version is less than the argument Version;
107 * 1 if this Version is greater than the argument Version;
108 */
109 public int compareTo( VersionString vsVersion )
110 {
111 int[] naTokensX = vsVersion.getTokens();
112
113 for ( int i = 0; ( i < naTokens.length ) && ( i < naTokensX.length ); i++ )
114 {
115 if ( naTokens[i] > naTokensX[i] )
116 return 1;
117 else if ( naTokens[i] == naTokensX[i] )
118 continue;
119 else
120 return -1;
121 }
122
123 if ( naTokens.length > naTokensX.length )
124 return 1;
125 else if ( naTokens.length == naTokensX.length )
126 return 0;
127 else
128 return -1;
129 }
130
131 /**
132 * Retrieves the string representation of this Version number.
133 *
134 * @return a string representing the version number in the format: "x.y.z...".
135 */
136 public String toString()
137 {
138 String sVersion = "";
139
140 for ( int i = 0; i < naTokens.length; i++ )
141 {
142 if ( i > 0 )
143 sVersion += ".";
144
145 sVersion += naTokens[i];
146 }
147
148 return sVersion;
149 }
150
151 /**
152 * Increases the current version adding 1 to the value in the last segment
153 * of the version number.
154 */
155 public void increment()
156 {
157 naTokens[naTokens.length - 1]++;
158 }
159
160 /**
161 * Decreases the current version substractiong 1 from the value in the last segment
162 * of the version number, if it is not equals to 0.
163 *
164 * @return true if version can be decreased; otherwise returns false.
165 */
166 public boolean decrement()
167 {
168 if ( naTokens[naTokens.length - 1] == 0 )
169 return false;
170
171 naTokens[naTokens.length - 1]--;
172
173 return true;
174 }
175
176 } // end of class