com.spun.util.ObjectUtils Maven / Gradle / Ivy
package com.spun.util;
import org.lambda.actions.Action0;
import org.lambda.actions.Action0WithException;
import org.lambda.functions.Function0WithException;
import org.lambda.query.Query;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
/**
* A static class of convenience functions for Manipulating objects
**/
public class ObjectUtils
{
public static Class> loadClass(String className)
{
return throwAsError(() -> Class.forName(className, true, Thread.currentThread().getContextClassLoader()));
}
public static int generateHashCode(Object... relevantMembers)
{
final int PRIME = 31;
int result = 1;
for (Object member : relevantMembers)
{
result = PRIME * result + ((member == null) ? 0 : member.hashCode());
}
return result;
}
/**
* tests if two objects are equal for all functions passed.
**/
public static boolean isEqualForMethods(Object o1, Object o2, String... methods)
{
try
{
Method[] m1 = getMethodsForObject(o1, methods);
Method[] m2 = getMethodsForObject(o2, methods);
for (int i = 0; i < m1.length; i++)
{
Object v1 = m1[i].invoke(o1, (Object[]) null);
Object v2 = m2[i].invoke(o2, (Object[]) null);
if (!isEqual(v1, v2))
{ return false; }
}
return true;
}
catch (Throwable t)
{
throw new Error(t);
}
}
public static Method[] getMethodsForObject(Object o2, String[] passedMethods)
{
Method[] methods = new Method[passedMethods.length];
Class> clazz = o2.getClass();
for (int i = 0; i < passedMethods.length; i++)
{
String methodName = passedMethods[i];
Class[] parameters = null;
methods[i] = throwAsError(() -> clazz.getMethod(methodName, parameters));
}
return methods;
}
/**
* A convenience function to check if 2 strings are equal.
**/
public static boolean isEqual(Object s1, Object s2)
{
if (s1 == s2)
{
return true;
}
else if ((s1 != null) && s1.equals(s2))
{
return true;
}
else
{
return false;
}
}
public static boolean isIn(Object target, Object[] objects)
{
for (int i = 0; i < objects.length; i++)
{
if (ObjectUtils.isEqual(objects[i], target))
{ return true; }
}
return false;
}
public static boolean isThisInstanceOfThat(Class> thiz, Class> that)
{
return that.isAssignableFrom(thiz);
}
public static Error throwAsError(Throwable t)
{
if (t instanceof RuntimeException)
{
throw (RuntimeException) t;
}
else if (t instanceof Error)
{
throw (Error) t;
}
else
{
throw new Error(t);
}
}
public static T getRandomIndex(T[] array)
{
if ((array == null) || (array.length == 0))
{ return null; }
return array[NumberUtils.RANDOM.nextInt(array.length)];
}
public static Method getGreatestCommonDenominator(Object[] from, String methodName)
{
List> classes = new ArrayList<>();
ArrayUtils.addArray(classes, getAllCastableClasses(from[0]));
for (Object o : from)
{
for (int i = classes.size() - 1; i >= 0; i--)
{
Class> clazz = classes.get(i);
if (!isThisInstanceOfThat(o.getClass(), clazz) || !ClassUtils.hasMethod(clazz, methodName))
{
classes.remove(i);
}
}
}
return classes.size() == 0
? null
: throwAsError(() -> ArrayUtils.getLast(classes).getMethod(methodName, (Class[]) null));
}
private static Class>[] getAllCastableClasses(Object object)
{
Class extends Object> clazz = object.getClass();
ArrayList
© 2015 - 2024 Weber Informatics LLC | Privacy Policy