summaryrefslogtreecommitdiff
path: root/src/com/android/loganalysis/util/NumberFormattingUtil.java
blob: 8652c2ee517e4c02070e8ff0b8b65c6ed16e3347 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.loganalysis.util;

import java.util.concurrent.TimeUnit;



/**
 * Utility methods for number formatting
 */
public class NumberFormattingUtil {

    private NumberFormattingUtil() {
    }

    /**
     * Convert days/hours/mins/secs/msecs into milliseconds.
     */
    public static long getMs(long days, long hours, long mins, long secs, long msecs) {
        return (((24 * days + hours) * 60 + mins) * 60 + secs) * 1000 + msecs;
    }

    /**
     * Convert hours/mins/secs/msecs into milliseconds.
     */
    public static long getMs(long hours, long mins, long secs, long msecs) {
        return getMs(0, hours, mins, secs, msecs);
    }

    /**
     * Parses a string into a long, or returns 0 if the string is null.
     *
     * @param s a {@link String} containing the long representation to be parsed
     * @return the long represented by the argument in decimal, or 0 if the string is {@code null}.
     * @throws NumberFormatException if the string is not {@code null} or does not contain a
     * parsable long.
     */
    public static long parseLongOrZero(String s) throws NumberFormatException {
        if (s == null) {
            return 0;
        }
        return Long.parseLong(s);
    }

    /**
     * Parses a string into a int, or returns 0 if the string is null.
     *
     * @param s a {@link String} containing the int representation to be parsed
     * @return the int represented by the argument in decimal, or 0 if the string is {@code null}.
     * @throws NumberFormatException if the string is not {@code null} or does not contain a
     * parsable long.
     */
    public static int parseIntOrZero(String s) throws NumberFormatException {
        if (s == null) {
            return 0;
        }
        return Integer.parseInt(s);
    }

    /**
     * Converts milliseconds to days/hours/mins/secs
     *
     * @param ms elapsed time in ms
     * @return the duration in days/hours/mins/secs
     */
    public static String getDuration(long ms) {
        if (ms <= 0) {
            return "Not a valid time";
        }
        final long days = TimeUnit.MILLISECONDS.toDays(ms);
        final long hrs = TimeUnit.MILLISECONDS.toHours(ms - TimeUnit.DAYS.toMillis(days));
        final long mins = TimeUnit.MILLISECONDS.toMinutes(ms - TimeUnit.DAYS.toMillis(days)
                - TimeUnit.HOURS.toMillis(hrs));
        final long secs = TimeUnit.MILLISECONDS.toSeconds(ms - TimeUnit.DAYS.toMillis(days)
                - TimeUnit.HOURS.toMillis(hrs) - TimeUnit.MINUTES.toMillis(mins));

        return String.format("%dd %dh %dm %ds", days, hrs, mins, secs);
    }
}