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