1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.internal.util;
17 
18 import android.annotation.Nullable;
19 
20 /**
21  * Various numeric -> strings conversion.
22  *
23  * Test:
24  atest /android/pi-dev/frameworks/base/core/tests/coretests/src/com/android/internal/util/ParseUtilsTest.java
25  */
26 @android.ravenwood.annotation.RavenwoodKeepWholeClass
27 public final class ParseUtils {
ParseUtils()28     private ParseUtils() {
29     }
30 
31     /** Parse a value as a base-10 integer. */
parseInt(@ullable String value, int defValue)32     public static int parseInt(@Nullable String value, int defValue) {
33         return parseIntWithBase(value, 10, defValue);
34     }
35 
36     /** Parse a value as an integer of a given base. */
parseIntWithBase(@ullable String value, int base, int defValue)37     public static int parseIntWithBase(@Nullable String value, int base, int defValue) {
38         if (value == null) {
39             return defValue;
40         }
41         try {
42             return Integer.parseInt(value, base);
43         } catch (NumberFormatException e) {
44             return defValue;
45         }
46     }
47 
48     /** Parse a value as a base-10 long. */
parseLong(@ullable String value, long defValue)49     public static long parseLong(@Nullable String value, long defValue) {
50         return parseLongWithBase(value, 10, defValue);
51     }
52 
53     /** Parse a value as a long of a given base. */
parseLongWithBase(@ullable String value, int base, long defValue)54     public static long parseLongWithBase(@Nullable String value, int base, long defValue) {
55         if (value == null) {
56             return defValue;
57         }
58         try {
59             return Long.parseLong(value, base);
60         } catch (NumberFormatException e) {
61             return defValue;
62         }
63     }
64 
65     /** Parse a value as a float. */
parseFloat(@ullable String value, float defValue)66     public static float parseFloat(@Nullable String value, float defValue) {
67         if (value == null) {
68             return defValue;
69         }
70         try {
71             return Float.parseFloat(value);
72         } catch (NumberFormatException e) {
73             return defValue;
74         }
75     }
76 
77     /** Parse a value as a double. */
parseDouble(@ullable String value, double defValue)78     public static double parseDouble(@Nullable String value, double defValue) {
79         if (value == null) {
80             return defValue;
81         }
82         try {
83             return Double.parseDouble(value);
84         } catch (NumberFormatException e) {
85             return defValue;
86         }
87     }
88 
89     /** Parse a value as a boolean. */
parseBoolean(@ullable String value, boolean defValue)90     public static boolean parseBoolean(@Nullable String value, boolean defValue) {
91         if ("true".equals(value)) {
92             return true;
93         }
94         if ("false".equals(value)) {
95             return false;
96         }
97         return parseInt(value, defValue ? 1 : 0) != 0;
98     }
99 }
100