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.hbase.classification.InterfaceAudience;
23  import org.apache.hadoop.hbase.classification.InterfaceStability;
24  import org.apache.hadoop.hbase.HConstants;
25  
26  @InterfaceAudience.Public // NOTE: This switches to Private in later versions
27  @InterfaceStability.Evolving
28  public class PrettyPrinter {
29  
30    @InterfaceAudience.Public
31    @InterfaceStability.Evolving
32    public enum Unit {
33      TIME_INTERVAL,
34      NONE
35    }
36  
37    public static String format(final String value, final Unit unit) {
38      StringBuilder human = new StringBuilder();
39      switch (unit) {
40        case TIME_INTERVAL:
41          human.append(humanReadableTTL(Long.valueOf(value)));
42          break;
43        default:
44          human.append(value);
45      }
46      return human.toString();
47    }
48  
49  
50    private static String humanReadableTTL(final long interval){
51      StringBuilder sb = new StringBuilder();
52      int days, hours, minutes, seconds;
53  
54      // edge cases first
55      if (interval == Integer.MAX_VALUE) {
56        sb.append("FOREVER");
57        return sb.toString();
58      }
59      if (interval < HConstants.MINUTE_IN_SECONDS) {
60        sb.append(interval);
61        sb.append(" SECOND").append(interval == 1 ? "" : "S");
62        return sb.toString();
63      }
64  
65      days  =   (int) (interval / HConstants.DAY_IN_SECONDS);
66      hours =   (int) (interval - HConstants.DAY_IN_SECONDS * days) / HConstants.HOUR_IN_SECONDS;
67      minutes = (int) (interval - HConstants.DAY_IN_SECONDS * days
68          - HConstants.HOUR_IN_SECONDS * hours) / HConstants.MINUTE_IN_SECONDS;
69      seconds = (int) (interval - HConstants.DAY_IN_SECONDS * days
70          - HConstants.HOUR_IN_SECONDS * hours - HConstants.MINUTE_IN_SECONDS * minutes);
71  
72      sb.append(interval);
73      sb.append(" SECONDS (");
74  
75      if (days > 0) {
76        sb.append(days);
77        sb.append(" DAY").append(days == 1 ? "" : "S");
78      }
79  
80      if (hours > 0 ) {
81        sb.append(days > 0 ? " " : "");
82        sb.append(hours);
83        sb.append(" HOUR").append(hours == 1 ? "" : "S");
84      }
85  
86      if (minutes > 0) {
87        sb.append(days + hours > 0 ? " " : "");
88        sb.append(minutes);
89        sb.append(" MINUTE").append(minutes == 1 ? "" : "S");
90      }
91  
92      if (seconds > 0) {
93        sb.append(days + hours + minutes > 0 ? " " : "");
94        sb.append(seconds);
95        sb.append(" SECOND").append(minutes == 1 ? "" : "S");
96      }
97  
98      sb.append(")");
99  
100     return sb.toString();
101   }
102 
103 }