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