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 029 public static String humanTimeDiff(long timeDiff) { 030 if (timeDiff < 1000) { 031 return String.format("%d msec", 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("%.4f sec", seconds)); 054 } 055 return buf.toString(); 056 } 057 058 public static String humanSize(double size) { 059 if (size >= (1L << 40)) { 060 return String.format("%.1f T", size / (1L << 40)); 061 } 062 063 if (size >= (1L << 30)) { 064 return String.format("%.1f G", size / (1L << 30)); 065 } 066 067 if (size >= (1L << 20)) { 068 return String.format("%.1f M", size / (1L << 20)); 069 } 070 071 if (size >= (1L << 10)) { 072 return String.format("%.1f K", size / (1L << 10)); 073 } 074 075 return String.format("%.0f", size); 076 } 077 078 public static boolean isEmpty(final String input) { 079 return input == null || input.length() == 0; 080 } 081 082 public static String buildString(final String... parts) { 083 StringBuilder sb = new StringBuilder(); 084 for (int i = 0; i < parts.length; ++i) { 085 sb.append(parts[i]); 086 } 087 return sb.toString(); 088 } 089 090 public static StringBuilder appendStrings(final StringBuilder sb, final String... parts) { 091 for (int i = 0; i < parts.length; ++i) { 092 sb.append(parts[i]); 093 } 094 return sb; 095 } 096}