Home | History | Annotate | Download | only in impl
      1 /* GENERATED SOURCE. DO NOT MODIFY. */
      2 //  2016 and later: Unicode, Inc. and others.
      3 // License & terms of use: http://www.unicode.org/copyright.html#License
      4 /**
      5  *******************************************************************************
      6  * Copyright (C) 2001-2010, International Business Machines Corporation and    *
      7  * others. All Rights Reserved.                                                *
      8  *******************************************************************************
      9  */
     10 package android.icu.impl;
     11 
     12 import android.icu.util.VersionInfo;
     13 
     14 /**
     15  * @hide Only a subset of ICU is exposed in Android
     16  */
     17 public final class ICUDebug {
     18     private static String params;
     19     static {
     20         try {
     21             params = System.getProperty("ICUDebug");
     22         }
     23         catch (SecurityException e) {
     24         }
     25     }
     26     private static boolean debug = params != null;
     27     private static boolean help = debug && (params.equals("") || params.indexOf("help") != -1);
     28 
     29     static {
     30         if (debug) {
     31             System.out.println("\nICUDebug=" + params);
     32         }
     33     }
     34 
     35     public static final String javaVersionString = System.getProperty("java.version", "0");
     36     public static final boolean isJDK14OrHigher;
     37     public static final VersionInfo javaVersion;
     38 
     39     public static VersionInfo getInstanceLenient(String s) {
     40         // Extracting ASCII numbers up to 4 delimited by
     41         // any non digit characters
     42         int[] ver = new int[4];
     43         boolean numeric = false;
     44         int i = 0, vidx = 0;
     45         while (i < s.length()) {
     46             char c = s.charAt(i++);
     47             if (c < '0' || c > '9') {
     48                 if (numeric) {
     49                     if (vidx == 3) {
     50                         // up to 4 numbers
     51                         break;
     52                     }
     53                     numeric = false;
     54                     vidx++;
     55                 }
     56             } else {
     57                 if (numeric) {
     58                     ver[vidx] = ver[vidx] * 10 + (c - '0');
     59                     if (ver[vidx] > 255) {
     60                         // VersionInfo does not support numbers
     61                         // greater than 255.  In such case, we
     62                         // ignore the number and the rest
     63                         ver[vidx] = 0;
     64                         break;
     65                     }
     66                 } else {
     67                     numeric = true;
     68                     ver[vidx] = c - '0';
     69                 }
     70             }
     71         }
     72 
     73         return VersionInfo.getInstance(ver[0], ver[1], ver[2], ver[3]);
     74     }
     75 
     76     static {
     77         javaVersion = getInstanceLenient(javaVersionString);
     78 
     79         VersionInfo java14Version = VersionInfo.getInstance("1.4.0");
     80 
     81         isJDK14OrHigher = javaVersion.compareTo(java14Version) >= 0;
     82     }
     83 
     84     public static boolean enabled() {
     85         return debug;
     86     }
     87 
     88     public static boolean enabled(String arg) {
     89         if (debug) {
     90             boolean result = params.indexOf(arg) != -1;
     91             if (help) System.out.println("\nICUDebug.enabled(" + arg + ") = " + result);
     92             return result;
     93         }
     94         return false;
     95     }
     96 
     97     public static String value(String arg) {
     98         String result = "false";
     99         if (debug) {
    100             int index = params.indexOf(arg);
    101             if (index != -1) {
    102                 index += arg.length();
    103                 if (params.length() > index && params.charAt(index) == '=') {
    104                     index += 1;
    105                     int limit = params.indexOf(",", index);
    106                     result = params.substring(index, limit == -1 ? params.length() : limit);
    107                 } else {
    108                     result = "true";
    109                 }
    110             }
    111 
    112             if (help) System.out.println("\nICUDebug.value(" + arg + ") = " + result);
    113         }
    114         return result;
    115     }
    116 
    117 //    static public void main(String[] args) {
    118 //        // test
    119 //        String[] tests = {
    120 //            "1.3.0",
    121 //            "1.3.0_02",
    122 //            "1.3.1ea",
    123 //            "1.4.1b43",
    124 //            "___41___5",
    125 //            "x1.4.51xx89ea.7f",
    126 //            "1.6_2009",
    127 //            "10-100-1000-10000",
    128 //            "beta",
    129 //            "0",
    130 //        };
    131 //        for (int i = 0; i < tests.length; ++i) {
    132 //            System.out.println(tests[i] + " => " + getInstanceLenient(tests[i]));
    133 //        }
    134 //    }
    135 }
    136