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.util; 019 020import org.apache.commons.lang3.StringUtils; 021import org.apache.yetus.audience.InterfaceAudience; 022 023/** 024 * Utility for Strings. 025 */ 026@InterfaceAudience.Private 027public final class Strings { 028 public static final String DEFAULT_SEPARATOR = "="; 029 public static final String DEFAULT_KEYVALUE_SEPARATOR = ", "; 030 031 private Strings() { 032 } 033 034 /** 035 * Append to a StringBuilder a key/value. Uses default separators. 036 * @param sb StringBuilder to use 037 * @param key Key to append. 038 * @param value Value to append. 039 * @return Passed <code>sb</code> populated with key/value. 040 */ 041 public static StringBuilder appendKeyValue(final StringBuilder sb, final String key, 042 final Object value) { 043 return appendKeyValue(sb, key, value, DEFAULT_SEPARATOR, DEFAULT_KEYVALUE_SEPARATOR); 044 } 045 046 /** 047 * Append to a StringBuilder a key/value. Uses default separators. 048 * @param sb StringBuilder to use 049 * @param key Key to append. 050 * @param value Value to append. 051 * @param separator Value to use between key and value. 052 * @param keyValueSeparator Value to use between key/value sets. 053 * @return Passed <code>sb</code> populated with key/value. 054 */ 055 public static StringBuilder appendKeyValue(final StringBuilder sb, final String key, 056 final Object value, final String separator, final String keyValueSeparator) { 057 if (sb.length() > 0) { 058 sb.append(keyValueSeparator); 059 } 060 return sb.append(key).append(separator).append(value); 061 } 062 063 /** 064 * Given a PTR string generated via reverse DNS lookup, return everything except the trailing 065 * period. Example for host.example.com., return host.example.com 066 * @param dnPtr a domain name pointer (PTR) string. 067 * @return Sanitized hostname with last period stripped off. 068 */ 069 public static String domainNamePointerToHostName(String dnPtr) { 070 if (dnPtr == null) { 071 return null; 072 } 073 074 return dnPtr.endsWith(".") ? dnPtr.substring(0, dnPtr.length() - 1) : dnPtr; 075 } 076 077 /** 078 * Push the input string to the right by appending a character before it, usually a space. 079 * @param input the string to pad 080 * @param padding the character to repeat to the left of the input string 081 * @param length the desired total length including the padding 082 * @return padding characters + input 083 */ 084 public static String padFront(String input, char padding, int length) { 085 if (input.length() > length) { 086 throw new IllegalArgumentException("input \"" + input + "\" longer than maxLength=" + length); 087 } 088 int numPaddingCharacters = length - input.length(); 089 return StringUtils.repeat(padding, numPaddingCharacters) + input; 090 } 091}