1 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; 38 private final static Integer NUM_BITS; 39 40 static 41 { 42 String 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 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 71 Integer tmpNumBits; 72 try 73 { 74 String numBitsStr = System.getProperty("sun.arch.data.model"); 75 if (numBitsStr == null) 76 tmpNumBits = null; 77 else 78 tmpNumBits = new Integer ( numBitsStr ); 79 } 80 catch (Exception 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 101 public static Integer 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 versionString, String delims) 141 throws NumberFormatException 142 { 143 String [] 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; 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; else 180 return 0; 181 } 182 } | Popular Tags |