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 class Strings {
28 public final static String DEFAULT_SEPARATOR = "=";
29 public final static String DEFAULT_KEYVALUE_SEPARATOR = ", ";
30
31 /**
32 * Append to a StringBuilder a key/value.
33 * Uses default separators.
34 * @param sb StringBuilder to use
35 * @param key Key to append.
36 * @param value Value to append.
37 * @return Passed <code>sb</code> populated with key/value.
38 */
39 public static StringBuilder appendKeyValue(final StringBuilder sb,
40 final String key, final Object value) {
41 return appendKeyValue(sb, key, value, DEFAULT_SEPARATOR,
42 DEFAULT_KEYVALUE_SEPARATOR);
43 }
44
45 /**
46 * Append to a StringBuilder a key/value.
47 * Uses default separators.
48 * @param sb StringBuilder to use
49 * @param key Key to append.
50 * @param value Value to append.
51 * @param separator Value to use between key and value.
52 * @param keyValueSeparator Value to use between key/value sets.
53 * @return Passed <code>sb</code> populated with key/value.
54 */
55 public static StringBuilder appendKeyValue(final StringBuilder sb,
56 final String key, final Object value, final String separator,
57 final String keyValueSeparator) {
58 if (sb.length() > 0) {
59 sb.append(keyValueSeparator);
60 }
61 return sb.append(key).append(separator).append(value);
62 }
63
64 /**
65 * Given a PTR string generated via reverse DNS lookup, return everything
66 * except the trailing period. Example for host.example.com., return
67 * host.example.com
68 * @param dnPtr a domain name pointer (PTR) string.
69 * @return Sanitized hostname with last period stripped off.
70 *
71 */
72 public static String domainNamePointerToHostName(String dnPtr) {
73 if (dnPtr == null)
74 return null;
75 return dnPtr.endsWith(".") ? dnPtr.substring(0, dnPtr.length()-1) : dnPtr;
76 }
77
78 /**
79 * Null-safe length check.
80 * @param input
81 * @return true if null or length==0
82 */
83 public static boolean isEmpty(String input) {
84 return input == null || input.length() == 0;
85 }
86
87 /**
88 * Push the input string to the right by appending a character before it, usually a space.
89 * @param input the string to pad
90 * @param padding the character to repeat to the left of the input string
91 * @param length the desired total length including the padding
92 * @return padding characters + input
93 */
94 public static String padFront(String input, char padding, int length) {
95 if (input.length() > length) {
96 throw new IllegalArgumentException("input \"" + input + "\" longer than maxLength=" + length);
97 }
98 int numPaddingCharacters = length - input.length();
99 return repeat(padding, numPaddingCharacters) + input;
100 }
101
102 /**
103 * @param c repeat this character
104 * @param reapeatFor the length of the output String
105 * @return c, repeated repeatFor times
106 */
107 public static String repeat(char c, int reapeatFor) {
108 StringBuilder sb = new StringBuilder();
109 for (int i = 0; i < reapeatFor; ++i) {
110 sb.append(c);
111 }
112 return sb.toString();
113 }
114 }