0
\$\begingroup\$

So again, I am a bit confused about some notes that i'am getting from my reviewer. I have some kind of utils, to print/prepare log messages. Yea, maybe I am a bit fanatic java 8 lambda guy but I think it is still under control. :)

So its looks like that [I hide 'sensitive' data]

public class ExceptionUtils {

  private static final BiFunction<String, String, String> CUSTOM_EXCEPTION_MESSAGE_FORMAT =
    (typeOfError, typeOfEndpoint) -> "[XXX] " + typeOfError + " Error during process bulk " + typeOfEndpoint + " events. "
        + "Event "
        + "occurrences time: %d "
        + "event retry count: %d, "
        + "session: %s";

  public static String validationMessageException(String endpoint, long timestamp, int retryCount, String sessionToken) {
    return String.format(CUSTOM_EXCEPTION_MESSAGE_FORMAT.apply("Validation", endpoint), timestamp, retryCount, sessionToken);
  }

  public static String unknownMessageException(String endpoint, long timestamp, int retryCount, String sessionToken) {
    return String.format(CUSTOM_EXCEPTION_MESSAGE_FORMAT.apply("Unknown", endpoint), timestamp, retryCount, sessionToken);
  }
}
\$\endgroup\$
4
  • \$\begingroup\$ I fix it, I have some problems if I copy paste then last brace is braking indents. \$\endgroup\$ Commented Oct 8, 2018 at 13:44
  • \$\begingroup\$ Do you prefer ˋapplyˋing to calling a function the classical way? To me this seems to be a useless use of lambda. ;-) \$\endgroup\$
    – aventurin
    Commented Oct 8, 2018 at 22:30
  • \$\begingroup\$ yea, maybe who knows if that class will contain other way to inject message formats. I just ask what people thinks, but as I see some of.. just cannot understand that 'asking' a questions is not a bad think. \$\endgroup\$ Commented Oct 9, 2018 at 7:05
  • \$\begingroup\$ You could use or assign a method reference to static String foo(String s1, String s2) {...} in places where a BiFunction<String, String, String> is expected. \$\endgroup\$
    – aventurin
    Commented Oct 9, 2018 at 20:17

1 Answer 1

3
\$\begingroup\$

What is the point of adding some parameters as part of format string when others are simply passed to String.format? You might as well just do the latter:

public class ExceptionUtils {

    private static final String CUSTOM_EXCEPTION_MESSAGE_FORMAT
            = "[XXX] %s Error during process bulk %s events. Event occurrences time: %d event retry count: %d, session: %s";

    private static String formatMessage(String type, String endpoint, long timestamp, int retryCount, String sessionToken) {
        return String.format(CUSTOM_EXCEPTION_MESSAGE_FORMAT, 
                type, endpoint, timestamp, retryCount, sessionToken);
    }

    public static String validationMessageException(String endpoint, long timestamp, int retryCount, String sessionToken) {
        return formatMessage("Validation", endpoint, timestamp, retryCount, sessionToken);
    }

    public static String unknownMessageException(String endpoint, long timestamp, int retryCount, String sessionToken) {
        return formatMessage("Unknown", endpoint, timestamp, retryCount, sessionToken);
    }
}

or even just:

public class ExceptionUtils {

    private static final String CUSTOM_EXCEPTION_MESSAGE_FORMAT
            = "[XXX] %s Error during process bulk %s events. Event occurrences time: %d event retry count: %d, session: %s";

    public static String validationMessageException(String endpoint, long timestamp, int retryCount, String sessionToken) {
        return String.format(CUSTOM_EXCEPTION_MESSAGE_FORMAT, 
                "Validation", endpoint, timestamp, retryCount, sessionToken);
    }

    public static String unknownMessageException(String endpoint, long timestamp, int retryCount, String sessionToken) {
        return String.format(CUSTOM_EXCEPTION_MESSAGE_FORMAT, 
                "Unknown", endpoint, timestamp, retryCount, sessionToken);
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.