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 */
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: 51 $ - $Date: 2008-05-09 22:16:32 -0400 (Fri, 09 May 2008) $
26 */
27public class StringUtil {
28
29    public static String padRight(String string, int width) {
30        int numSpaces = width - string.length();
31        return (numSpaces > 0) ? string + spaces(numSpaces) : string;
32    }
33
34    public static String padLeft(String string, int width) {
35        int numSpaces = width - string.length();
36        return (numSpaces > 0) ? spaces(numSpaces) + string : string;
37    }
38
39    public static String join(Collection parts, String delimiter) {
40        Assert.notNull(parts, "parts");
41        Assert.notNull(delimiter, "delimiter");
42
43        StringBuffer buf = new StringBuffer();
44        Iterator iter = parts.iterator();
45        while (iter.hasNext()) {
46            String component = (String) iter.next();
47            buf.append(component);
48            if (iter.hasNext()) {
49                buf.append(delimiter);
50            }
51        }
52        return buf.toString();
53    }
54
55    //--------------------------------------------------------------------------
56    // Internal Helper Methods
57    //--------------------------------------------------------------------------
58
59    private static String spaces(int numSpaces) {
60        StringBuffer buf = new StringBuffer();
61        for (int i = 0; i < numSpaces; i++) {
62            buf.append(" ");
63        }
64        return buf.toString();
65    }
66
67    /**
68     * Private constructor to prevent instantiation. All members are static.
69     */
70    private StringUtil() {
71    }
72
73}