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  
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.valueOf(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      // edge cases first
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 }