001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.procedure2.util;
019
020import org.apache.yetus.audience.InterfaceAudience;
021import org.apache.yetus.audience.InterfaceStability;
022
023@InterfaceAudience.Private
024@InterfaceStability.Evolving
025public final class StringUtils {
026  private StringUtils() {}
027
028  public static String humanTimeDiff(long timeDiff) {
029    if (timeDiff < 1000) {
030      return String.format("%d msec", timeDiff);
031    }
032
033    StringBuilder buf = new StringBuilder();
034    long hours = timeDiff / (60*60*1000);
035    long rem = (timeDiff % (60*60*1000));
036    long minutes =  rem / (60*1000);
037    rem = rem % (60*1000);
038    float seconds = rem / 1000.0f;
039
040    if (hours != 0){
041      buf.append(hours);
042      buf.append(" hrs, ");
043    }
044    if (minutes != 0){
045      buf.append(minutes);
046      buf.append(" mins, ");
047    }
048    if (hours > 0 || minutes > 0) {
049      buf.append(seconds);
050      buf.append(" sec");
051    } else {
052      buf.append(String.format("%.4f sec", seconds));
053    }
054    return buf.toString();
055  }
056
057  public static String humanSize(double size) {
058    if (size >= (1L << 40)) {
059      return String.format("%.1f T", size / (1L << 40));
060    }
061
062    if (size >= (1L << 30)) {
063      return String.format("%.1f G", size / (1L << 30));
064    }
065
066    if (size >= (1L << 20)) {
067      return String.format("%.1f M", size / (1L << 20));
068    }
069
070    if (size >= (1L << 10)) {
071      return String.format("%.1f K", size / (1L << 10));
072    }
073
074    return String.format("%.0f", size);
075  }
076
077  public static boolean isEmpty(final String input) {
078    return input == null || input.length() == 0;
079  }
080
081  public static String buildString(final String... parts) {
082    StringBuilder sb = new StringBuilder();
083    for (int i = 0; i < parts.length; ++i) {
084      sb.append(parts[i]);
085    }
086    return sb.toString();
087  }
088
089  public static StringBuilder appendStrings(final StringBuilder sb, final String... parts) {
090    for (int i = 0; i < parts.length; ++i) {
091      sb.append(parts[i]);
092    }
093    return sb;
094  }
095}