1 /** 2 * Copyright 2010 The Apache Software Foundation 3 * 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 package org.apache.hadoop.hbase.util; 21 22 /** 23 * Utility for Strings. 24 */ 25 public class Strings { 26 public final static String DEFAULT_SEPARATOR = "="; 27 public final static String DEFAULT_KEYVALUE_SEPARATOR = ", "; 28 29 /** 30 * Append to a StringBuilder a key/value. 31 * Uses default separators. 32 * @param sb StringBuilder to use 33 * @param key Key to append. 34 * @param value Value to append. 35 * @return Passed <code>sb</code> populated with key/value. 36 */ 37 public static StringBuilder appendKeyValue(final StringBuilder sb, 38 final String key, final Object value) { 39 return appendKeyValue(sb, key, value, DEFAULT_SEPARATOR, 40 DEFAULT_KEYVALUE_SEPARATOR); 41 } 42 43 /** 44 * Append to a StringBuilder a key/value. 45 * Uses default separators. 46 * @param sb StringBuilder to use 47 * @param key Key to append. 48 * @param value Value to append. 49 * @param separator Value to use between key and value. 50 * @param keyValueSeparator Value to use between key/value sets. 51 * @return Passed <code>sb</code> populated with key/value. 52 */ 53 public static StringBuilder appendKeyValue(final StringBuilder sb, 54 final String key, final Object value, final String separator, 55 final String keyValueSeparator) { 56 if (sb.length() > 0) { 57 sb.append(keyValueSeparator); 58 } 59 return sb.append(key).append(separator).append(value); 60 } 61 62 /** 63 * Given a PTR string generated via reverse DNS lookup, return everything 64 * except the trailing period. Example for host.example.com., return 65 * host.example.com 66 * @param dnPtr a domain name pointer (PTR) string. 67 * @return Sanitized hostname with last period stripped off. 68 * 69 */ 70 public static String domainNamePointerToHostName(String dnPtr) { 71 if (dnPtr == null) 72 return null; 73 return dnPtr.endsWith(".") ? dnPtr.substring(0, dnPtr.length()-1) : dnPtr; 74 } 75 }