01: /* Utils.java
02:
03: {{IS_NOTE
04: Purpose:
05:
06: Description:
07:
08: History:
09: Wed Jul 11 14:47:08 2007, Created by tomyeh
10: }}IS_NOTE
11:
12: Copyright (C) 2007 Potix Corporation. All Rights Reserved.
13:
14: {{IS_RIGHT
15: This program is distributed under GPL Version 2.0 in the hope that
16: it will be useful, but WITHOUT ANY WARRANTY.
17: }}IS_RIGHT
18: */
19: package org.zkoss.util;
20:
21: /**
22: * Generic utilities.
23: *
24: * @author tomyeh
25: * @since 3.0.0
26: */
27: public class Utils {
28: /** Returns a portion of the specified version in an integer,
29: * or 0 if no such portion exists.
30: *
31: * <p>For example, getSubversion(0) returns the so-called major version
32: * (2 in "2.4.0"), and getSubversion(1) returns the so-called
33: * minor version (4 in "2.4.0").
34: *
35: * <p>Note: alpha is consider as -500, beta -300, and rc -100.
36: * Moreover, beta3 is -497 (= -500 + 3) and rc5 -95 (-100 + 5)
37: *
38: * @param version the version. The version is assumed to
39: * a series of integer separated by a non-alphanemric separator.
40: * @param portion which portion of the version; starting from 0.
41: * If you want to retrieve the major verion, specify 0.
42: * @since 3.0.0
43: */
44: public static final int getSubversion(String version, int portion) {
45: if (portion < 0)
46: throw new IllegalArgumentException("Negative not allowed: "
47: + portion);
48:
49: final int len = version.length();
50: int j = 0;
51: while (--portion >= 0) {
52: j = nextVerSeparator(version, j) + 1;
53: if (j >= len)
54: return 0; //no such portion
55: }
56:
57: String s = version.substring(j, nextVerSeparator(version, j));
58: try {
59: return Integer.parseInt(s);
60: } catch (Throwable ex) { //eat
61: }
62:
63: s = s.toLowerCase();
64: final int base;
65: if (s.startsWith("rc")) {
66: base = -100;
67: j = 2;
68: } else if (s.startsWith("beta")) {
69: base = -300;
70: j = 4;
71: } else if (s.startsWith("alpha")) {
72: base = -500;
73: j = 5;
74: } else {
75: return 0; //unknown
76: }
77: if (j < s.length()) {
78: try {
79: return base + Integer.parseInt(s.substring(j));
80: } catch (Throwable ex) { //eat
81: }
82: }
83: return base;
84: }
85:
86: private static final int nextVerSeparator(String version, int from) {
87: for (final int len = version.length(); from < len; ++from) {
88: final char cc = version.charAt(from);
89: if ((cc < '0' || cc > '9') && (cc < 'a' || cc > 'z')
90: && (cc < 'A' || cc > 'Z'))
91: break;
92: }
93: return from;
94: }
95: }
|