View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.util;
21  
22  import org.apache.hadoop.classification.InterfaceAudience;
23  import org.apache.hadoop.classification.InterfaceStability;
24  import org.apache.hadoop.hbase.HConstants;
25  
26  @InterfaceAudience.Public
27  @InterfaceStability.Evolving
28  public class PrettyPrinter {
29  
30    public enum Unit {
31      TIME_INTERVAL,
32      NONE
33    }
34  
35    public static String format(final String value, final Unit unit) {
36      StringBuilder human = new StringBuilder();
37      switch (unit) {
38        case TIME_INTERVAL:
39          human.append(humanReadableTTL(Long.valueOf(value)));
40          break;
41        default:
42          human.append(value);
43      }
44      return human.toString();
45    }
46  
47  
48    private static String humanReadableTTL(final long interval){
49      StringBuilder sb = new StringBuilder();
50      int days, hours, minutes, seconds;
51  
52      // edge cases first
53      if (interval == Integer.MAX_VALUE) {
54        sb.append("FOREVER");
55        return sb.toString();
56      }
57      if (interval < HConstants.MINUTE_IN_SECONDS) {
58        sb.append(interval);
59        sb.append(" SECOND").append(interval == 1 ? "" : "S");
60        return sb.toString();
61      }
62  
63      days  =   (int) (interval / HConstants.DAY_IN_SECONDS);
64      hours =   (int) (interval - HConstants.DAY_IN_SECONDS * days) / HConstants.HOUR_IN_SECONDS;
65      minutes = (int) (interval - HConstants.DAY_IN_SECONDS * days
66          - HConstants.HOUR_IN_SECONDS * hours) / HConstants.MINUTE_IN_SECONDS;
67      seconds = (int) (interval - HConstants.DAY_IN_SECONDS * days
68          - HConstants.HOUR_IN_SECONDS * hours - HConstants.MINUTE_IN_SECONDS * minutes);
69  
70      sb.append(interval);
71      sb.append(" SECONDS (");
72  
73      if (days > 0) {
74        sb.append(days);
75        sb.append(" DAY").append(days == 1 ? "" : "S");
76      }
77  
78      if (hours > 0 ) {
79        sb.append(days > 0 ? " " : "");
80        sb.append(hours);
81        sb.append(" HOUR").append(hours == 1 ? "" : "S");
82      }
83  
84      if (minutes > 0) {
85        sb.append(days + hours > 0 ? " " : "");
86        sb.append(minutes);
87        sb.append(" MINUTE").append(minutes == 1 ? "" : "S");
88      }
89  
90      if (seconds > 0) {
91        sb.append(days + hours + minutes > 0 ? " " : "");
92        sb.append(seconds);
93        sb.append(" SECOND").append(minutes == 1 ? "" : "S");
94      }
95  
96      sb.append(")");
97  
98      return sb.toString();
99    }
100 
101 }