001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v2.lang;
024:
025: import com.mchange.v2.log.*;
026: import com.mchange.v1.util.StringTokenizerUtils;
027:
028: public final class VersionUtils {
029: private final static MLogger logger = MLog
030: .getLogger(VersionUtils.class);
031:
032: private final static int[] DFLT_VERSION_ARRAY = { 1, 1 };
033:
034: private final static int[] JDK_VERSION_ARRAY;
035: private final static int JDK_VERSION; //two digit int... 10 for 1.0, 11 for 1.1, etc.
036:
037: private final static Integer NUM_BITS;
038:
039: static {
040: String vstr = System.getProperty("java.version");
041: int[] v;
042: if (vstr == null) {
043: if (logger.isLoggable(MLevel.WARNING))
044: logger
045: .warning("Could not find java.version System property. Defaulting to JDK 1.1");
046: v = DFLT_VERSION_ARRAY;
047: } else {
048: try {
049: v = extractVersionNumberArray(vstr, "._");
050: } catch (NumberFormatException e) {
051: if (logger.isLoggable(MLevel.WARNING))
052: logger
053: .warning("java.version ''"
054: + vstr
055: + "'' could not be parsed. Defaulting to JDK 1.1.");
056: v = DFLT_VERSION_ARRAY;
057: }
058: }
059: int jdkv = 0;
060: if (v.length > 0)
061: jdkv += (v[0] * 10);
062: if (v.length > 1)
063: jdkv += (v[1]);
064:
065: JDK_VERSION_ARRAY = v;
066: JDK_VERSION = jdkv;
067:
068: //System.err.println( JDK_VERSION );
069:
070: Integer tmpNumBits;
071: try {
072: String numBitsStr = System
073: .getProperty("sun.arch.data.model");
074: if (numBitsStr == null)
075: tmpNumBits = null;
076: else
077: tmpNumBits = new Integer(numBitsStr);
078: } catch (Exception e) {
079: tmpNumBits = null;
080: }
081:
082: if (tmpNumBits == null || tmpNumBits.intValue() == 32
083: || tmpNumBits.intValue() == 64)
084: NUM_BITS = tmpNumBits;
085: else {
086: if (logger.isLoggable(MLevel.WARNING))
087: logger
088: .warning("Determined a surprising jvmNumerOfBits: "
089: + tmpNumBits
090: + ". Setting jvmNumberOfBits to unknown (null).");
091: NUM_BITS = null;
092: }
093: }
094:
095: /**
096: * @return null if unknown,
097: * an Integer (as of 2006 always 32 or 64)
098: * otherwise
099: */
100: public static Integer jvmNumberOfBits() {
101: return NUM_BITS;
102: }
103:
104: public static boolean isJavaVersion10() {
105: return (JDK_VERSION == 10);
106: }
107:
108: public static boolean isJavaVersion11() {
109: return (JDK_VERSION == 11);
110: }
111:
112: public static boolean isJavaVersion12() {
113: return (JDK_VERSION == 12);
114: }
115:
116: public static boolean isJavaVersion13() {
117: return (JDK_VERSION == 13);
118: }
119:
120: public static boolean isJavaVersion14() {
121: return (JDK_VERSION == 14);
122: }
123:
124: public static boolean isJavaVersion15() {
125: return (JDK_VERSION == 15);
126: }
127:
128: public static boolean isAtLeastJavaVersion10() {
129: return (JDK_VERSION >= 10);
130: }
131:
132: public static boolean isAtLeastJavaVersion11() {
133: return (JDK_VERSION >= 11);
134: }
135:
136: public static boolean isAtLeastJavaVersion12() {
137: return (JDK_VERSION >= 12);
138: }
139:
140: public static boolean isAtLeastJavaVersion13() {
141: return (JDK_VERSION >= 13);
142: }
143:
144: public static boolean isAtLeastJavaVersion14() {
145: return (JDK_VERSION >= 14);
146: }
147:
148: public static boolean isAtLeastJavaVersion15() {
149: return (JDK_VERSION >= 15);
150: }
151:
152: public static int[] extractVersionNumberArray(String versionString,
153: String delims) throws NumberFormatException {
154: String[] intStrs = StringTokenizerUtils.tokenizeToArray(
155: versionString, delims, false);
156: int len = intStrs.length;
157: int[] out = new int[len];
158: for (int i = 0; i < len; ++i)
159: out[i] = Integer.parseInt(intStrs[i]);
160: return out;
161: }
162:
163: public boolean prefixMatches(int[] pfx, int[] fullVersion) {
164: if (pfx.length > fullVersion.length)
165: return false;
166: else {
167: for (int i = 0, len = pfx.length; i < len; ++i)
168: if (pfx[i] != fullVersion[i])
169: return false;
170: return true;
171: }
172: }
173:
174: public static int lexicalCompareVersionNumberArrays(int[] a, int[] b) {
175: int alen = a.length;
176: int blen = b.length;
177: for (int i = 0; i < alen; ++i) {
178: if (i == blen)
179: return 1; //a is larger if they are the same to a point, but a has an extra version number
180: else if (a[i] > b[i])
181: return 1;
182: else if (a[i] < b[i])
183: return -1;
184: }
185: if (blen > alen)
186: return -1; //a is smaller if they are the same to a point, but b has an extra version number
187: else
188: return 0;
189: }
190: }
|