blob: 090a53c954f9f4c756bd17dd302eae72c6393f37 [file] [log] [blame]
chrismair00dc7bd2014-05-11 21:21:28 +00001/*
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 */
16package org.mockftpserver.core.util;
17
18import java.util.Collection;
19import java.util.Iterator;
20
21/**
22 * Contains static String-related utility methods.
23 *
24 * @author Chris Mair
25 * @version $Revision$ - $Date$
26 */
27public 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 int numSpaces = width - string.length();
39 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 int numSpaces = width - string.length();
52 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 Assert.notNull(parts, "parts");
64 Assert.notNull(delimiter, "delimiter");
65
66 StringBuffer buf = new StringBuffer();
67 Iterator iter = parts.iterator();
68 while (iter.hasNext()) {
69 String component = (String) iter.next();
70 buf.append(component);
71 if (iter.hasNext()) {
72 buf.append(delimiter);
73 }
74 }
75 return buf.toString();
76 }
77
78 //--------------------------------------------------------------------------
79 // Internal Helper Methods
80 //--------------------------------------------------------------------------
81
82 private static String spaces(int numSpaces) {
83 StringBuffer buf = new StringBuffer();
84 for (int i = 0; i < numSpaces; i++) {
85 buf.append(" ");
86 }
87 return buf.toString();
88 }
89
90 /**
91 * Private constructor to prevent instantiation. All members are static.
92 */
93 private StringUtil() {
94 }
95
96}