1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.util;
21
22 import org.apache.hadoop.hbase.classification.InterfaceAudience;
23 import org.apache.hadoop.hbase.HConstants;
24
25 @InterfaceAudience.Private
26 public class PrettyPrinter {
27
28 public enum Unit {
29 TIME_INTERVAL,
30 NONE
31 }
32
33 public static String format(final String value, final Unit unit) {
34 StringBuilder human = new StringBuilder();
35 switch (unit) {
36 case TIME_INTERVAL:
37 human.append(humanReadableTTL(Long.parseLong(value)));
38 break;
39 default:
40 human.append(value);
41 }
42 return human.toString();
43 }
44
45 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="ICAST_INTEGER_MULTIPLY_CAST_TO_LONG",
46 justification="Will not overflow")
47 private static String humanReadableTTL(final long interval){
48 StringBuilder sb = new StringBuilder();
49 int days, hours, minutes, seconds;
50
51
52 if (interval == Integer.MAX_VALUE) {
53 sb.append("FOREVER");
54 return sb.toString();
55 }
56 if (interval < HConstants.MINUTE_IN_SECONDS) {
57 sb.append(interval);
58 sb.append(" SECOND").append(interval == 1 ? "" : "S");
59 return sb.toString();
60 }
61
62 days = (int) (interval / HConstants.DAY_IN_SECONDS);
63 hours = (int) (interval - HConstants.DAY_IN_SECONDS * days) / HConstants.HOUR_IN_SECONDS;
64 minutes = (int) (interval - HConstants.DAY_IN_SECONDS * days
65 - HConstants.HOUR_IN_SECONDS * hours) / HConstants.MINUTE_IN_SECONDS;
66 seconds = (int) (interval - HConstants.DAY_IN_SECONDS * days
67 - HConstants.HOUR_IN_SECONDS * hours - HConstants.MINUTE_IN_SECONDS * minutes);
68
69 sb.append(interval);
70 sb.append(" SECONDS (");
71
72 if (days > 0) {
73 sb.append(days);
74 sb.append(" DAY").append(days == 1 ? "" : "S");
75 }
76
77 if (hours > 0 ) {
78 sb.append(days > 0 ? " " : "");
79 sb.append(hours);
80 sb.append(" HOUR").append(hours == 1 ? "" : "S");
81 }
82
83 if (minutes > 0) {
84 sb.append(days + hours > 0 ? " " : "");
85 sb.append(minutes);
86 sb.append(" MINUTE").append(minutes == 1 ? "" : "S");
87 }
88
89 if (seconds > 0) {
90 sb.append(days + hours + minutes > 0 ? " " : "");
91 sb.append(seconds);
92 sb.append(" SECOND").append(minutes == 1 ? "" : "S");
93 }
94
95 sb.append(")");
96
97 return sb.toString();
98 }
99
100 }