View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.procedure2.util;
20  
21  import org.apache.hadoop.hbase.classification.InterfaceAudience;
22  import org.apache.hadoop.hbase.classification.InterfaceStability;
23  
24  @InterfaceAudience.Private
25  @InterfaceStability.Evolving
26  public final class StringUtils {
27    private StringUtils() {}
28  
29    public static String humanTimeDiff(long timeDiff) {
30      if (timeDiff < 1000) {
31        return String.format("%d msec", timeDiff);
32      }
33  
34      StringBuilder buf = new StringBuilder();
35      long hours = timeDiff / (60*60*1000);
36      long rem = (timeDiff % (60*60*1000));
37      long minutes =  rem / (60*1000);
38      rem = rem % (60*1000);
39      float seconds = rem / 1000.0f;
40  
41      if (hours != 0){
42        buf.append(hours);
43        buf.append(" hrs, ");
44      }
45      if (minutes != 0){
46        buf.append(minutes);
47        buf.append(" mins, ");
48      }
49      if (hours > 0 || minutes > 0) {
50        buf.append(seconds);
51        buf.append(" sec");
52      } else {
53        buf.append(String.format("%.4f sec", seconds));
54      }
55      return buf.toString();
56    }
57  
58    public static String humanSize(double size) {
59      if (size >= (1L << 40)) {
60        return String.format("%.1f T", size / (1L << 40));
61      }
62  
63      if (size >= (1L << 30)) {
64        return String.format("%.1f G", size / (1L << 30));
65      }
66  
67      if (size >= (1L << 20)) {
68        return String.format("%.1f M", size / (1L << 20));
69      }
70  
71      if (size >= (1L << 10)) {
72        return String.format("%.1f K", size / (1L << 10));
73      }
74  
75      return String.format("%.0f", size);
76    }
77  
78    public static boolean isEmpty(final String input) {
79      return input == null || input.length() == 0;
80    }
81  
82    public static String buildString(final String... parts) {
83      StringBuilder sb = new StringBuilder();
84      for (int i = 0; i < parts.length; ++i) {
85        sb.append(parts[i]);
86      }
87      return sb.toString();
88    }
89  
90    public static StringBuilder appendStrings(final StringBuilder sb, final String... parts) {
91      for (int i = 0; i < parts.length; ++i) {
92        sb.append(parts[i]);
93      }
94      return sb;
95    }
96  }