1 /** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 */ 19 package org.apache.hadoop.hbase.util; 20 21 import java.util.Iterator; 22 23 import org.apache.hadoop.hbase.classification.InterfaceAudience; 24 25 /** 26 * Utility for Strings. 27 */ 28 @InterfaceAudience.Private 29 public class Strings { 30 public final static String DEFAULT_SEPARATOR = "="; 31 public final static String DEFAULT_KEYVALUE_SEPARATOR = ", "; 32 33 /** 34 * Append to a StringBuilder a key/value. 35 * Uses default separators. 36 * @param sb StringBuilder to use 37 * @param key Key to append. 38 * @param value Value to append. 39 * @return Passed <code>sb</code> populated with key/value. 40 */ 41 public static StringBuilder appendKeyValue(final StringBuilder sb, 42 final String key, final Object value) { 43 return appendKeyValue(sb, key, value, DEFAULT_SEPARATOR, 44 DEFAULT_KEYVALUE_SEPARATOR); 45 } 46 47 /** 48 * Append to a StringBuilder a key/value. 49 * Uses default separators. 50 * @param sb StringBuilder to use 51 * @param key Key to append. 52 * @param value Value to append. 53 * @param separator Value to use between key and value. 54 * @param keyValueSeparator Value to use between key/value sets. 55 * @return Passed <code>sb</code> populated with key/value. 56 */ 57 public static StringBuilder appendKeyValue(final StringBuilder sb, 58 final String key, final Object value, final String separator, 59 final String keyValueSeparator) { 60 if (sb.length() > 0) { 61 sb.append(keyValueSeparator); 62 } 63 return sb.append(key).append(separator).append(value); 64 } 65 66 /** 67 * Given a PTR string generated via reverse DNS lookup, return everything 68 * except the trailing period. Example for host.example.com., return 69 * host.example.com 70 * @param dnPtr a domain name pointer (PTR) string. 71 * @return Sanitized hostname with last period stripped off. 72 * 73 */ 74 public static String domainNamePointerToHostName(String dnPtr) { 75 if (dnPtr == null) 76 return null; 77 return dnPtr.endsWith(".") ? dnPtr.substring(0, dnPtr.length()-1) : dnPtr; 78 } 79 80 /** 81 * Null-safe length check. 82 * @param input 83 * @return true if null or length==0 84 */ 85 public static boolean isEmpty(String input) { 86 return input == null || input.length() == 0; 87 } 88 89 /** 90 * Push the input string to the right by appending a character before it, usually a space. 91 * @param input the string to pad 92 * @param padding the character to repeat to the left of the input string 93 * @param length the desired total length including the padding 94 * @return padding characters + input 95 */ 96 public static String padFront(String input, char padding, int length) { 97 if (input.length() > length) { 98 throw new IllegalArgumentException("input \"" + input + "\" longer than maxLength=" + length); 99 } 100 int numPaddingCharacters = length - input.length(); 101 return repeat(padding, numPaddingCharacters) + input; 102 } 103 104 /** 105 * @param c repeat this character 106 * @param reapeatFor the length of the output String 107 * @return c, repeated repeatFor times 108 */ 109 public static String repeat(char c, int reapeatFor) { 110 StringBuilder sb = new StringBuilder(); 111 for (int i = 0; i < reapeatFor; ++i) { 112 sb.append(c); 113 } 114 return sb.toString(); 115 } 116 117 /** 118 * Concatenates strings, using a separator. 119 * 120 * @param separator Separator to join with. 121 * @param strings Strings to join. 122 */ 123 public static String join(CharSequence separator, Iterable<?> strings) { 124 Iterator<?> i = strings.iterator(); 125 if (!i.hasNext()) { 126 return ""; 127 } 128 StringBuilder sb = new StringBuilder(i.next().toString()); 129 while (i.hasNext()) { 130 sb.append(separator); 131 sb.append(i.next().toString()); 132 } 133 return sb.toString(); 134 } 135 }