summaryrefslogtreecommitdiffstats
path: root/src/libs/xpcom18a4/java/src/org/mozilla/xpcom/VersionComparator.java
blob: f9fb1058c1d6b403fe4050be275d8e793b3b5708 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Java XPCOM Bindings.
 *
 * The Initial Developer of the Original Code is
 * IBM Corporation.
 * Portions created by the Initial Developer are Copyright (C) 2005
 * IBM Corporation. All Rights Reserved.
 *
 * Contributor(s):
 *   Javier Pedemonte (jhpedemonte@gmail.com)
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

package org.mozilla.xpcom;

import java.util.Enumeration;
import java.util.StringTokenizer;

import org.mozilla.interfaces.nsISupports;
/* import org.mozilla.interfaces.nsIVersionComparator; */


/**
 * Version strings are dot-separated sequences of version-parts.
 * <p>
 * A version-part consists of up to four parts, all of which are optional:
 * <br><code>
 * &lt;number-a&gt;&lt;string-b&gt;&lt;number-c&gt;
 * &lt;string-d (everything else)&gt;
 * </code> <p>
 * A version-part may also consist of a single asterisk "*" which indicates
 * "infinity".
 * <p>
 * Numbers are base-10, and are zero if left out.
 * Strings are compared bytewise.
 * <p>
 * For additional backwards compatibility, if "string-b" is "+" then
 * "number-a" is incremented by 1 and "string-b" becomes "pre".
 * <p> <pre>
 * 1.0pre1
 * < 1.0pre2
 *   < 1.0 == 1.0.0 == 1.0.0.0
 *     < 1.1pre == 1.1pre0 == 1.0+
 *       < 1.1pre1a
 *         < 1.1pre1
 *           < 1.1pre10a
 *             < 1.1pre10
 * </pre>
 * Although not required by this interface, it is recommended that
 * numbers remain within the limits of a signed char, i.e. -127 to 128.
 */
public class VersionComparator implements nsISupports /* implements nsIVersionComparator */ {

  public nsISupports queryInterface(String aIID) {
    return Mozilla.queryInterface(this, aIID);
  }

  /**
   * Compare two version strings
   * @param A   a version string
   * @param B   a version string
   * @return a value less than 0 if A < B;
   *         the value 0 if A == B;
   *         or a value greater than 0 if A > B
   */
  public int compare(String A, String B) {
    int result;
    String a = A, b = B;

    do {
      VersionPart va = new VersionPart();
      VersionPart vb = new VersionPart();
      a = parseVersionPart(a, va);
      b = parseVersionPart(b, vb);

      result = compareVersionPart(va, vb);
      if (result != 0) {
        break;
      }
    } while (a != null || b != null);

    return result;
  }

  private class VersionPart {
    int     numA = 0;
    String  strB;
    int     numC = 0;
    String  extraD;
  }

  private static String parseVersionPart(String aVersion, VersionPart result) {
    if (aVersion == null || aVersion.length() == 0) {
      return aVersion;
    }

    StringTokenizer tok = new StringTokenizer(aVersion.trim(), ".");
    String part = tok.nextToken();

    if (part.equals("*")) {
      result.numA = Integer.MAX_VALUE;
      result.strB = "";
    } else {
      VersionPartTokenizer vertok = new VersionPartTokenizer(part);
      try {
        result.numA = Integer.parseInt(vertok.nextToken());
      } catch (NumberFormatException e) {
        // parsing error; default to zero like 'strtol' C function
        result.numA = 0;
      }

      if (vertok.hasMoreElements()) {
        String str = vertok.nextToken();

        // if part is of type "<num>+"
        if (str.charAt(0) == '+') {
          result.numA++;
          result.strB = "pre";
        } else {
          // else if part is of type "<num><alpha>..."
          result.strB = str;

          if (vertok.hasMoreTokens()) {
            try {
              result.numC = Integer.parseInt(vertok.nextToken());
            } catch (NumberFormatException e) {
              // parsing error; default to zero like 'strtol' C function
              result.numC = 0;
            }
            if (vertok.hasMoreTokens()) {
              result.extraD = vertok.getRemainder();
            }
          }
        }
      }
    }

    if (tok.hasMoreTokens()) {
      // return everything after "."
      return aVersion.substring(part.length() + 1);
    }
    return null;
  }

  private int compareVersionPart(VersionPart va, VersionPart vb) {
    int res = compareInt(va.numA, vb.numA);
    if (res != 0) {
      return res;
    }

    res = compareString(va.strB, vb.strB);
    if (res != 0) {
      return res;
    }

    res = compareInt(va.numC, vb.numC);
    if (res != 0) {
      return res;
    }

    return compareString(va.extraD, vb.extraD);
  }

  private int compareInt(int n1, int n2) {
    return n1 - n2;
  }

  private int compareString(String str1, String str2) {
    // any string is *before* no string
    if (str1 == null) {
      return (str2 != null) ? 1 : 0;
    }

    if (str2 == null) {
      return -1;
    }

    return str1.compareTo(str2);
  }

}

/**
 * Specialized tokenizer for Mozilla version strings.  A token can
 * consist of one of the four sections of a version string: <code>
 * &lt;number-a&gt;&lt;string-b&gt;&lt;number-c&gt;
 * &lt;string-d (everything else)&gt;</code>.
 */
class VersionPartTokenizer implements Enumeration {

  String part;

  public VersionPartTokenizer(String aPart) {
    part = aPart;
  }

  public boolean hasMoreElements() {
    return part.length() != 0;
  }

  public boolean hasMoreTokens() {
    return part.length() != 0;
  }

  public Object nextElement() {
    if (part.matches("[\\+\\-]?[0-9].*")) {
      // if string starts with a number...
      int index = 0;
      if (part.charAt(0) == '+' || part.charAt(0) == '-') {
        index = 1;
      }

      while (index < part.length() && Character.isDigit(part.charAt(index))) {
        index++;
      }

      String numPart = part.substring(0, index);
      part = part.substring(index);
      return numPart;
    } else {
      // ... or if this is the non-numeric part of version string
      int index = 0;
      while (index < part.length() && !Character.isDigit(part.charAt(index))) {
        index++;
      }

      String alphaPart = part.substring(0, index);
      part = part.substring(index);
      return alphaPart;
    }
  }

  public String nextToken() {
    return (String) nextElement();
  }

  /**
   * Returns what remains of the original string, without tokenization.  This
   * method is useful for getting the <code>&lt;string-d (everything else)&gt;
   * </code> section of a version string.
   *
   * @return remaining version string
   */
  public String getRemainder() {
    return part;
  }

}