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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 package org
.apache
.hadoop
.hbase
.util
;
21 import org
.apache
.commons
.lang3
.StringUtils
;
22 import org
.apache
.yetus
.audience
.InterfaceAudience
;
25 * Utility for Strings.
27 @InterfaceAudience.Private
28 public final class Strings
{
29 public static final String DEFAULT_SEPARATOR
= "=";
30 public static final String DEFAULT_KEYVALUE_SEPARATOR
= ", ";
36 * Append to a StringBuilder a key/value.
37 * Uses default separators.
38 * @param sb StringBuilder to use
39 * @param key Key to append.
40 * @param value Value to append.
41 * @return Passed <code>sb</code> populated with key/value.
43 public static StringBuilder
appendKeyValue(final StringBuilder sb
,
44 final String key
, final Object value
) {
45 return appendKeyValue(sb
, key
, value
, DEFAULT_SEPARATOR
,
46 DEFAULT_KEYVALUE_SEPARATOR
);
50 * Append to a StringBuilder a key/value.
51 * Uses default separators.
52 * @param sb StringBuilder to use
53 * @param key Key to append.
54 * @param value Value to append.
55 * @param separator Value to use between key and value.
56 * @param keyValueSeparator Value to use between key/value sets.
57 * @return Passed <code>sb</code> populated with key/value.
59 public static StringBuilder
appendKeyValue(final StringBuilder sb
,
60 final String key
, final Object value
, final String separator
,
61 final String keyValueSeparator
) {
62 if (sb
.length() > 0) {
63 sb
.append(keyValueSeparator
);
65 return sb
.append(key
).append(separator
).append(value
);
69 * Given a PTR string generated via reverse DNS lookup, return everything
70 * except the trailing period. Example for host.example.com., return
72 * @param dnPtr a domain name pointer (PTR) string.
73 * @return Sanitized hostname with last period stripped off.
75 public static String
domainNamePointerToHostName(String dnPtr
) {
80 return dnPtr
.endsWith(".") ? dnPtr
.substring(0, dnPtr
.length()-1) : dnPtr
;
84 * Push the input string to the right by appending a character before it, usually a space.
85 * @param input the string to pad
86 * @param padding the character to repeat to the left of the input string
87 * @param length the desired total length including the padding
88 * @return padding characters + input
90 public static String
padFront(String input
, char padding
, int length
) {
91 if (input
.length() > length
) {
92 throw new IllegalArgumentException("input \"" + input
+ "\" longer than maxLength=" + length
);
94 int numPaddingCharacters
= length
- input
.length();
95 return StringUtils
.repeat(padding
, numPaddingCharacters
) + input
;