class Util {

    public static String justify(String str, String value, Map<String, ConversionOptionBO> options,
                                 int length) {

        if (value == null) {
            value = "both";
        }

        boolean justifyLeft = false;
        boolean justifyRight = false;
        if (value.equalsIgnoreCase("left")) {
            justifyLeft = true;
        }
        if (value.equalsIgnoreCase("right")) {
            justifyRight = true;
        }
        if (value.equalsIgnoreCase("both")) {
            justifyLeft = true;
            justifyRight = true;
        }

        String strPadChar = " ";
        String arg = getValue(options, "pad-character");
        if (arg != null) {
            strPadChar = arg;
        }

        // if length is 0, we are removing padding, otherwise, we are adding it
        if (0 == length) {
            // Remove left justification
            if (justifyLeft) {
                int i;
                for (i = str.length() - 1; i > -1 && isPadChar(str.charAt(i), strPadChar); i--)
                    ;
                if (i != str.length() - 1) {
                    str = str.substring(0, i + 1);
                }
            }

            // Remove right justification
            if (justifyRight) {
                int i;
                for (i = 0; i < str.length() && isPadChar(str.charAt(i), strPadChar); i++)
                    ;
                if (i != 0) {
                    str = str.substring(i, str.length());
                }
            }
        } else {
            // pad only with first character
            strPadChar = strPadChar.substring(0, 1);

            if (str.length() < length) {
                // Figure out difference in length to create padding string
                int lenDiff = length - str.length();

                String padding = "";
                for (int i = 0; i < lenDiff; i++)
                    padding = padding + strPadChar;

                if (justifyLeft) {
                    str = str + padding;
                }

                if (justifyRight) {
                    str = padding + str;
                }
            }
        }

        return str;
    }

}