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("%dmsec", 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("%.4fsec", seconds));
54      }
55      return buf.toString();
56    }
57  
58    public static String humanSize(double size) {
59      if (size >= (1L << 40)) return String.format("%.1fT", size / (1L << 40));
60      if (size >= (1L << 30)) return String.format("%.1fG", size / (1L << 30));
61      if (size >= (1L << 20)) return String.format("%.1fM", size / (1L << 20));
62      if (size >= (1L << 10)) return String.format("%.1fK", size / (1L << 10));
63      return String.format("%.0f", size);
64    }
65  
66    public static boolean isEmpty(final String input) {
67      return input == null || input.length() == 0;
68    }
69  
70    public static String buildString(final String... parts) {
71      StringBuilder sb = new StringBuilder();
72      for (int i = 0; i < parts.length; ++i) {
73        sb.append(parts[i]);
74      }
75      return sb.toString();
76    }
77  
78    public static StringBuilder appendStrings(final StringBuilder sb, final String... parts) {
79      for (int i = 0; i < parts.length; ++i) {
80        sb.append(parts[i]);
81      }
82      return sb;
83    }
84  }