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
034  public static final Joiner JOINER = Joiner.on(",");
035  public static final Splitter SPLITTER = Splitter.on(",");
036
037  private Strings() {
038  }
039
040  /**
041   * Append to a StringBuilder a key/value. Uses default separators.
042   * @param sb    StringBuilder to use
043   * @param key   Key to append.
044   * @param value Value to append.
045   * @return Passed <code>sb</code> populated with key/value.
046   */
047  public static StringBuilder appendKeyValue(final StringBuilder sb, final String key,
048    final Object value) {
049    return appendKeyValue(sb, key, value, DEFAULT_SEPARATOR, DEFAULT_KEYVALUE_SEPARATOR);
050  }
051
052  /**
053   * Append to a StringBuilder a key/value. Uses default separators.
054   * @param sb                StringBuilder to use
055   * @param key               Key to append.
056   * @param value             Value to append.
057   * @param separator         Value to use between key and value.
058   * @param keyValueSeparator Value to use between key/value sets.
059   * @return Passed <code>sb</code> populated with key/value.
060   */
061  public static StringBuilder appendKeyValue(final StringBuilder sb, final String key,
062    final Object value, final String separator, final String keyValueSeparator) {
063    if (sb.length() > 0) {
064      sb.append(keyValueSeparator);
065    }
066    return sb.append(key).append(separator).append(value);
067  }
068
069  /**
070   * Given a PTR string generated via reverse DNS lookup, return everything except the trailing
071   * period. Example for host.example.com., return 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}