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

prompto.utils.ObjectUtils Maven / Gradle / Ivy

The newest version!
package prompto.utils;

import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public abstract class ObjectUtils {

	@SuppressWarnings("unchecked")
	public static  T downcast(Class klass, Object actual) {
		if(actual!=null && klass.isAssignableFrom(actual.getClass()))
			return (T)actual;
		else
			return null;
	}

	@SuppressWarnings("unchecked")
	public static Set> getClassesInCallStack() {
		return (Set>)(Object) Stream.of(new Throwable().getStackTrace())
				.map(StackTraceElement::getClassName)
				.filter(s->s.indexOf("$Lambda$")<0)
				.map(n->{try{ return Class.forName(n); } catch (ClassNotFoundException e) { throw new RuntimeException(e); }})
				.collect(Collectors.toSet());
	}

	@SuppressWarnings("unchecked")
	public static int safeCompare(Object a, Object b) {
		if(a==b)
			return 0;
		else if(a==null)
			return -1;
		else if(b==null)
			return 1;
		else if(a instanceof Comparable)
			return ((Comparable)a).compareTo(b);
		else throw new RuntimeException("Not a comparable object:" + a);
	}

}