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 public final class ParseUtils {
ParseUtils()27     private ParseUtils() {
28     }
29 
30     /** Parse a value as a base-10 integer. */
parseInt(@ullable String value, int defValue)31     public static int parseInt(@Nullable String value, int defValue) {
32         return parseIntWithBase(value, 10, defValue);
33     }
34 
35     /** Parse a value as an integer of a given base. */
parseIntWithBase(@ullable String value, int base, int defValue)36     public static int parseIntWithBase(@Nullable String value, int base, int defValue) {
37         if (value == null) {
38             return defValue;
39         }
40         try {
41             return Integer.parseInt(value, base);
42         } catch (NumberFormatException e) {
43             return defValue;
44         }
45     }
46 
47     /** Parse a value as a base-10 long. */
parseLong(@ullable String value, long defValue)48     public static long parseLong(@Nullable String value, long defValue) {
49         return parseLongWithBase(value, 10, defValue);
50     }
51 
52     /** Parse a value as a long of a given base. */
parseLongWithBase(@ullable String value, int base, long defValue)53     public static long parseLongWithBase(@Nullable String value, int base, long defValue) {
54         if (value == null) {
55             return defValue;
56         }
57         try {
58             return Long.parseLong(value, base);
59         } catch (NumberFormatException e) {
60             return defValue;
61         }
62     }
63 
64     /** Parse a value as a float. */
parseFloat(@ullable String value, float defValue)65     public static float parseFloat(@Nullable String value, float defValue) {
66         if (value == null) {
67             return defValue;
68         }
69         try {
70             return Float.parseFloat(value);
71         } catch (NumberFormatException e) {
72             return defValue;
73         }
74     }
75 
76     /** Parse a value as a double. */
parseDouble(@ullable String value, double defValue)77     public static double parseDouble(@Nullable String value, double defValue) {
78         if (value == null) {
79             return defValue;
80         }
81         try {
82             return Double.parseDouble(value);
83         } catch (NumberFormatException e) {
84             return defValue;
85         }
86     }
87 
88     /** Parse a value as a boolean. */
parseBoolean(@ullable String value, boolean defValue)89     public static boolean parseBoolean(@Nullable String value, boolean defValue) {
90         if ("true".equals(value)) {
91             return true;
92         }
93         if ("false".equals(value)) {
94             return false;
95         }
96         return parseInt(value, defValue ? 1 : 0) != 0;
97     }
98 }
99