/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.util;

import java.util.Arrays;
import java.util.Iterator;
import java.util.regex.Pattern;

import org.bodington.util.html.Utils;

/**
 * Class that contains utility methods for handling text.
 * @author Alexis O'Connor
 */
public final class TextUtils
{
    // Utility class, therefore no instances.
    private TextUtils()
    {
    }
    
    private static Pattern vowels = Pattern.compile( "[aeiou]",
        Pattern.CASE_INSENSITIVE );

    /**
     * Indicates whether or not a string begins a vowel. The check is 
     * case-insensitive. This method does not trim leading whitespace, so the
     * check is literally on the first letter of the string.
     * @param s the string that is to be matched.
     * @return <code>true</code> if the string begins with a vowel, otherwise
     * <code>false</code>.
     */
    public static boolean beginsWithVowel( String s )
    {
        return vowels.matcher( s.subSequence( 0, 1 ) ).matches();
    }
    
    /**
     * Performs a substring but doesn't go outside the limits of the string. If 
     * the end index is greater than the length of the string the end is used.
     * @param s The source string to extract the substring from.
     * @param begin The begin index.
     * @param end The end index.
     * @return The substring.
     */
    public static String boundSubstring(String s, int begin, int end)
    {
        int length = s.length();
        return s.substring(Utils.bound(begin,0,length),Utils.bound(end,0,length));
    }
    
    /**
     * @see #join(Iterator, String)
     */
    public static String join(Object objects[], String seperator)
    {
        return join(Arrays.asList(objects).iterator(), seperator);
    }

    /**
     * Utility method to join together the String versions of all the objects
     * seperated by the specified string.
     * @param it The iterator to join together.
     * @param seperator The seperator to place between elements.
     * @return The joined together string.
     */
    public static String join(Iterator it, String seperator)
    {
        if ( it.hasNext() )
        {
            StringBuffer problems = new StringBuffer(it.next().toString());
            while( it.hasNext() )
            {
                problems.append(seperator);
                problems.append(it.next().toString());
            }
            return problems.toString();
        }
        return "";
    }
    
    
}
