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