1 55 package org.apache.avalon.framework; 56 57 import java.io.Serializable ; 58 import java.util.StringTokenizer ; 59 60 92 public final class Version 93 implements Serializable 94 { 95 private int m_major; 96 private int m_minor; 97 private int m_micro; 98 99 110 public static Version getVersion( final String version ) 111 throws NumberFormatException , IllegalArgumentException 112 { 113 final StringTokenizer tokenizer = new StringTokenizer ( version, "." ); 114 final String [] levels = new String [ tokenizer.countTokens() ]; 115 for( int i = 0; i < levels.length; i++ ) 116 { 117 levels[ i ] = tokenizer.nextToken(); 118 } 119 120 if( 0 == levels.length || 3 < levels.length ) 121 { 122 throw new IllegalArgumentException ( "Malformed version string " + version ); 123 } 124 125 final int major = Integer.parseInt( levels[ 0 ] ); 126 127 int minor = 0; 128 if( 1 < levels.length ) 129 { 130 minor = Integer.parseInt( levels[ 1 ] ); 131 } 132 133 int micro = 0; 134 if( 2 < levels.length ) 135 { 136 micro = Integer.parseInt( levels[ 2 ] ); 137 } 138 139 return new Version( major, minor, micro ); 140 } 141 142 150 public Version( final int major, final int minor, final int micro ) 151 { 152 m_major = major; 153 m_minor = minor; 154 m_micro = micro; 155 } 156 157 163 public int getMajor() 164 { 165 return m_major; 166 } 167 168 174 public int getMinor() 175 { 176 return m_minor; 177 } 178 179 185 public int getMicro() 186 { 187 return m_micro; 188 } 189 190 201 public boolean equals( final Version other ) 202 { 203 if( m_major != other.m_major ) 204 { 205 return false; 206 } 207 else if( m_minor != other.m_minor ) 208 { 209 return false; 210 } 211 else if( m_micro != other.m_micro ) 212 { 213 return false; 214 } 215 else 216 { 217 return true; 218 } 219 } 220 221 229 public boolean equals( final Object other ) 230 { 231 if( other instanceof Version ) 232 { 233 return equals( (Version)other ); 234 } 235 else 236 { 237 return false; 238 } 239 } 240 241 270 public boolean complies( final Version other ) 271 { 272 if( m_major != other.m_major ) 273 { 274 return false; 275 } 276 else if( m_minor < other.m_minor ) 277 { 278 return false; 280 } 281 else if( m_minor == other.m_minor 282 && m_micro < other.m_micro ) 283 { 284 return false; 287 } 288 else 289 { 290 return true; 291 } 292 } 293 294 299 public String toString() 300 { 301 return m_major + "." + m_minor + "." + m_micro; 302 } 303 } 304 | Popular Tags |