View Javadoc

1   /*
2    * Copyright 2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.jjar;
18  
19  /**
20   *  simple class to handle JJAR version information
21   *
22   *  should be of format 
23   *     <major>.<minor>-<modifier>
24   *
25   *  @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
26   *  @version $Id: Version.java 155454 2005-02-26 13:23:34Z dirkv $
27   */
28  public class Version
29  {
30  	private static String VER_SEP = ".";
31  	private static String MOD_SEP = "-";
32  	
33  	private boolean proper = false;
34  	private String version;
35  	private int major = -1;
36  	private int minor = -1;
37  	private String modifier = "";
38  
39  	public Version( String version )
40  	{
41  		this.version = version;
42  		parse();
43  	}
44  
45  	public boolean isProper()
46  	{
47          return proper;
48  	}
49  
50      public String toString()
51      {
52          return version;
53      }
54  
55      public int getMajor()
56      {
57          return this.major;
58      }
59  
60      public int getMinor()
61      {
62          return this.minor;
63      }
64  
65      public String getModifier()
66      {
67          return this.modifier;
68      }
69  
70      public boolean equals( String version )
71      {
72          Version v = new Version( version );
73  
74          return equals( v );
75      }
76  
77      public boolean equals( Version v )
78      {
79          return (v.getMajor() == major  && v.getMinor() == minor && v.getModifier().equals( modifier));
80      }
81            
82  	private void parse()
83  	{
84  		/*
85  		 *  first, look for the separators
86  		 */
87  
88  		int wherever = version.indexOf( VER_SEP );
89  		int wheremod = version.indexOf( MOD_SEP );
90  
91  		/*
92  		 *  we must have the VER_SEP or it's not 
93  		 *  properly formatted
94  		 */
95  
96  		if (wherever != -1 )
97  		{
98  			/*
99  			 *  major : get the 'left' of VER_SEP
100 			 */
101 			
102 			String maj = version.substring( 0,wherever);
103 			
104 			try
105 			{
106 				major = Integer.parseInt( maj );
107 				proper = true;
108 			}
109 			catch( Exception e )
110 			{}
111 
112             /*
113              *  minor :
114              */
115 
116 			String min = version.substring( wherever + 1, 
117 											(wheremod == -1 ? version.length() :  wheremod ) );
118 
119             try
120 			{
121 				minor = Integer.parseInt( min );
122 				proper = true;
123 			}
124 			catch( Exception e )
125 			{}
126 		}
127 
128         if ( wheremod != -1 )
129         {
130             /*
131              *  now the modifier
132              */
133 
134             modifier = version.substring( wheremod + 1 );
135         }
136 		
137 	}
138 
139     public static void main( String args[])
140     {
141         Version jv = new Version( "1.1");
142         System.out.println( jv.getMajor() + " : " + jv.getMinor() + " : " + jv.getModifier());
143 
144         jv = new Version( "2.1-rc1");
145         System.out.println( jv.getMajor() + " : " + jv.getMinor() + " : " + jv.getModifier());
146     }
147 
148 }
149 
150 
151