100dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair/* 200dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * Copyright 2008 the original author or authors. 300dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * 400dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * Licensed under the Apache License, Version 2.0 (the "License"); 500dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * you may not use this file except in compliance with the License. 600dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * You may obtain a copy of the License at 700dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * 800dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * http://www.apache.org/licenses/LICENSE-2.0 900dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * 1000dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * Unless required by applicable law or agreed to in writing, software 1100dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * distributed under the License is distributed on an "AS IS" BASIS, 1200dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1300dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * See the License for the specific language governing permissions and 1400dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * limitations under the License. 1500dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair */ 1600dc7bdcf1df9e86789d963984dfc6912a8854c6chrismairpackage org.mockftpserver.core.util; 1700dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair 1800dc7bdcf1df9e86789d963984dfc6912a8854c6chrismairimport java.util.Collection; 1900dc7bdcf1df9e86789d963984dfc6912a8854c6chrismairimport java.util.Iterator; 2000dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair 2100dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair/** 2200dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * Contains static String-related utility methods. 2300dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * 2400dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * @author Chris Mair 2500dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * @version $Revision$ - $Date$ 2600dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair */ 2700dc7bdcf1df9e86789d963984dfc6912a8854c6chrismairpublic class StringUtil { 2800dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair 2900dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair /** 3000dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * Pad the specified String with spaces to the right to the specified width. If the length 3100dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * of string is already equal to or greater than width, then just return string. 3200dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * 3300dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * @param string - the String to pad 3400dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * @param width - the target width 3500dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * @return a String of at least width characters, padded on the right with spaces as necessary 3600dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair */ 3700dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair public static String padRight(String string, int width) { 3800dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair int numSpaces = width - string.length(); 3900dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair return (numSpaces > 0) ? string + spaces(numSpaces) : string; 4000dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair } 4100dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair 4200dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair /** 4300dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * Pad the specified String with spaces to the left to the specified width. If the length 4400dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * of string is already equal to or greater than width, then just return string. 4500dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * 4600dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * @param string - the String to pad 4700dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * @param width - the target width 4800dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * @return a String of at least width characters, padded on the left with spaces as necessary 4900dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair */ 5000dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair public static String padLeft(String string, int width) { 5100dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair int numSpaces = width - string.length(); 5200dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair return (numSpaces > 0) ? spaces(numSpaces) + string : string; 5300dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair } 5400dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair 5500dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair /** 5600dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * Join the Strings within the parts Collection, inserting the delimiter in between elements 5700dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * 5800dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * @param parts - the Collection of Strings to join 5900dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * @param delimiter - the delimiter String to insert between the parts 6000dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * @return the Strings within the parts collection joined together using the specified delimiter 6100dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair */ 6200dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair public static String join(Collection parts, String delimiter) { 6300dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair Assert.notNull(parts, "parts"); 6400dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair Assert.notNull(delimiter, "delimiter"); 6500dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair 6600dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair StringBuffer buf = new StringBuffer(); 6700dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair Iterator iter = parts.iterator(); 6800dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair while (iter.hasNext()) { 6900dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair String component = (String) iter.next(); 7000dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair buf.append(component); 7100dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair if (iter.hasNext()) { 7200dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair buf.append(delimiter); 7300dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair } 7400dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair } 7500dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair return buf.toString(); 7600dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair } 7700dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair 7800dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair //-------------------------------------------------------------------------- 7900dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair // Internal Helper Methods 8000dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair //-------------------------------------------------------------------------- 8100dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair 8200dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair private static String spaces(int numSpaces) { 8300dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair StringBuffer buf = new StringBuffer(); 8400dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair for (int i = 0; i < numSpaces; i++) { 8500dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair buf.append(" "); 8600dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair } 8700dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair return buf.toString(); 8800dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair } 8900dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair 9000dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair /** 9100dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair * Private constructor to prevent instantiation. All members are static. 9200dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair */ 9300dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair private StringUtil() { 9400dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair } 9500dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair 9600dc7bdcf1df9e86789d963984dfc6912a8854c6chrismair}