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      LONG,
31      BOOLEAN,
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.parseLong(value)));
40          break;
41        case LONG:
42          byte[] longBytes = Bytes.toBytesBinary(value);
43          human.append(String.valueOf(Bytes.toLong(longBytes)));
44          break;
45        case BOOLEAN:
46          byte[] booleanBytes = Bytes.toBytesBinary(value);
47          human.append(String.valueOf(Bytes.toBoolean(booleanBytes)));
48          break;
49        default:
50          human.append(value);
51      }
52      return human.toString();
53    }
54  
55    @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="ICAST_INTEGER_MULTIPLY_CAST_TO_LONG",
56        justification="Will not overflow")
57    private static String humanReadableTTL(final long interval){
58      StringBuilder sb = new StringBuilder();
59      int days, hours, minutes, seconds;
60  
61      // edge cases first
62      if (interval == Integer.MAX_VALUE) {
63        sb.append("FOREVER");
64        return sb.toString();
65      }
66      if (interval < HConstants.MINUTE_IN_SECONDS) {
67        sb.append(interval);
68        sb.append(" SECOND").append(interval == 1 ? "" : "S");
69        return sb.toString();
70      }
71  
72      days  =   (int) (interval / HConstants.DAY_IN_SECONDS);
73      hours =   (int) (interval - HConstants.DAY_IN_SECONDS * days) / HConstants.HOUR_IN_SECONDS;
74      minutes = (int) (interval - HConstants.DAY_IN_SECONDS * days
75          - HConstants.HOUR_IN_SECONDS * hours) / HConstants.MINUTE_IN_SECONDS;
76      seconds = (int) (interval - HConstants.DAY_IN_SECONDS * days
77          - HConstants.HOUR_IN_SECONDS * hours - HConstants.MINUTE_IN_SECONDS * minutes);
78  
79      sb.append(interval);
80      sb.append(" SECONDS (");
81  
82      if (days > 0) {
83        sb.append(days);
84        sb.append(" DAY").append(days == 1 ? "" : "S");
85      }
86  
87      if (hours > 0 ) {
88        sb.append(days > 0 ? " " : "");
89        sb.append(hours);
90        sb.append(" HOUR").append(hours == 1 ? "" : "S");
91      }
92  
93      if (minutes > 0) {
94        sb.append(days + hours > 0 ? " " : "");
95        sb.append(minutes);
96        sb.append(" MINUTE").append(minutes == 1 ? "" : "S");
97      }
98  
99      if (seconds > 0) {
100       sb.append(days + hours + minutes > 0 ? " " : "");
101       sb.append(seconds);
102       sb.append(" SECOND").append(minutes == 1 ? "" : "S");
103     }
104 
105     sb.append(")");
106 
107     return sb.toString();
108   }
109 
110 }