/*
 * StringUtil.java  $Revision: 1.3 $  $Date: 2007/10/03 04:39:39 $
 *
 * Copyright (C) 2007 Meldcraft Corporation
 *
 * This file is part of Scaffold.
 * 
 * Scaffold is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Scaffold is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with Scaffold.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.meldcraft.util;

/**
 * @version $Revision: 1.3 $ $Date: 2007/10/03 04:39:39 $
 * @author Rob Richards
 */
public class StringUtil
{
// Static variables
    public static final int DEFAULT_FIT_STRING_LENGTH = 40;

// Member variables
// Constructors and initialization
    // prevent instantiation
    private StringUtil()
    {
    }

// Interface implemenations
// Public methods
    /**
     * Returns string truncated to DEFAULT_FIT_STRING_LENGTH.
     */
    public static String fitString(String string)
    {
        return fitString(string, DEFAULT_FIT_STRING_LENGTH);
    }

    /**
     * Returns string truncated to maxLength.  If string
     * is more than maxLength characters, ellipses are inserted into the
     * middle of the string to represent truncated characters.
     */
    public static String fitString(String string, int maxLength)
    {
        String str = string;

        if ((string != null) && (string.length() > maxLength))
        {
            int len = string.length();
            String pad = "...";
            int half = maxLength / 2;
            int idx1 = half - 2;
            int idx2 = len - (maxLength - idx1 - pad.length()) + 1;

            str = string.substring(0, idx1+1) + pad +
                  string.substring(idx2, len);
        }

        return str;
    }

    /** Returns true if str is null or str.trim().equals(""). */
    public static boolean isEmpty(String str)
    {
        return isEmpty(str, true);
    }

    /**
     * Returns true if str is null or str.trim().equals(""), optionally
     * trimming the string before comparing to "".
     */
    public static boolean isEmpty(String str, boolean trimString)
    {
        boolean ret = (str == null);

        if (!ret)
        {
            if (trimString)
            {
                str = str.trim();
            }

            ret = str.equals("");
        }

        return ret;
    }
    
    /**
     * Returns "" if string is null; otherwise, returns string.trim().
     */
    public static String trim(String string)
    {
        String ret = null;

        if (string == null)
        {
            ret = "";
        }
        else
        {
            ret = string.trim();
        }

        return ret;
    }

// Protected methods
// Private methods
// Inner classes
}
