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 */
018
019package org.apache.hadoop.hbase.procedure2.util;
020
021import org.apache.yetus.audience.InterfaceAudience;
022import org.apache.yetus.audience.InterfaceStability;
023
024@InterfaceAudience.Private
025@InterfaceStability.Evolving
026public final class StringUtils {
027  private StringUtils() {}
028
029  public static String humanTimeDiff(long timeDiff) {
030    if (timeDiff < 1000) {
031      return String.format("%dmsec", timeDiff);
032    }
033
034    StringBuilder buf = new StringBuilder();
035    long hours = timeDiff / (60*60*1000);
036    long rem = (timeDiff % (60*60*1000));
037    long minutes =  rem / (60*1000);
038    rem = rem % (60*1000);
039    float seconds = rem / 1000.0f;
040
041    if (hours != 0){
042      buf.append(hours);
043      buf.append("hrs, ");
044    }
045    if (minutes != 0){
046      buf.append(minutes);
047      buf.append("mins, ");
048    }
049    if (hours > 0 || minutes > 0) {
050      buf.append(seconds);
051      buf.append("sec");
052    } else {
053      buf.append(String.format("%.4fsec", seconds));
054    }
055    return buf.toString();
056  }
057
058  public static String humanSize(double size) {
059    if (size >= (1L << 40)) return String.format("%.1fT", size / (1L << 40));
060    if (size >= (1L << 30)) return String.format("%.1fG", size / (1L << 30));
061    if (size >= (1L << 20)) return String.format("%.1fM", size / (1L << 20));
062    if (size >= (1L << 10)) return String.format("%.1fK", size / (1L << 10));
063    return String.format("%.0f", size);
064  }
065
066  public static boolean isEmpty(final String input) {
067    return input == null || input.length() == 0;
068  }
069
070  public static String buildString(final String... parts) {
071    StringBuilder sb = new StringBuilder();
072    for (int i = 0; i < parts.length; ++i) {
073      sb.append(parts[i]);
074    }
075    return sb.toString();
076  }
077
078  public static StringBuilder appendStrings(final StringBuilder sb, final String... parts) {
079    for (int i = 0; i < parts.length; ++i) {
080      sb.append(parts[i]);
081    }
082    return sb;
083  }
084}