View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.util;
21  
22  import java.util.StringTokenizer;
23  
24  /***
25   * This class can parse an XNap version string.
26   */
27  public class VersionParser implements Comparable {
28      
29      // --- Data Field(s) ---
30  
31      private String version;
32  
33      // --- Constructor(s) ---
34  
35      /***
36       * Constructs a new version parser that parses <code>version</code>.
37       */
38      public VersionParser(String version) 
39      {
40  		this.version = version;
41      }
42  
43      // --- Method(s) ---
44  
45      /***
46       * Compares version <code>v1</code> with version <code>v2</code>.
47       *
48       * @return 1, if v1 is more recent; 0, if v1 is equal to v2; -1, if v2
49       *         is more recent
50       * @see #compareTo(Object)
51       */
52      public static int compare(String v1, String v2) 
53      {
54  		int result = (new VersionParser(v1)).compareTo(new VersionParser(v2));
55  		return result;
56      }
57  
58      /***
59       * Compares this VersionParser to <code>o</code>.
60       *
61       * @return 1, if v1 is more recent; 0, if v1 is equal to v2; -1, if v2
62       *         is more recent
63       */
64      public int compareTo(Object o) 
65      {
66  		VersionParser vp = (VersionParser)o;
67  
68  		StringTokenizer left = new StringTokenizer(version, ".-_: ");
69  		StringTokenizer right = new StringTokenizer(vp.version, ".-_: ");
70  
71  		int result = 0;
72  		while (result == 0) {
73  			// check if one version is longer than the other one
74  			if (!left.hasMoreTokens() && !right.hasMoreTokens()) {
75  				return 0;
76  			}
77  			else if (!right.hasMoreTokens()) {
78  				return 1;
79  			}
80  			else if (!left.hasMoreTokens()) {
81  				return -1;
82  			}
83  
84  			result = compareTokens(left.nextToken(), right.nextToken());
85  		}
86  		return result;
87      }
88  
89  	private static int compareTokens(String left, String right)
90  	{
91  		int leftNumber = getNumberPrefix(left);
92  		int rightNumber = getNumberPrefix(right);
93  
94  		int result = leftNumber - rightNumber;
95  		if (result != 0) {
96  			return result;
97  		}
98  
99  		return left.compareTo(right);
100 	}
101 
102 	private static int getNumberPrefix(String s)
103 	{
104 		int i = 0;
105 		while (i < s.length() && s.charAt(i) >= '0' && s.charAt(i) <= '9') {
106 			i++;
107 		}
108 		return (i > 0) ? Integer.parseInt(s.substring(0, i)) : -1;
109 	}
110 
111     public String toString()
112     {
113 		return version;
114     }
115 
116 }
117