KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > lang > VersionUtils


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v2.lang;
25
26 import com.mchange.v2.log.*;
27 import com.mchange.v1.util.StringTokenizerUtils;
28
29 public final class VersionUtils
30 {
31     private final static MLogger logger = MLog.getLogger( VersionUtils.class );
32
33     private final static int[] DFLT_VERSION_ARRAY = {1,1};
34
35     private final static int[] JDK_VERSION_ARRAY;
36     private final static int JDK_VERSION; //two digit int... 10 for 1.0, 11 for 1.1, etc.
37

38     private final static Integer JavaDoc NUM_BITS;
39
40     static
41     {
42     String JavaDoc vstr = System.getProperty( "java.version" );
43     int[] v;
44     if (vstr == null)
45         {
46         if (logger.isLoggable( MLevel.WARNING ))
47             logger.warning("Could not find java.version System property. Defaulting to JDK 1.1");
48         v = DFLT_VERSION_ARRAY;
49         }
50     else
51         {
52         try { v = extractVersionNumberArray( vstr, "._" ); }
53         catch ( NumberFormatException JavaDoc e )
54             {
55             if (logger.isLoggable( MLevel.WARNING ))
56                 logger.warning("java.version ''" + vstr + "'' could not be parsed. Defaulting to JDK 1.1.");
57             v = DFLT_VERSION_ARRAY;
58             }
59         }
60     int jdkv = 0;
61     if (v.length > 0)
62         jdkv += (v[0] * 10);
63     if (v.length > 1)
64         jdkv += (v[1]);
65
66     JDK_VERSION_ARRAY = v;
67     JDK_VERSION = jdkv;
68
69     //System.err.println( JDK_VERSION );
70

71     Integer JavaDoc tmpNumBits;
72     try
73         {
74         String JavaDoc numBitsStr = System.getProperty("sun.arch.data.model");
75         if (numBitsStr == null)
76             tmpNumBits = null;
77         else
78             tmpNumBits = new Integer JavaDoc( numBitsStr );
79         }
80     catch (Exception JavaDoc e)
81         {
82         tmpNumBits = null;
83         }
84
85     if (tmpNumBits == null || tmpNumBits.intValue() == 32 || tmpNumBits.intValue() == 64)
86         NUM_BITS = tmpNumBits;
87     else
88         {
89         if ( logger.isLoggable( MLevel.WARNING ) )
90             logger.warning("Determined a surprising jvmNumerOfBits: " + tmpNumBits +
91                    ". Setting jvmNumberOfBits to unknown (null).");
92         NUM_BITS = null;
93         }
94     }
95
96     /**
97      * @return null if unknown,
98      * an Integer (as of 2006 always 32 or 64)
99      * otherwise
100      */

101     public static Integer JavaDoc jvmNumberOfBits()
102     { return NUM_BITS; }
103
104     public static boolean isJavaVersion10()
105     { return (JDK_VERSION == 10); }
106
107     public static boolean isJavaVersion11()
108     { return (JDK_VERSION == 11); }
109
110     public static boolean isJavaVersion12()
111     { return (JDK_VERSION == 12); }
112
113     public static boolean isJavaVersion13()
114     { return (JDK_VERSION == 13); }
115
116     public static boolean isJavaVersion14()
117     { return (JDK_VERSION == 14); }
118
119     public static boolean isJavaVersion15()
120     { return (JDK_VERSION == 15); }
121
122     public static boolean isAtLeastJavaVersion10()
123     { return (JDK_VERSION >= 10); }
124
125     public static boolean isAtLeastJavaVersion11()
126     { return (JDK_VERSION >= 11); }
127
128     public static boolean isAtLeastJavaVersion12()
129     { return (JDK_VERSION >= 12); }
130
131     public static boolean isAtLeastJavaVersion13()
132     { return (JDK_VERSION >= 13); }
133
134     public static boolean isAtLeastJavaVersion14()
135     { return (JDK_VERSION >= 14); }
136
137     public static boolean isAtLeastJavaVersion15()
138     { return (JDK_VERSION >= 15); }
139
140     public static int[] extractVersionNumberArray(String JavaDoc versionString, String JavaDoc delims)
141     throws NumberFormatException JavaDoc
142     {
143     String JavaDoc[] intStrs = StringTokenizerUtils.tokenizeToArray( versionString, delims, false );
144     int len = intStrs.length;
145     int[] out = new int[ len ];
146     for (int i = 0; i < len; ++i)
147         out[i] = Integer.parseInt( intStrs[i] );
148     return out;
149     }
150
151     public boolean prefixMatches( int[] pfx, int[] fullVersion )
152     {
153     if (pfx.length > fullVersion.length)
154         return false;
155     else
156         {
157         for (int i = 0, len = pfx.length; i < len; ++i)
158             if (pfx[i] != fullVersion[i])
159             return false;
160         return true;
161         }
162     }
163
164     public static int lexicalCompareVersionNumberArrays(int[] a, int[] b)
165     {
166     int alen = a.length;
167     int blen = b.length;
168     for (int i = 0; i < alen; ++i)
169         {
170         if (i == blen)
171             return 1; //a is larger if they are the same to a point, but a has an extra version number
172
else if (a[i] > b[i])
173             return 1;
174         else if (a[i] < b[i])
175             return -1;
176         }
177     if (blen > alen)
178         return -1; //a is smaller if they are the same to a point, but b has an extra version number
179
else
180         return 0;
181     }
182 }
Popular Tags