package org.minimalj.util;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Iterator;

public class EqualsHelper {

	public static boolean equals(Object from, Object to) {
		if (from == null) {
			return to == null;
		}
		if (to == null) {
			return false;
		}
		if (FieldUtils.isAllowedPrimitive(from.getClass())) {
			return from.equals(to);
		}
		try {
			return _equals(from, to);
		} catch (IllegalAccessException | IllegalArgumentException x) {
			throw new RuntimeException(x);
		}
	}
	
	@SuppressWarnings({ "rawtypes" })
	private static boolean _equals(Object from, Object to) throws IllegalArgumentException, IllegalAccessException {
		for (Field field : from.getClass().getDeclaredFields()) {
			if (FieldUtils.isStatic(field)) continue;
			field.setAccessible(true);
			Object fromValue = field.get(from);
			Object toValue = field.get(to);
			
			if (fromValue == null && toValue == null) continue;
			if (fromValue == null && toValue != null) return false;
			if (fromValue != null && toValue == null) return false;
			
			if (fromValue instanceof Collection) {
				Collection fromCollection = (Collection) fromValue;
				Collection toCollection = (Collection) toValue;

				if (fromCollection.size() != toCollection.size()) return false;
				Iterator toIterator = toCollection.iterator();
				for (Object fromObject : fromCollection) {
					boolean itemEqual = _equals(fromObject, toIterator.next());
					if (!itemEqual) return false;
				}
			} else if (IdUtils.hasId(fromValue.getClass())) {
				if (!IdUtils.equals(fromValue, toValue)) {
					return false;
				}
			} else {
				if (!fromValue.equals(toValue)) {
					return false;
				}
			}
		}
		return true;
	}
	
}

@@ -548,16 +548,29 @@
 awtypes%22
+, %22unchecked%22
  %7D)%0A%09pri
@@ -1609,32 +1609,166 @@
 rn false;%0A%09%09%09%09%7D%0A
+%09%09%09%7D else if (fromValue instanceof Comparable) %7B%0A%09%09%09%09if (((Comparable) fromValue).compareTo(toValue) != 0) %7B%0A%09%09%09%09%09return false;%0A%09%09%09%09%7D%0A
 %09%09%09%7D else %7B%0A%09%09%09%09

fix for BigDecimal