1 /*
2  * Copyright (C) 2015 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.loganalysis.util;
17 
18 import java.util.concurrent.TimeUnit;
19 
20 
21 
22 /**
23  * Utility methods for number formatting
24  */
25 public class NumberFormattingUtil {
26 
NumberFormattingUtil()27     private NumberFormattingUtil() {
28     }
29 
30     /**
31      * Convert days/hours/mins/secs/msecs into milliseconds.
32      */
getMs(long days, long hours, long mins, long secs, long msecs)33     public static long getMs(long days, long hours, long mins, long secs, long msecs) {
34         return (((24 * days + hours) * 60 + mins) * 60 + secs) * 1000 + msecs;
35     }
36 
37     /**
38      * Convert hours/mins/secs/msecs into milliseconds.
39      */
getMs(long hours, long mins, long secs, long msecs)40     public static long getMs(long hours, long mins, long secs, long msecs) {
41         return getMs(0, hours, mins, secs, msecs);
42     }
43 
44     /**
45      * Parses a string into a long, or returns 0 if the string is null.
46      *
47      * @param s a {@link String} containing the long representation to be parsed
48      * @return the long represented by the argument in decimal, or 0 if the string is {@code null}.
49      * @throws NumberFormatException if the string is not {@code null} or does not contain a
50      * parsable long.
51      */
parseLongOrZero(String s)52     public static long parseLongOrZero(String s) throws NumberFormatException {
53         if (s == null) {
54             return 0;
55         }
56         return Long.parseLong(s);
57     }
58 
59     /**
60      * Parses a string into a int, or returns 0 if the string is null.
61      *
62      * @param s a {@link String} containing the int representation to be parsed
63      * @return the int represented by the argument in decimal, or 0 if the string is {@code null}.
64      * @throws NumberFormatException if the string is not {@code null} or does not contain a
65      * parsable long.
66      */
parseIntOrZero(String s)67     public static int parseIntOrZero(String s) throws NumberFormatException {
68         if (s == null) {
69             return 0;
70         }
71         return Integer.parseInt(s);
72     }
73 
74     /**
75      * Converts milliseconds to days/hours/mins/secs
76      *
77      * @param ms elapsed time in ms
78      * @return the duration in days/hours/mins/secs
79      */
getDuration(long ms)80     public static String getDuration(long ms) {
81         if (ms <= 0) {
82             return "Not a valid time";
83         }
84         final long days = TimeUnit.MILLISECONDS.toDays(ms);
85         final long hrs = TimeUnit.MILLISECONDS.toHours(ms - TimeUnit.DAYS.toMillis(days));
86         final long mins = TimeUnit.MILLISECONDS.toMinutes(ms - TimeUnit.DAYS.toMillis(days)
87                 - TimeUnit.HOURS.toMillis(hrs));
88         final long secs = TimeUnit.MILLISECONDS.toSeconds(ms - TimeUnit.DAYS.toMillis(days)
89                 - TimeUnit.HOURS.toMillis(hrs) - TimeUnit.MINUTES.toMillis(mins));
90 
91         return String.format("%dd %dh %dm %ds", days, hrs, mins, secs);
92     }
93 }
94 
95