Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: nextapp/echoservlet/util/Version.java


1   /* 
2    * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3    * Copyright (C) 2002-2004 NextApp, Inc.
4    *
5    * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6    *
7    * The contents of this file are subject to the Mozilla Public License Version
8    * 1.1 (the "License"); you may not use this file except in compliance with
9    * the License. You may obtain a copy of the License at
10   * http://www.mozilla.org/MPL/
11   *
12   * Software distributed under the License is distributed on an "AS IS" basis,
13   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14   * for the specific language governing rights and limitations under the
15   * License.
16   *
17   * Alternatively, the contents of this file may be used under the terms of
18   * either the GNU General Public License Version 2 or later (the "GPL"), or
19   * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20   * in which case the provisions of the GPL or the LGPL are applicable instead
21   * of those above. If you wish to allow use of your version of this file only
22   * under the terms of either the GPL or the LGPL, and not to allow others to
23   * use your version of this file under the terms of the MPL, indicate your
24   * decision by deleting the provisions above and replace them with the notice
25   * and other provisions required by the GPL or the LGPL. If you do not delete
26   * the provisions above, a recipient may use your version of this file under
27   * the terms of any one of the MPL, the GPL or the LGPL.
28   */
29  
30  package nextapp.echoservlet.util;
31  
32  import java.util.StringTokenizer;
33  
34  /**
35   * Utility class for comparing version numbers in String format.  This class
36   * may be used to determine if a version number is greater than another to
37   * make a hypothesis about whether the version is compatible with a
38   * requirement.
39   */
40  public final class Version {
41  
42      /**
43       * Tests to see if a version number meets or exceeds a requirement.
44       * For example, if version 1.4 is required, 1.4, 1.4.0, 1.4.1, 1.5, 
45       * and 2.0 would meet the requirement.  0.7, 1, 1.0, 1.3, and 
46       * 1.3.5 would not.  Warning: At this time all versions must be numeric and
47       * separated by periods.  Any deviation will result in .0 versions, such as
48       * 1.5.3b being calculated as 1.5.0.
49       *
50       * @param requiredVersion The version number that is required.
51       * @param testVersion The version number that is available.
52       * @return True if the tested version is equal to or higher than the 
53       *         requirement.
54       */ 
55      public static final boolean meetsRequirement(String requiredVersion, String testVersion) {
56          StringTokenizer reqTokenizer = new StringTokenizer(requiredVersion, ".");
57          StringTokenizer testTokenizer = new StringTokenizer(testVersion, ".");
58          boolean value = true;
59          
60          int reqValue;
61          int testValue;
62          
63          while (value && (reqTokenizer.hasMoreTokens() || testTokenizer.hasMoreTokens())) {
64              if (reqTokenizer.hasMoreTokens()) {
65                  reqValue = getTokenValue(reqTokenizer.nextToken());
66              } else {
67                  reqValue = 0;
68              }
69              if (testTokenizer.hasMoreTokens()) {
70                  testValue = getTokenValue(testTokenizer.nextToken());
71              } else {
72                  testValue = 0;
73              }
74              
75              if (testValue > reqValue) {
76                  break;
77              } else if (testValue < reqValue) {
78                  value = false;
79              }
80          }
81          
82          return value;
83      }
84      
85      /**
86       * Returns a best guess translation of a string to an integer.
87       * If the string is not numeric, zero is returned.
88       *
89       * @param token A string that may or may not contain a number.
90       * @return The string's value, forcibly converted into a number.
91       */
92      private static final int getTokenValue(String token) {
93          int value;
94          
95          try {
96              value = Integer.parseInt(token);
97          } catch (NumberFormatException ex) {
98              value = 0;
99          }
100         
101         return value;
102     }
103     
104     /** Non-instantiable class. */
105     private Version() { }
106 }