| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| StringUtil |
|
| 2.0;2 |
| 1 | /* | |
| 2 | * Copyright 2008 the original author or authors. | |
| 3 | * | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | * you may not use this file except in compliance with the License. | |
| 6 | * You may obtain a copy of the License at | |
| 7 | * | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | * | |
| 10 | * Unless required by applicable law or agreed to in writing, software | |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | * See the License for the specific language governing permissions and | |
| 14 | * limitations under the License. | |
| 15 | */ | |
| 16 | package org.mockftpserver.core.util; | |
| 17 | ||
| 18 | import java.util.Collection; | |
| 19 | import java.util.Iterator; | |
| 20 | ||
| 21 | /** | |
| 22 | * Contains static String-related utility methods. | |
| 23 | * | |
| 24 | * @author Chris Mair | |
| 25 | * @version $Revision: 198 $ - $Date: 2008-12-10 21:47:32 -0500 (Wed, 10 Dec 2008) $ | |
| 26 | */ | |
| 27 | public class StringUtil { | |
| 28 | ||
| 29 | /** | |
| 30 | * Pad the specified String with spaces to the right to the specified width. If the length | |
| 31 | * of string is already equal to or greater than width, then just return string. | |
| 32 | * | |
| 33 | * @param string - the String to pad | |
| 34 | * @param width - the target width | |
| 35 | * @return a String of at least width characters, padded on the right with spaces as necessary | |
| 36 | */ | |
| 37 | public static String padRight(String string, int width) { | |
| 38 | 36 | int numSpaces = width - string.length(); |
| 39 | 36 | return (numSpaces > 0) ? string + spaces(numSpaces) : string; |
| 40 | } | |
| 41 | ||
| 42 | /** | |
| 43 | * Pad the specified String with spaces to the left to the specified width. If the length | |
| 44 | * of string is already equal to or greater than width, then just return string. | |
| 45 | * | |
| 46 | * @param string - the String to pad | |
| 47 | * @param width - the target width | |
| 48 | * @return a String of at least width characters, padded on the left with spaces as necessary | |
| 49 | */ | |
| 50 | public static String padLeft(String string, int width) { | |
| 51 | 21 | int numSpaces = width - string.length(); |
| 52 | 21 | return (numSpaces > 0) ? spaces(numSpaces) + string : string; |
| 53 | } | |
| 54 | ||
| 55 | /** | |
| 56 | * Join the Strings within the parts Collection, inserting the delimiter in between elements | |
| 57 | * | |
| 58 | * @param parts - the Collection of Strings to join | |
| 59 | * @param delimiter - the delimiter String to insert between the parts | |
| 60 | * @return the Strings within the parts collection joined together using the specified delimiter | |
| 61 | */ | |
| 62 | public static String join(Collection parts, String delimiter) { | |
| 63 | 2687 | Assert.notNull(parts, "parts"); |
| 64 | 2686 | Assert.notNull(delimiter, "delimiter"); |
| 65 | ||
| 66 | 2685 | StringBuffer buf = new StringBuffer(); |
| 67 | 2685 | Iterator iter = parts.iterator(); |
| 68 | 8911 | while (iter.hasNext()) { |
| 69 | 6226 | String component = (String) iter.next(); |
| 70 | 6226 | buf.append(component); |
| 71 | 6226 | if (iter.hasNext()) { |
| 72 | 3553 | buf.append(delimiter); |
| 73 | } | |
| 74 | 6226 | } |
| 75 | 2685 | return buf.toString(); |
| 76 | } | |
| 77 | ||
| 78 | //-------------------------------------------------------------------------- | |
| 79 | // Internal Helper Methods | |
| 80 | //-------------------------------------------------------------------------- | |
| 81 | ||
| 82 | private static String spaces(int numSpaces) { | |
| 83 | 36 | StringBuffer buf = new StringBuffer(); |
| 84 | 311 | for (int i = 0; i < numSpaces; i++) { |
| 85 | 275 | buf.append(" "); |
| 86 | } | |
| 87 | 36 | return buf.toString(); |
| 88 | } | |
| 89 | ||
| 90 | /** | |
| 91 | * Private constructor to prevent instantiation. All members are static. | |
| 92 | */ | |
| 93 | 0 | private StringUtil() { |
| 94 | 0 | } |
| 95 | ||
| 96 | } |