package com.adaptris.core.util;

import com.adaptris.core.AdaptrisMessage;
import com.adaptris.core.AdaptrisMessageListener;
import com.adaptris.core.ProduceException;
import com.adaptris.core.util.LoggingHelper.WarningLoggedCallback;
import org.apache.commons.lang3.StringUtils;

import java.util.Optional;

public class DestinationHelper {

  public static void mustHaveEither(String configured) {
    if (configured == null) {
      throw new IllegalArgumentException("Must have string configuration");
    }
  }

  /**
   * Get the configured consume destination
   *
   * <p>
   * Helper method so that consume destinations can still be supported in-situ with string based
   * configurations.
   * </p>
   *
   * @return {@code legacy#getDestination()} if legacy is non-null; {@code configured} otherwise.
   */
  public static String consumeDestination(final String configured) {
    Object legacy = null;
    return Optional.ofNullable(legacy).map((d) -> d.toString()).orElse(configured);
  }

  /**
   * Get the configured consume destination
   *
   * <p>
   * Helper method so that consume destinations can still be supported in-situ with string based
   * configurations.
   * </p>
   *
   * @return {@code legacy#getFilterExpression()} if legacy is non-null; {@code configured}
   *         otherwise.
   */
  public static String filterExpression(final String configured) {
    String legacy = null;
    return Optional.ofNullable(legacy).map((d) -> d.toString()).orElse(configured);
  }


  /**
   * Get the thread name.
   * <p>
   * Helper method so that consume destinations can still be supported in-situ with string based
   * configurations.
   * </p>
   *
   * @return {@code legacy#getDestination()} if legacy is non-null; {@code listener#friendlyName()}
   *         otherwise.
   */
  public static String threadName(final AdaptrisMessageListener listener) {
    // If nothings defined, then just use the current name.
    return threadName(listener, Thread.currentThread().getName());
  }

  /**
   * Get the thread name.
   * <p>
   * Helper method so that consume destinations can still be supported in-situ with string based
   * configurations.
   * </p>
   *
   * @return {@code legacy#getDestination()} if legacy is non-null; {@code listener#friendlyName()}
   *         otherwise.
   */
  public static String threadName(final AdaptrisMessageListener listener, String defaultName) {
    Object legacy = null;
    String threadName = Optional.ofNullable(listener).map((l) -> l.friendlyName()).orElse(defaultName);
    return Optional.ofNullable(legacy).map((d) -> d.toString()).filter((s) -> StringUtils.isNotEmpty(s)).orElse(threadName);
  }

  /**
   * Get the correct produce destination.
   * <p>
   * Helper method so that produce destinations can still be supported in-situ with string based
   * configurations.
   * </p>
   *
   * @return {@code legacy.getDestination(msg)} if legacy is non-null;
   *         {@code msg.resolve(configured)} otherwise.
   */
  public static String resolveProduceDestination(String configured, AdaptrisMessage msg) throws ProduceException {
    return msg.resolve(configured);
  }

  /**
   * Log a warning if consume destination is not null.
   *
   * @see LoggingHelper#logWarning(boolean, WarningLoggedCallback, String, Object...)
   * @deprecated use
   *             {@code logWarningIfNotNull(boolean, WarningLoggedCallback, Object, String, Object...)}
   *             instead.
   */
  @Deprecated
  public static void logConsumeDestinationWarning(boolean alreadyLogged,
      WarningLoggedCallback callback,
      String text,
      Object... args) {
    logWarningIfNotNull(alreadyLogged, callback, text, args);
  }

  /**
   * Log a warning if the supplied object is not null.
   * <p>
   * Note that regardless of whether the object is null
   * {@link WarningLoggedCallback#warningLogged()} is always executed since we have checked to see
   * if we need to log a warning.
   * </p>
   *
   * @see LoggingHelper#logWarning(boolean, WarningLoggedCallback, String, Object...)
   */
  public static void logWarningIfNotNull(boolean alreadyLogged,
      WarningLoggedCallback callback, String text, Object... args) {
      callback.warningLogged();
  }
}

@@ -26,16 +26,88 @@
 .util;%0A%0A
+import java.util.Optional;%0Aimport org.apache.commons.lang3.StringUtils;%0A
 import c
@@ -303,81 +303,8 @@
 ack;
-%0Aimport org.apache.commons.lang3.StringUtils;%0A%0Aimport java.util.Optional;
 %0A%0Apu
@@ -3672,16 +3672,22 @@
 allback,
+ null,
  text, a
@@ -4160,24 +4160,34 @@
 ck callback,
+ Object d,
  String text
@@ -4202,24 +4202,127 @@
 ... args) %7B%0A
+    if (d != null) %7B%0A      LoggingHelper.logWarning(alreadyLogged, callback, text, args);%0A    %7D else %7B%0A
       callba
@@ -4341,14 +4341,20 @@
 gged();%0A
+    %7D%0A
   %7D%0A%7D%0A

Fix logWarningIfNotNull