package org.minimalj.repository.sql;

import java.util.HashMap;
import java.util.Map;

import org.minimalj.util.EqualsHelper;

@SuppressWarnings("unchecked")
public class EmptyObjects {
	private static Map<Class<?>, Object> emptyObjects = new HashMap<Class<?>, Object>();

	public static <T> boolean isEmpty(T object) {
		if (object != null && !object.equals("")) {
			Class<T> clazz = (Class<T>) object.getClass();
			if (clazz.getName().startsWith("java") || Enum.class.isAssignableFrom(clazz)) return false;
			
			T emptyObject = getEmptyObject(clazz);
			return EqualsHelper.equals(emptyObject, object);
		} else {
			return true;
		}
	}
	
	public static <T> T getEmptyObject(Class<T> clazz) {
		if (!emptyObjects.containsKey(clazz)) {
			try {
				Object emptyObject = clazz.newInstance();
				emptyObjects.put(clazz, emptyObject);
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
		return (T) emptyObjects.get(clazz);
	}

}

@@ -31,16 +31,74 @@
 y.sql;%0A%0A
+import java.math.BigDecimal;%0Aimport java.util.Collection;%0A
 import j
@@ -115,16 +115,16 @@
 ashMap;%0A
-
 import j
@@ -388,20 +388,19 @@
 ect 
-!
+=
 = null 
-&& !
+%7C%7C 
 obje
@@ -416,16 +416,376 @@
 (%22%22)) %7B%0A
+%09%09%09return true;%0A%09%09%7D else if (object instanceof Collection) %7B%0A%09%09%09return ((Collection%3C?%3E) object).isEmpty();%0A%09%09%7D else if (object instanceof Integer) %7B%0A%09%09%09return (Integer) object == 0;%09%09%0A%09%09%7D else if (object instanceof Long) %7B%0A%09%09%09return (Long) object == 0;%09%09%0A%09%09%7D else if (object instanceof BigDecimal) %7B%0A%09%09%09return ((BigDecimal) object).signum() == 0;%09%09%0A%09%09%7D else %7B%0A
 %09%09%09Class
@@ -967,16 +967,16 @@
 clazz);%0A
+
 %09%09%09retur
@@ -1023,35 +1023,8 @@
 t);%0A
-%09%09%7D else %7B%0A%09%09%09return true;%0A
 %09%09%7D%0A

handle collections / number