001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.util;
020
021import org.apache.commons.lang3.StringUtils;
022import org.apache.yetus.audience.InterfaceAudience;
023
024/**
025 * Utility for Strings.
026 */
027@InterfaceAudience.Private
028public class Strings {
029  public final static String DEFAULT_SEPARATOR = "=";
030  public final static String DEFAULT_KEYVALUE_SEPARATOR = ", ";
031
032  /**
033   * Append to a StringBuilder a key/value.
034   * Uses default separators.
035   * @param sb StringBuilder to use
036   * @param key Key to append.
037   * @param value Value to append.
038   * @return Passed <code>sb</code> populated with key/value.
039   */
040  public static StringBuilder appendKeyValue(final StringBuilder sb,
041      final String key, final Object value) {
042    return appendKeyValue(sb, key, value, DEFAULT_SEPARATOR,
043      DEFAULT_KEYVALUE_SEPARATOR);
044  }
045
046  /**
047   * Append to a StringBuilder a key/value.
048   * Uses default separators.
049   * @param sb StringBuilder to use
050   * @param key Key to append.
051   * @param value Value to append.
052   * @param separator Value to use between key and value.
053   * @param keyValueSeparator Value to use between key/value sets.
054   * @return Passed <code>sb</code> populated with key/value.
055   */
056  public static StringBuilder appendKeyValue(final StringBuilder sb,
057      final String key, final Object value, final String separator,
058      final String keyValueSeparator) {
059    if (sb.length() > 0) {
060      sb.append(keyValueSeparator);
061    }
062    return sb.append(key).append(separator).append(value);
063  }
064
065  /**
066   * Given a PTR string generated via reverse DNS lookup, return everything
067   * except the trailing period. Example for host.example.com., return
068   * host.example.com
069   * @param dnPtr a domain name pointer (PTR) string.
070   * @return Sanitized hostname with last period stripped off.
071   *
072   */
073  public static String domainNamePointerToHostName(String dnPtr) {
074    if (dnPtr == null)
075      return null;
076    return dnPtr.endsWith(".") ? dnPtr.substring(0, dnPtr.length()-1) : dnPtr;
077  }
078
079  /**
080   * Push the input string to the right by appending a character before it, usually a space.
081   * @param input the string to pad
082   * @param padding the character to repeat to the left of the input string
083   * @param length the desired total length including the padding
084   * @return padding characters + input
085   */
086  public static String padFront(String input, char padding, int length) {
087    if (input.length() > length) {
088      throw new IllegalArgumentException("input \"" + input + "\" longer than maxLength=" + length);
089    }
090    int numPaddingCharacters = length - input.length();
091    return StringUtils.repeat(padding, numPaddingCharacters) + input;
092  }
093}