com.xlrit.gears.base.util.CommonUtils Maven / Gradle / Ivy
package com.xlrit.gears.base.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import com.google.common.collect.ImmutableList;
import org.springframework.core.io.Resource;
public class CommonUtils {
private CommonUtils() {}
@SuppressWarnings("unchecked")
public static Class getRealClass(T instance) {
Objects.requireNonNull(instance, "Cannot determine real class of null value");
return (Class) org.hibernate.Hibernate.getClass(instance);
}
public static Class> getNormalizedType(Object instance) {
Objects.requireNonNull(instance, "Cannot determine normalized type of null value");
if (instance instanceof List) return List.class;
if (instance instanceof Set) return Set.class;
if (instance instanceof Collection) return Collection.class;
return org.hibernate.Hibernate.getClass(instance);
}
public static List safeCopy(List list) {
return list == null ? null : ImmutableList.copyOf(list);
}
public static Properties loadProperties(Resource resource) {
if (resource == null || !resource.exists()) throw new IllegalArgumentException("Cannot load properties from non-existent resource: " + resource);
try (InputStream is = resource.getInputStream()) {
Properties properties = new Properties();
properties.load(is);
return properties;
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
}