All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xapi.util.X_Util Maven / Gradle / Ivy

Go to download

Everything needed to run a comprehensive dev environment. Just type X_ and pick a service from autocomplete; new dev modules will be added as they are built. The only dev service not included in the uber jar is xapi-dev-maven, as it includes all runtime dependencies of maven, adding ~4 seconds to build time, and 6 megabytes to the final output jar size (without xapi-dev-maven, it's ~1MB).

The newest version!
package xapi.util;

import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.ExecutionException;




/**
 * Generic purpose utility methods;
 * this class has no fields, no
 *
 * @author "James X. Nelson ([email protected])"
 *
 */
public final class X_Util{

  private X_Util() {}

  public static boolean equal(final Object one, final Object two){
    return one == two || (one != null && one.equals(two));
  }

  public static  T firstNotNull(final T first, final T second) {
    return first == null ? second : first;
  }

  public static  T firstNotNull(final T first, final T second, final T third) {
    return first == null ? second == null ? third : second : first;
  }

  @SuppressWarnings("unchecked")
  public static  T firstNotNull(final T first, final T ... rest) {
    if (first == null) {
      for (final T t : rest) {
        if (t != null) {
          return t;
        }
      }
    }
    return first;
  }

  public static RuntimeException rethrow(Throwable e) {
    if (e instanceof RuntimeException)
     {
      throw (RuntimeException)e;// Don't re-wrap
    }
    if (e instanceof Error)
     {
      throw ((Error)e);// Just rethrow errors without wrappers
    }
    while (// unwrap checked wrappers, for ease later on
        e instanceof InvocationTargetException
        || e instanceof ExecutionException
        ) {
      if (e.getCause()!=null){
        e = e.getCause();
      } else if (e == e.getCause()) {
        break;
      }
    }
    if (e instanceof InterruptedException) {
      Thread.currentThread().interrupt();
    }
    // throw unchecked.
    throw new RuntimeException(e);
  }

  public static int zeroSafeInt(final Integer i) {
    return i == null ? 0 : i;
  }

  public static double zeroSafeDouble(final Number i) {
    return i == null ? 0 : i.doubleValue();
  }

  public static Throwable unwrap(Throwable e) {
    if (X_Runtime.isDebug()) {
      e.printStackTrace();// Avoid losing information in debug mode.
    }

    //don't use instanceof because we don't want to treat subclasses of RuntimeException as wrappers...
    while (e.getClass().equals(RuntimeException.class) || e.getClass().equals(ExecutionException.class)) {
      if (e.getCause() == null) {
        return e;
      }
      e = e.getCause();
    }
    return e;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy