View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.util;
20  
21  import org.apache.hadoop.hbase.classification.InterfaceAudience;
22  
23  /**
24   * Utility for Strings.
25   */
26  @InterfaceAudience.Private
27  public final class Strings {
28    public static final String DEFAULT_SEPARATOR = "=";
29    public static final String DEFAULT_KEYVALUE_SEPARATOR = ", ";
30  
31    private Strings() {
32    }
33  
34    /**
35     * Append to a StringBuilder a key/value.
36     * Uses default separators.
37     * @param sb StringBuilder to use
38     * @param key Key to append.
39     * @param value Value to append.
40     * @return Passed <code>sb</code> populated with key/value.
41     */
42    public static StringBuilder appendKeyValue(final StringBuilder sb,
43        final String key, final Object value) {
44      return appendKeyValue(sb, key, value, DEFAULT_SEPARATOR,
45        DEFAULT_KEYVALUE_SEPARATOR);
46    }
47  
48    /**
49     * Append to a StringBuilder a key/value.
50     * Uses default separators.
51     * @param sb StringBuilder to use
52     * @param key Key to append.
53     * @param value Value to append.
54     * @param separator Value to use between key and value.
55     * @param keyValueSeparator Value to use between key/value sets.
56     * @return Passed <code>sb</code> populated with key/value.
57     */
58    public static StringBuilder appendKeyValue(final StringBuilder sb,
59        final String key, final Object value, final String separator,
60        final String keyValueSeparator) {
61      if (sb.length() > 0) {
62        sb.append(keyValueSeparator);
63      }
64      return sb.append(key).append(separator).append(value);
65    }
66  
67    /**
68     * Given a PTR string generated via reverse DNS lookup, return everything
69     * except the trailing period. Example for host.example.com., return
70     * host.example.com
71     * @param dnPtr a domain name pointer (PTR) string.
72     * @return Sanitized hostname with last period stripped off.
73     */
74    public static String domainNamePointerToHostName(String dnPtr) {
75      if (dnPtr == null) {
76        return null;
77      }
78  
79      return dnPtr.endsWith(".") ? dnPtr.substring(0, dnPtr.length()-1) : dnPtr;
80    }
81  
82    /**
83     * Null-safe length check.
84     * @param input the {@link String} to do the null-safe length check for
85     * @return true if null or length==0
86     */
87    public static boolean isEmpty(String input) {
88      return input == null || input.length() == 0;
89    }
90  
91    /**
92     * Push the input string to the right by appending a character before it, usually a space.
93     * @param input the string to pad
94     * @param padding the character to repeat to the left of the input string
95     * @param length the desired total length including the padding
96     * @return padding characters + input
97     */
98    public static String padFront(String input, char padding, int length) {
99      if (input.length() > length) {
100       throw new IllegalArgumentException("input \"" + input + "\" longer than maxLength=" + length);
101     }
102     int numPaddingCharacters = length - input.length();
103     return repeat(padding, numPaddingCharacters) + input;
104   }
105 
106   /**
107    * @param c repeat this character
108    * @param reapeatFor the length of the output String
109    * @return c, repeated repeatFor times
110    */
111   public static String repeat(char c, int reapeatFor) {
112     StringBuilder sb = new StringBuilder();
113     for (int i = 0; i < reapeatFor; ++i) {
114       sb.append(c);
115     }
116     return sb.toString();
117   }
118 }