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.HConstants;
23  import org.apache.hadoop.hbase.classification.InterfaceAudience;
24  import org.apache.hadoop.hbase.classification.InterfaceStability;
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    @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="ICAST_INTEGER_MULTIPLY_CAST_TO_LONG",
48        justification="Will not overflow")
49    private static String humanReadableTTL(final long interval){
50      StringBuilder sb = new StringBuilder();
51      int days, hours, minutes, seconds;
52  
53      // edge cases first
54      if (interval == Integer.MAX_VALUE) {
55        sb.append("FOREVER");
56        return sb.toString();
57      }
58      if (interval < HConstants.MINUTE_IN_SECONDS) {
59        sb.append(interval);
60        sb.append(" SECOND").append(interval == 1 ? "" : "S");
61        return sb.toString();
62      }
63  
64      days  =   (int) (interval / HConstants.DAY_IN_SECONDS);
65      hours =   (int) (interval - HConstants.DAY_IN_SECONDS * days) / HConstants.HOUR_IN_SECONDS;
66      minutes = (int) (interval - HConstants.DAY_IN_SECONDS * days
67          - HConstants.HOUR_IN_SECONDS * hours) / HConstants.MINUTE_IN_SECONDS;
68      seconds = (int) (interval - HConstants.DAY_IN_SECONDS * days
69          - HConstants.HOUR_IN_SECONDS * hours - HConstants.MINUTE_IN_SECONDS * minutes);
70  
71      sb.append(interval);
72      sb.append(" SECONDS (");
73  
74      if (days > 0) {
75        sb.append(days);
76        sb.append(" DAY").append(days == 1 ? "" : "S");
77      }
78  
79      if (hours > 0 ) {
80        sb.append(days > 0 ? " " : "");
81        sb.append(hours);
82        sb.append(" HOUR").append(hours == 1 ? "" : "S");
83      }
84  
85      if (minutes > 0) {
86        sb.append(days + hours > 0 ? " " : "");
87        sb.append(minutes);
88        sb.append(" MINUTE").append(minutes == 1 ? "" : "S");
89      }
90  
91      if (seconds > 0) {
92        sb.append(days + hours + minutes > 0 ? " " : "");
93        sb.append(seconds);
94        sb.append(" SECOND").append(minutes == 1 ? "" : "S");
95      }
96  
97      sb.append(")");
98  
99      return sb.toString();
100   }
101 
102 }