Java Message Format formatCause(final String message, final Throwable cause, final Object... args)

Here you can find the source of formatCause(final String message, final Throwable cause, final Object... args)

Description

format Cause

License

Open Source License

Declaration

public static String formatCause(final String message, final Throwable cause, final Object... args) 

Method Source Code


//package com.java2s;
/*//from   ww  w . j av a  2  s .c om
 * Copyright 2000-2013 Enonic AS
 * http://www.enonic.com/license
 */

import java.text.MessageFormat;

public class Main {
    public static String formatCause(final String message, final Throwable cause, final Object... args) {
        if (cause == null) {
            return format(message, args);
        } else {
            return formatThrowable(format(message, args), cause);
        }
    }

    public static String format(final String message, final Object... args) {
        if (args == null || args.length == 0) {
            return message;
        } else {
            return MessageFormat.format(message, args);
        }
    }

    private static String formatThrowable(final String message, final Throwable cause) {
        if (cause == null) {
            return message;
        }

        final int index = message.indexOf("%t");

        if (index >= 0) {
            final String msg = cause != null ? cause.getMessage() : null;
            final String text = msg != null ? msg : "null";

            final StringBuilder stringBuilder = new StringBuilder(message);
            stringBuilder.replace(index, index + 2, text);
            return stringBuilder.toString();
        }

        return message;
    }
}

Related

  1. format(String pattern, String[] replacements)
  2. format(String str, int replacement)
  3. format(String str, Object... arguments)
  4. format(String textPattern, Object... args)
  5. formatAsNumber(double value)
  6. formatDuring(long mss, String pattern)
  7. formatError(Map.Entry entry, String errorPlural, String errorSingular)
  8. formatMessage(final String pattern, final Object... params)
  9. formatMessage(final String pattern, final String param1)