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 com.google.common.base.Joiner;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.List;
26  import java.util.Objects;
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.hbase.HConstants;
29  
30  @InterfaceAudience.Private
31  public class PrettyPrinter {
32  
33    public enum Unit {
34      TIME_INTERVAL,
35      NONE
36    }
37  
38    public static String format(final String value, final Unit unit) {
39      StringBuilder human = new StringBuilder();
40      switch (unit) {
41        case TIME_INTERVAL:
42          human.append(humanReadableTTL(Long.parseLong(value)));
43          break;
44        default:
45          human.append(value);
46      }
47      return human.toString();
48    }
49  
50    @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="ICAST_INTEGER_MULTIPLY_CAST_TO_LONG",
51        justification="Will not overflow")
52    private static String humanReadableTTL(final long interval){
53      StringBuilder sb = new StringBuilder();
54      int days, hours, minutes, seconds;
55  
56      // edge cases first
57      if (interval == Integer.MAX_VALUE) {
58        sb.append("FOREVER");
59        return sb.toString();
60      }
61      if (interval < HConstants.MINUTE_IN_SECONDS) {
62        sb.append(interval);
63        sb.append(" SECOND").append(interval == 1 ? "" : "S");
64        return sb.toString();
65      }
66  
67      days  =   (int) (interval / HConstants.DAY_IN_SECONDS);
68      hours =   (int) (interval - HConstants.DAY_IN_SECONDS * days) / HConstants.HOUR_IN_SECONDS;
69      minutes = (int) (interval - HConstants.DAY_IN_SECONDS * days
70          - HConstants.HOUR_IN_SECONDS * hours) / HConstants.MINUTE_IN_SECONDS;
71      seconds = (int) (interval - HConstants.DAY_IN_SECONDS * days
72          - HConstants.HOUR_IN_SECONDS * hours - HConstants.MINUTE_IN_SECONDS * minutes);
73  
74      sb.append(interval);
75      sb.append(" SECONDS (");
76  
77      if (days > 0) {
78        sb.append(days);
79        sb.append(" DAY").append(days == 1 ? "" : "S");
80      }
81  
82      if (hours > 0 ) {
83        sb.append(days > 0 ? " " : "");
84        sb.append(hours);
85        sb.append(" HOUR").append(hours == 1 ? "" : "S");
86      }
87  
88      if (minutes > 0) {
89        sb.append(days + hours > 0 ? " " : "");
90        sb.append(minutes);
91        sb.append(" MINUTE").append(minutes == 1 ? "" : "S");
92      }
93  
94      if (seconds > 0) {
95        sb.append(days + hours + minutes > 0 ? " " : "");
96        sb.append(seconds);
97        sb.append(" SECOND").append(minutes == 1 ? "" : "S");
98      }
99  
100     sb.append(")");
101 
102     return sb.toString();
103   }
104 
105   /**
106    * Pretty prints a collection of any type to a string. Relies on toString() implementation of the
107    * object type.
108    * @param collection collection to pretty print.
109    * @return Pretty printed string for the collection.
110    */
111   public static String toString(Collection<?> collection) {
112     List<String> stringList = new ArrayList<>();
113     for (Object o: collection) {
114       stringList.add(Objects.toString(o));
115     }
116     return "[" + Joiner.on(',').join(stringList) + "]";
117   }
118 
119 }